Problem Description
给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍, 她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去。令人头痛的是,gloria是个没什 么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的。我们假定给定的两个位置都是空地,初始时,gloria所面向的方向未定,她可以 选择4个方向的任何一个出发,而不算成一次转弯。gloria能从一个位置走到另外一个位置吗?
|
Input
第1行为一个整数t (1 ≤ t ≤ 100),表示测试数据的个数,接下来为t组测试数据,每组测试数据中,
第1行为两个整数m, n (1 ≤ m, n ≤ 100),分别表示迷宫的行数和列数,接下来m行,每行包括n个字符,其中字符'.'表示该位置为空地,字符'*'表示该位置为障碍,输入数据中只有这两种字符,每组测试数据的最后一行为5个整数k, x1, y1, x2, y2 (1 ≤ k ≤ 10, 1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m),其中k表示gloria最多能转的弯数,(x1, y1), (x2, y2)表示两个位置,其中x1,x2对应列,y1, y2对应行。 |
Output
每组测试数据对应为一行,若gloria能从一个位置走到另外一个位置,输出“yes”,否则输出“no”。
|
Sample Input
2 5 5 ...** *.**. ..... ..... *.... 1 1 1 1 3 5 5 ...** *.**. ..... ..... *.... 2 1 1 1 3 |
Sample Output
no yes |
Code View Code
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <queue> 4 using namespace std; 5 #define N 100 6 #define M 100 7 struct Position 8 { 9 int x; 10 int y; 11 int numOfTurn; 12 }; 13 char map[M + 5][N + 5]; 14 int flags[M + 5][N + 5]; 15 int dx[5] = {1, 0, 0, -1}; 16 int dy[5] = {0, -1, 1, 0}; 17 int main() 18 { 19 int c, m, n, k, x1, y1, x2, y2, flag, floornum, i, j; 20 Position source, des, pre, cur; 21 scanf("%d", &c); 22 while (c--) 23 { 24 queue<Position> q; 25 scanf("%d %d", &m, &n); 26 //input 27 for (i = 0; i < m; i++) 28 scanf("%s", map[i]); 29 scanf("%d %d %d %d %d", &k, &x1, &y1, &x2, &y2); 30 for (i = 0; i < m; i++) 31 for (j = 0; j < n; j++) 32 flags[i][j] = 0; 33 //printf("input successfully\n"); 34 source.x = y1 - 1; 35 source.y = x1 - 1; 36 source.numOfTurn = -1; 37 q.push(source); 38 des.x = y2 - 1; 39 des.y = x2 - 1; 40 flag = 0; //mark whether find it or not 41 flags[source.x][source.y] = 1; 42 //the source is equal to destination 43 if (source.x == des.x && source.y == des.y) 44 flag = 1; 45 while (!q.empty() && !flag) 46 { 47 pre = q.front(); 48 q.pop(); 49 //printf("pop(): x = %d, y = %d, dir = %d, numOfTurn = %d\n", pre.x, pre.y, pre.dir, pre.numOfTurn); 50 for (i = 0; i < 4; i++) 51 { 52 //get the current 53 cur.x = pre.x + dx[i]; 54 cur.y = pre.y + dy[i]; 55 //it's necessary to explain the next line. 56 //because we use flags[][] to record whether we have passed or not,and every step, we always traversal whole row or column 57 //in this way, every step need to plus one. 58 cur.numOfTurn = pre.numOfTurn + 1; 59 //out of time 60 if (cur.numOfTurn > k) 61 continue; 62 while (cur.x >= 0 && cur.x < m && cur.y >= 0 && cur.y < n && map[cur.x][cur.y] == '.') 63 { 64 //note though flags[][] is equal to zero, we still must judge the next point 65 if (flags[cur.x][cur.y] == 0) 66 { 67 flags[cur.x][cur.y] = 1; 68 q.push(cur); 69 //printf("push(): x = %d, y = %d, dir = %d, numOfTurn = %d\n", cur.x, cur.y, cur.dir, cur.numOfTurn); 70 if (cur.x == des.x && cur.y == des.y) 71 { 72 flag = 1; 73 break; 74 //printf("find it. i = %d\n", i); 75 } 76 } 77 cur.x += dx[i]; 78 cur.y += dy[i]; 79 } 80 } 81 //printf("--------------------------------\n"); 82 } 83 if (flag == 0) 84 printf("no\n"); 85 else 86 printf("yes\n"); 87 } 88 system("pause"); 89 return 0; 90 } |
Idea For this question, I think it's really simple firstly. But actually, it have a lots detail to note. I just modify the previosu program to solve this question, when I finished and submit, I get out of Money, it's terrible! After consider and reading others' blogs, I find what the question want is to find the shortest path, if u want to use BFS, even thought u find it, but it don't present the shortest. Because what the question care about is the number of turning around rathan than step. So I want to traversal all the point again and again to find the shortest. But it doesn't work, finally, I find that if we traversal whole row or colum every step, then we use BFS, we can find the shortest path. In conclusion, if we want the shortest path, we can use BFS, but I should make it clear what's the step. |