1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<queue>
5 #define N 18
6 using namespace std;
7 int map[N][N],n,T,x,y,sx,sy;
8 int dx[]={0,0,1,-1};int dy[]={1,-1,0,0};
9 queue<int> qx,qy;
10 bool BFS()
11 {
12 qx.push(x);qy.push(y);
13 while(!qx.empty())
14 {
15 int zx=qx.front();int zy=qy.front();
16 qx.pop();qy.pop();
17 if(zx==sx&&zy==sy)
18 return true;
19 for(int i=0;i<4;i++)
20 {
21 int xx=zx+dx[i],yy=zy+dy[i];
22 if(map[xx][yy]==1)
23 {
24 qx.push(xx);qy.push(yy);
25 }
26 }
27 }
28 return false;
29 }
30 int main()
31 {
32 char sh;
33 scanf("%d",&T);
34 while(T--)
35 {
36 scanf("%d",&n);
37 memset(map,0,sizeof(map));
38 for(int i=1;i<=n;i++)
39 for(int j=1;j<=n;j++)
40 {
41 cin>>sh;
42 if(sh==‘#‘) map[i][j]=0;
43 if(sh==‘.‘) map[i][j]=1;
44 if(sh==‘s‘){
45 x=i;y=j;map[i][j]=1;
46 }
47 if(sh==‘e‘){
48 sx=i;sy=j;map[i][j]=1;
49 }
50 }
51 if(BFS())
52 printf("YES\n");
53 else printf("NO\n");
54 }
55 return 0;
56 }
1 #include <iostream>
2 using namespace std;
3 const int MAXN=18;
4 char map[MAXN][MAXN];
5 int book[MAXN][MAXN];
6 int flag=0;
7 int m,n;
8 int dx[]={0,0,1,-1};
9 int dy[]={1,-1,0,0};
10 bool islegal(int x,int y)
11 {
12 if (x>=1 && x<=m &&y>=1 && y<=m)
13 return true;
14 else return false;
15 }
16 void dfs(int x,int y);
17 int main()
18 {
19 cin>>n>>m;
20 for (int i=1;i<=m;i++)
21 for (int j=1;j<=m;j++)
22 cin>>map[i][j];
23 int p,q;
24 dfs(1,1);
25 if (flag==0)
26 cout<<"NO"<<endl;
27 return 0;
28 }
29 void dfs(int x,int y)
30 {
31 book[x][y]=1;
32 if(flag==1)
33 return;
34 if (x==m && y==m)
35 {
36 flag=1;
37 cout<<"YES"<<endl;
38 }
39 for (int i=0;i<4;i++)
40 {
41 int nx=x+dx[i];
42 int ny=y+dy[i];
43 if (map[nx][ny]!=‘#‘ && book[nx][ny]==0 && islegal(nx,ny))
44 {
45 dfs(nx,ny);
46 book[nx][ny]==0;
47 }
48 }
49 }