1215 maze
6 people are recommended to add questions to favorites
Description
Description
In the N * n maze, "#" is the wall, "." is the path, "S" is the start point, and "e" is the end point. A total of four directions can be taken. Go to the (n-1, n-1) "E") position in the upper-left corner (0, 0) "S"). You can use the general rule to output yes. If not, no is output.
Input description
Input description
The first input behavior is an integer m, indicating the number of maze. Then, the first behavior of each maze data is an integer n (n ≤ 16), which indicates the length of the sides of the maze. The next n rows contain N characters in each line and are separated by spaces.
Output description
Output description
The output contains m rows. If the maze corresponding to each row can go, yes is output; otherwise, no is output.
Sample Input
Sample Input
1 7 s...##. .#..... ....... ..#.... ..#...# ###...# ......e
Sample output
Sample output
YES
Simply put, this question is simply explained by the DFS and then expanded to the surrounding area.
#include<stdio.h>#include<math.h>char a[1001][1001],ans=0;int directx[5]={0,0,0,-1,1},vis[1001][1001];int directy[5]={0,-1,1,0,0};void dfs(int i,int j,int n){ if(i<=0||i>n||j<=0||j>n)return ; if(vis[i][j]==1||a[i][j]==‘#‘)return ; if(a[i][j]==‘e‘){ans=a[i][j]; printf("YES"); } vis[i][j]=1; for(int x=1;x<=4;x++) dfs(i+directx[x],j+directy[x],n) ;}int main(){ int b,i,z,o,j,n; scanf("%d",&z); for(o=1;o<=z;o++) { scanf("%d\n",&n); for(i=1;i<=n;i++) for(j=1;j<=n;j++) { if(j<n) scanf("%c",&a[i][j]); else scanf("%c\n",&a[i][j]); } dfs(1,1,n); if(ans==0)printf("NO"); printf("\n"); } return 0;}