POJ 3083 - Children of the Candy Corn(BFS+DFS)
来源:互联网 发布:堆排序算法实现 编辑:程序博客网 时间:2024/06/10 05:31
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
-----------------------------------------------------
题意:
左转优先,输出路径; 右转优先,输出路径;输出最短路径。
思路:
最短路径使用BFS,
前两个使用DFS,因为有可能走过的路还会再走。要根据当前的位置决定下一步的方向。
使用queue之前记得清空。
CODE:
#include <iostream>#include <cstdio>#include <queue>#include <algorithm>#include <cstring>using namespace std;const int INF = 0xfffffff;char maze[50][50];int dis[50][50];int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};int dl[] = {3,0,1,2}, dr[] = {1,0,3,2};int w, h, sx, sy, fx, fy, cnt;struct node { int x, y; };queue<node> que;bool textleft(int x, int y){ if(x > 0 && x <= h && y > 0 && y <= w && maze[x][y] != '#')return true; return false;}void dfs_left(int d, int x, int y){ cnt++; if(x == fx && y == fy) { printf("%d ", cnt); return; } for(int i = 0; i < 4; ++i) { int ndes = (d + dl[i]) % 4; int nx = x + dx[ndes], ny = y + dy[ndes]; if(textleft(nx, ny)) { dfs_left(ndes, nx, ny); return; } }}bool textright(int x, int y){ if(x > 0 && x <= h && y > 0 && y <= w && maze[x][y] != '#') return true; return false;}void dfs_right(int d, int x, int y){ cnt++; if(x == fx && y == fy) { printf("%d ", cnt); return; } for(int i = 0; i < 4; ++i) { int ndes = (d + dr[i]) % 4; int nx = x + dx[ndes], ny = y + dy[ndes]; if(textright(nx, ny)) { dfs_right(ndes, nx, ny); return; } }}bool text(int x, int y){ if(x > 0 && x <= h && y > 0 && y <= w && maze[x][y] != '#' && dis[x][y] == -1) return true; return false;}void bfs(){ while(que.size()) que.pop(); memset(dis, -1, sizeof(dis)); node now, tmp; tmp.x = sx; tmp.y = sy; dis[sx][sy] = 1; que.push(tmp); while(que.size()) { now = que.front(); que.pop(); if(now.x == fx && now.y == fy) { printf("%d\n", dis[fx][fy]); return; } for(int i = 0; i < 4; ++i) { int nx = now.x + dx[i], ny = now.y + dy[i]; if(text(nx, ny)) { tmp.x = nx; tmp.y = ny; que.push(tmp); dis[nx][ny] = dis[now.x][now.y] + 1; } } }}int main(){//freopen("in", "r", stdin); int T; scanf("%d", &T); while(T--) { memset(maze, 0, sizeof(maze)); scanf("%d %d", &w, &h); for(int i = 1; i <= h; ++i) { scanf("%s", maze[i]+1); for(int j = 1; j <= w; ++j) { if(maze[i][j] == 'S') { sx = i; sy = j; } if(maze[i][j] == 'E') { fx = i; fy = j; } } } int des; if(sx == 1) des = 0; if(sx == h) des = 2; if(sy == 1) des = 3; if(sy == w) des = 1; cnt = 0; dfs_left(des, sx, sy); cnt = 0; dfs_right(des, sx, sy); bfs(); } return 0;}
0 0
- POJ 3083 Children of the Candy Corn (bfs+dfs)
- POJ 3083:Children of the Candy Corn(DFS+BFS)
- POJ 3083 - Children of the Candy Corn(BFS+DFS)
- POJ 3083 Children of the Candy Corn(bfs+dfs)
- POJ 3083 Children of the Candy Corn(BFS + DFS)
- POJ 3083 Children of the Candy Corn (DFS+BFS)
- POJ 3083Children of the Candy Corn(BFS + DFS)
- POJ 3083 Children of the Candy Corn(DFS+BFS)
- POJ 3083 Children of the Candy Corn (BFS+DFS)
- POJ 3083 Children of the Candy Corn dfs+bfs
- poj 3083 Children of the Candy Corn(dfs+bfs)
- POJ 3083Children of the Candy Corn(DFS*2+BFS)
- POJ 3083 Children of the Candy Corn (DFS + BFS)
- POJ 3083-Children of the Candy Corn(dfs+bfs)
- poj 3083 Children of the Candy Corn(bfs+dfs)
- poj 3083 Children of the Candy Corn (DFS+BFS)
- poj 3083 Children of the Candy Corn dfs+bfs
- poj-3083 Children of the Candy Corn-DFS+BFS
- 黑马程序员——类加载器、泛型、代理
- 2改变WordPress的主题
- 写程序引发的思考
- CentOS/Linux 重启网卡 活跃连接路径/network configure error错误解决
- 苦逼经历(二)
- POJ 3083 - Children of the Candy Corn(BFS+DFS)
- POJ 1811 Prime Test (miller_rabin + pollard_rho)
- 相应式布局
- php-设计模式-外观模式
- ER图,数据库模型从这里开始
- Hadoop计算中的Shuffle过程
- Arm-linux下QT使用键盘
- 我喜欢的编程工具
- Windows上编译最新的CURL,含有zlib,openssl