HDOJ_ACM_逃离迷宫
2013-04-13 11:02
323 查看
[align=left]Problem Description[/align] 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍, 她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去。令人头痛的是,gloria是个没什 么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的。我们假定给定的两个位置都是空地,初始时,gloria所面向的方向未定,她可以 选择4个方向的任何一个出发,而不算成一次转弯。gloria能从一个位置走到另外一个位置吗? |
[align=left]Input[/align] 第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对应行。 |
[align=left]Output[/align] 每组测试数据对应为一行,若gloria能从一个位置走到另外一个位置,输出“yes”,否则输出“no”。 |
[align=left]Sample Input[/align]2 5 5 ...** *.**. ..... ..... *.... 1 1 1 1 3 5 5 ...** *.**. ..... ..... *.... 2 1 1 1 3 |
[align=left]Sample Output[/align]no yes |
Code View Code #include <stdio.h> #include <stdlib.h> #include <queue> using namespace std; #define N 100 #define M 100 struct Position { int x; int y; int numOfTurn; }; char map[M + 5][N + 5]; int flags[M + 5][N + 5]; int dx[5] = {1, 0, 0, -1}; int dy[5] = {0, -1, 1, 0}; int main() { int c, m, n, k, x1, y1, x2, y2, flag, floornum, i, j; Position source, des, pre, cur; scanf("%d", &c); while (c--) { queue<Position> q; scanf("%d %d", &m, &n); //input for (i = 0; i < m; i++) scanf("%s", map[i]); scanf("%d %d %d %d %d", &k, &x1, &y1, &x2, &y2); for (i = 0; i < m; i++) for (j = 0; j < n; j++) flags[i][j] = 0; //printf("input successfully\n"); source.x = y1 - 1; source.y = x1 - 1; source.numOfTurn = -1; q.push(source); des.x = y2 - 1; des.y = x2 - 1; flag = 0; //mark whether find it or not flags[source.x][source.y] = 1; //the source is equal to destination if (source.x == des.x && source.y == des.y) flag = 1; while (!q.empty() && !flag) { pre = q.front(); q.pop(); //printf("pop(): x = %d, y = %d, dir = %d, numOfTurn = %d\n", pre.x, pre.y, pre.dir, pre.numOfTurn); for (i = 0; i < 4; i++) { //get the current cur.x = pre.x + dx[i]; cur.y = pre.y + dy[i]; //it's necessary to explain the next line. //because we use flags[][] to record whether we have passed or not,and every step, we always traversal whole row or column //in this way, every step need to plus one. cur.numOfTurn = pre.numOfTurn + 1; //out of time if (cur.numOfTurn > k) continue; while (cur.x >= 0 && cur.x < m && cur.y >= 0 && cur.y < n && map[cur.x][cur.y] == '.') { //note though flags[][] is equal to zero, we still must judge the next point if (flags[cur.x][cur.y] == 0) { flags[cur.x][cur.y] = 1; q.push(cur); //printf("push(): x = %d, y = %d, dir = %d, numOfTurn = %d\n", cur.x, cur.y, cur.dir, cur.numOfTurn); if (cur.x == des.x && cur.y == des.y) { flag = 1; break; //printf("find it. i = %d\n", i); } } cur.x += dx[i]; cur.y += dy[i]; } } //printf("--------------------------------\n"); } if (flag == 0) printf("no\n"); else printf("yes\n"); } system("pause"); return 0; } |
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. |
相关文章推荐
- HDOJ 4524 郑厂长系列故事——逃离迷宫 杭电 ACM
- HDOJ1728(逃离迷宫)
- Hdoj 1728 逃离迷宫
- HDOJ 1728 逃离迷宫 (DFS)
- HDOJ/HDU 1728 逃离迷宫 DFS 深度优先搜素
- HDOJ 1728 逃离迷宫(BFS)
- ACM -- 逃离迷宫
- ACM HDOJ 1272 (小希的迷宫)
- 【HDOJ】1728 逃离迷宫
- HDOJ HDU 1272 小希的迷宫 ACM 1272 IN HDU
- HDOJ 题目1728 逃离迷宫(BFS)
- HDOJ 1728 逃离迷宫 (BFS)
- ACM-简单题之郑厂长系列故事(逃离迷宫)——hdu4524
- HDOJ-1728 逃离迷宫(BFS + 转弯)
- 逃离ACM迷宫(BFS)
- hdoj 1728 逃离迷宫
- [HDOJ 4524] 郑厂长系列故事――逃离迷宫
- [置顶] HDOJ 1728 逃离迷宫 (BFS )解题报告
- hdoj1728逃离迷宫【BFS】
- HDOJ(1728)逃离迷宫