POJ3083 DFS&BFS
来源:互联网 发布:mac分段压缩文件 编辑:程序博客网 时间:2024/06/12 23:08
Description
The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit.
One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.)
As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.
One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.)
As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.
Input
Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks ('#'), empty space by periods ('.'), the start by an 'S' and the exit by an 'E'.
Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also be separated by at least one wall ('#').
You may assume that the maze exit is always reachable from the start point.
Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also be separated by at least one wall ('#').
You may assume that the maze exit is always reachable from the start point.
Output
For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the 'S' and 'E') for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.
Sample Input
28 8#########......##.####.##.####.##.####.##.####.##...#..##S#E####9 5##########.#.#.#.#S.......E#.#.#.#.##########
Sample Output
37 5 517 17 9
#include <IOSTREAM>#include <QUEUE>#include <CSTDLIB>using namespace std;#define MM 65bool MA[MM][MM];typedef struct{int x, y;int step;}St;St START, END;int LeftStep, RightStep, ShortStep;void DFS_LeftFirst(int direction, int x, int y){LeftStep++;if (x == END.x&&y == END.y){cout << LeftStep << " ";return;}switch (direction){case 0:{if (MA[x][y - 1])DFS_LeftFirst(1, x, y - 1);else if (MA[x - 1][y])DFS_LeftFirst(0, x - 1, y);else if (MA[x][y + 1])DFS_LeftFirst(3, x, y + 1);else if (MA[x + 1][y])DFS_LeftFirst(2, x + 1, y);break;}case 1:{if (MA[x + 1][y])DFS_LeftFirst(2, x + 1, y);else if (MA[x][y - 1])DFS_LeftFirst(1, x, y - 1);else if (MA[x - 1][y])DFS_LeftFirst(0, x - 1, y);else if (MA[x][y + 1])DFS_LeftFirst(3, x, y + 1);break;}case 2:{if (MA[x][y + 1])DFS_LeftFirst(3, x, y + 1);else if (MA[x + 1][y])DFS_LeftFirst(2, x + 1, y);else if (MA[x][y - 1])DFS_LeftFirst(1, x, y - 1);else if (MA[x - 1][y])DFS_LeftFirst(0, x - 1, y);break;}case 3:{if (MA[x - 1][y])DFS_LeftFirst(0, x - 1, y);else if (MA[x][y + 1])DFS_LeftFirst(3, x, y + 1);else if (MA[x + 1][y])DFS_LeftFirst(2, x + 1, y);else if (MA[x][y - 1])DFS_LeftFirst(1, x, y - 1);break;}}}void DFS_RightFirst(int direction, int x, int y){RightStep++;if (x == END.x&&y == END.y){cout << RightStep << " ";return;}switch (direction){case 0:{if (MA[x][y + 1])DFS_RightFirst(3, x, y + 1);else if (MA[x - 1][y])DFS_RightFirst(0, x - 1, y);else if (MA[x][y - 1])DFS_RightFirst(1, x, y - 1);else if (MA[x + 1][y])DFS_RightFirst(2, x + 1, y);break;}case 1:{if (MA[x - 1][y])DFS_RightFirst(0, x - 1, y);else if (MA[x][y - 1])DFS_RightFirst(1, x, y - 1);else if (MA[x + 1][y])DFS_RightFirst(2, x + 1, y);else if (MA[x][y + 1])DFS_RightFirst(3, x, y + 1);break;}case 2:{if (MA[x][y - 1])DFS_RightFirst(1, x, y - 1);else if (MA[x + 1][y])DFS_RightFirst(2, x + 1, y);else if (MA[x][y + 1])DFS_RightFirst(3, x, y + 1);else if (MA[x - 1][y])DFS_RightFirst(0, x - 1, y);break;}case 3:{if (MA[x + 1][y])DFS_RightFirst(2, x + 1, y);else if (MA[x][y + 1])DFS_RightFirst(3, x, y + 1);else if (MA[x - 1][y])DFS_RightFirst(0, x - 1, y);else if (MA[x][y - 1])DFS_RightFirst(1, x, y - 1);break;}}}void BFS_Short(){bool visit[MM][MM];memset(visit, false, sizeof(visit));queue<St> Qst;Qst.push(START);visit[START.x][START.y] = true;while (!Qst.empty()){ShortStep++;St stemp = Qst.front();Qst.pop();if (stemp.x == END.x&&stemp.y == END.y){cout << stemp.step << endl;return;}int i = stemp.x, j = stemp.y;St Inp;if (MA[i][j - 1] && !visit[i][j - 1]){Inp.x = i; Inp.y = j - 1; Inp.step = stemp.step; Inp.step++;Qst.push(Inp);visit[i][j - 1] = true;}if (MA[i][j + 1] && !visit[i][j + 1]){Inp.x = i; Inp.y = j + 1; Inp.step = stemp.step; Inp.step++;Qst.push(Inp);visit[i][j + 1] = true;}if (MA[i - 1][j] && !visit[i - 1][j]){Inp.x = i - 1; Inp.y = j; Inp.step = stemp.step; Inp.step++;Qst.push(Inp);visit[i - 1][j] = true;}if (MA[i + 1][j] && !visit[i + 1][j]){Inp.x = i + 1; Inp.y = j; Inp.step = stemp.step; Inp.step++;Qst.push(Inp);visit[i + 1][j] = true;}}}int main(){int cases, w, h, direction;cin >> cases;char cc;while (cases--){cin >> w >> h;if ((w < 3 && w>40) || (h < 3 && h>40))continue;LeftStep = 1; RightStep = 1; ShortStep = 0;memset(MA, false, sizeof(MA));for (int i = 1; i <= h; i++)for (int j = 1; j <= w; j++){cin >> cc;if (cc == '.')MA[i][j] = true;if (cc == 'S'){MA[i][j] = true;START.x = i; START.y = j; START.step = 1;if (i == 1){direction = 2;}else if (i == h){direction = 0;}else if (j == 1){direction = 3;}else if (j == w){direction = 1;}}if (cc == 'E'){END.x = i; END.y = j; MA[i][j] = true;}}// DFS_LeftFirst(direction,START.x,START.y);// DFS_RightFirst(direction,START.x,START.y);switch (direction){case 0:{DFS_LeftFirst(direction, START.x - 1, START.y); break; }case 1:{DFS_LeftFirst(direction, START.x, START.y - 1); break; }case 2:{DFS_LeftFirst(direction, START.x + 1, START.y); break; }case 3:{DFS_LeftFirst(direction, START.x, START.y + 1); break; }}switch (direction){case 0:{DFS_RightFirst(direction, START.x - 1, START.y); break; }case 1:{DFS_RightFirst(direction, START.x, START.y - 1); break; }case 2:{DFS_RightFirst(direction, START.x + 1, START.y); break; }case 3:{DFS_RightFirst(direction, START.x, START.y + 1); break; }}BFS_Short();}return 0;}
0 0
- poj3083 dfs + bfs
- bfs+dfs(poj3083)
- [DFS && BFS]poj3083
- POJ3083 -- BFS和DFS
- POJ3083 DFS&BFS
- 基础BFS+DFS poj3083
- poj3083(dfs/bfs混合)
- poj3083 DFS靠墙走+BFS
- POJ3083 DFS+BFS
- poj3083 dfs+bfs一次ac
- poj3083 dfs+bfs搜索迷宫
- poj3083 非常好 的入门级dfs+bfs
- POJ3083《Children of the Candy Corn》方法:DFS+BFS
- POJ3083 Children of the Candy Corn dfs+bfs
- POJ3083 Children of the Candy Corn(BFS+DFS水题)
- POJ3083 Children of the Candy Corn BFS+DFS
- POJ3083 Children of the Candy Corn(DFS + BFS)
- POJ3083 Children of the Candy Corn(BFS+DFS)
- 头文件与库文件的区别与联系
- memcached的配置和spymemcached的使用笔记
- [Algorithm]Maze Prim算法与A*寻路算法(下)
- LeetCode47. Permutations II
- Project文档编辑和权限分配
- POJ3083 DFS&BFS
- nyoj779兰州烧饼
- ADO.NET之command更新数据(视图版)
- hdu 4509
- POJ 3069
- 项目学习的方法与经验
- c#之多态的一个应用
- android数据存储的四种方案(二)
- 蓝桥杯 - 道路和航路(SPFA)