Links: https://ac.nowcoder.com/acm/challenge/terminal
Source: Cattle-off network
Xiao Ming is now playing a game, the game came to the teaching points, it is a maze of N * M matrix.
Xiao Ming's starting point on the map with "S" to indicate the end point with "E" to represent an obstacle by "#" is represented by open space "." To represent.
It can not be an obstacle. If Bob is now at a point (x, y), the next step can only go four adjacent grid in one of: (x + 1, y), (x-1, y), (x, y +1), (x, y-1);
Xiao Ming wants to know, he can now come to the end from the beginning.
Enter a description:
This title contains multiple sets of data.
Each set of data input to two numbers N, M
Next N lines of M symbols to represent a map of the state.
Data range:
2 <= N, M <= 500
ensure that there is a starting point S, while ensuring an end E.
Output Description:
Each data output line, if Bob can come to the end from the beginning, then the output Yes, otherwise output No
Example 1
Export
copyYes No
problem-solving ideas: classical BFD
#pragma GCC optimize(2) #include<bits/stdc++.h> using namespace std; inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;} typedef long long ll; const int maxn = 1e5+10; char a[600][600]; int vis[600][600]; struct node{ int x,y; }; int d[4][2]={0,1,0,-1,1,0,-1,0}; int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF){ node S;//记录起点 for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>a[i][j]; if(a[i][j]=='S'){ S.x=i; Sy = j; } } } int flag=0; Memset (VIS, 0 , the sizeof (VIS)); // Clear tag array Queue <Node> Q; // define a queue q.push (S); // inject a starting point the while (! q.empty ()) { TEMP Node = q.front (); // Analyzing q.pop (); // pop-up point for the first IF (A [temp.x] [temp.y] == ' E ' ) { flag=1; break; } vis[temp.x][temp.y]=1; for(int i=0;i<4;i++){ node R; R.x=temp.x+d[i][0]; R.y=temp.y+d[i][1]; if(R.x>=1&&R.x<=n&&R.y>=1&&R.y<=m&&vis[R.x][R.y]==0&&a[R.x][R.y]!='#'){ manner [R x] [y] = 1 ; q.push (R & lt); // stack idle determination } } } if(flag==1){ printf("Yes\n"); } else{ printf("No\n"); } } return 0; }
AC code 2;
#include<cstdio> #include<cstring> #include<queue> using namespace std; char map[600][600]; char s[100]; bool vis[600][600]; struct Node{ int x,y; }; int d[4][2]={0,1,0,-1,1,0,-1,0}; int main() { int N,M; while(scanf("%d%d",&N,&M)!=EOF) { Node S; gets(s);//getchar(); for(int i=1;i<=N;i++){ for(int j=1;j<=M;j++){ scanf("%c",&map[i][j]); if(map[i][j]=='S'){ S.x=i; Sy = j; } } gets(s); } int flag=0; memset(vis,0,sizeof(vis)); queue<Node>q; q.push(S); while(!q.empty()) { Node temp=q.front(); q.pop(); if(map[temp.x][temp.y]=='E') {flag=1;break;} vis[temp.x][temp.y]=1; for(int i=0;i<4;i++) { Node R; R.x=temp.x+d[i][0];R.y=temp.y+d[i][1]; if(R.x<1||R.y<1||R.x>N||R.y>M) continue; if(!vis[R.x][R.y]&&map[R.x][R.y]!='#') { manner [R x] [y] = 1 ; q.push(R); } } } if(flag) printf("Yes\n"); else printf("No\n"); } return 0; }