求助,bfs只过了50%

https://ac.nowcoder.com/acm/contest/23156/1014

#include<bits/stdc++.h>

using namespace std;

const int N=1000;

int dx[]={1,-1,0,0};

int dy[]={0,0,-1,1};

int stx,sty,edx,edy;

int v[N][N]={0};

struct point{

int x;

int y;

};

queue<point> q;

int t,n,m;

char s[N][N];

void bfs()

{

int flag=1;

q.push({stx,sty});

v[stx][sty]=1;

while(!q.empty())

{

point t=q.front();

q.pop();

if(t.x==edx&&t.y==edy)

{

cout<<"YES"<<endl;

flag=0;

break;

}

else{

for(int i=0;i<4;i++)

{

int tx=t.x+dx[i];

int ty=t.y+dy[i];

if(tx<1||tx>n||ty<1||ty>m) continue;

if(v[tx][ty]==0)

if(s[tx][ty]=='.'||s[tx][ty]=='t')

{

q.push({tx,ty});

v[tx][ty]=1;

}

}

}

}

if(flag)

cout<<"NO"<<endl;

}

int main()

{

cin>>t;

while(t--)

{

cin>>n>>m;

for(int i=1;i<=n;i++)

for(int j=1;j<=m;j++)

{

cin>>s[i][j];

if(s[i][j]=='s')

{

stx=i;

sty=j;

}

if(s[i][j]=='t')

{

edx=i;

edy=j;

}

}

bfs();

}

return 0;

}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务