Title Description
Description
Inside the maze of N*n, "#" is the Wall, "." For the road, "s" for the beginning, "E" for the end, altogether 4 directions can walk. From the upper left corner ((0,0) "s" position to the lower right corner ((n-1,n-1) "E") position, you can walk the general output Yes, can not go output no.
Enter a description input
Description
The first behavior of the input is an integer m, which indicates the number of mazes.
The first behavior of each maze data followed by an integer n (n≤16), representing the edge length of the maze, followed by n lines of n characters per line, with no spaces separating the characters.
outputs description output
Description
Output has m line, each line corresponding to the maze can go, then output yes, otherwise output No.
sample input to
sample
... E
Sample output Sample
outputs
YES
data
size & Hint first of all, the solution of Dfs, is the normal search, from a location to four directions to search, but remember to use a record array to record whether the current location has been accessed in the same way
#include <stdio.h> #include <string.h> #define N 25char map[n][n];bool visited[n][n][5];int N, flag;//i, J stands for coordinates, d for direction void Dfs (int i, int j, int d) {if (flag = = 1 | | visited[i][j][d]) return;if (map[i][j] = = ' E ') {flag = 1;printf ("Y Es\n ");} VISITED[I][J][D] = 1;//If the current position is not the previous position down, then you can go up if (d! = 2 && map[i-1][j]! = ' # ' && i-1 >= 0) DFS (i- 1, J, 1); if (d! = 1 && map[i + 1][j]! = ' # ' && i + 1 < n) Dfs (i + 1, J, 2); if (d! = 4 && map[i] [j-1]! = ' # ' && j-1 >= 0) Dfs (i, j-1, 3); if (d! = 3 && map[i][j + 1]! = ' # ' && j + 1 < N) Dfs (i, j + 1, 4);} int main () {int m;scanf ("%d", &m), while (m--) {flag = 0;memset (visited, 0, sizeof (visited)); scanf ("%d", &n); int i; for (i = 0; i < n; i++) scanf ("%s", Map[i]);d FS (0, 0, 0), if (flag = = 0) printf ("no\n");} return 0;}
Then talk about the BFS solution, here is the BFS code is relatively long, mainly hand-out stack,BFS is no change, that is, the starting point into the stack, and then start to judge the empty stack, non-empty is out of the stack, and then out of the stack position of four points to meet the conditions can be into the stack
#include <stdio.h> #include <string.h> #define N 25char map[n][n];bool visited[n][n];struct node{int x, y;}; struct Queue{node a[10000];int head, rear; Queue () {head = 0; rear = 0;} void push (int i, int j) {a[rear].x = I;a[rear++].y = j;} void Pop (Node *b) {b->x = A[head].x;b->y = a[head].y;head++;} BOOL IsEmpty () {return head >= rear;}} Q;int Main () {int m, n;scanf ("%d", &m), while (m--) {int flag = 0;memset (visited, 0, sizeof (visited)); scanf ("%d", &n int i;for (i = 0; i < n; i++) scanf ("%s", Map[i]), visited[0][0] = 1;q.push (0, 0); while (!q.isempty ()) {Node B;q.pop (&am P;B)///If the point below the current position can go if (!visited[b.x + 1][b.y] && map[b.x + 1][b.y]! = ' # ' && b.x + 1 < N) {if (map[b.x + 1][B.Y] = = ' E ') {flag = 1;break;} visited[b.x + 1][b.y] = 1;q.push (b.x + 1, b.y);} If the point above the current position can go if (!visited[b.x-1][b.y] && map[b.x-1][b.y]! = ' # ' && b.x-1 >= 0) {if (map[b.x-1] [B.Y] = = ' E ') {flag = 1;break;} VISITED[B.X-1][B.Y] = 1;q.push (b.x-1, b.y);} IfThe point to the right of the current position can go if (!visited[b.x][b.y + 1] && MAP[B.X][B.Y + 1]! = ' # ' && b.y + 1 < N) {if (map[b.x][b.y + 1] = = ' E ') {flag = 1;break;} VISITED[B.X][B.Y + 1] = 1;q.push (b.x, b.y + 1);} If the point at the left of the current position can go if (!visited[b.x][b.y-1] && map[b.x][b.y-1]! = ' # ' && b.y-1 >= 0) {if (map[b.x][b.y -1] = = ' E ') {flag = 1;break;} Visited[b.x][b.y-1] = 1;q.push (b.x, b.y-1);}} if (flag = = 0) printf ("no\n"), Else printf ("yes\n");} return 0;}
Maze---DFS and BFS solutions