poj Children of the Candy Corn(Bfs + Dfs)
2012-08-10 17:07
405 查看
Children of the Candy Corn
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 6
Problem 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.
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.
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
2 8 8 ######## #......# #.####.# #.####.# #.####.# #.####.# #...#..# #S#E#### 9 5 ######### #.#.#.#.# S.......E #.#.#.#.# #########
Sample Output
37 5 5 17 17 9
Source
PKU
这道题本身并不难,但大部分人的代码写的都很长,主要是题目问了三个问题,(其实没什么意思)
新意在总是朝左或右走,所以需要一个face记录当前面朝哪面
我的代码也很长,感觉虽然写法傻瓜,但思路清晰就行了
#include <stdio.h> #include <string.h> #include <queue> using namespace std; int map[50][50]; bool vist[50][50]; int sx,sy,ex,ey; int flag; int ans,ansl,ansr; struct Node{ int x,y,step; }m[2000]; queue <Node> q; void Dfsl(int x,int y,int step,int face){ if(flag == 1) return; if(x == ex && y == ey){ flag = 1; ansl = step; return; } if(face == 1){ if(map[x+1][y]) Dfsl(x+1,y,step+1,4); if(map[x][y-1]) Dfsl(x,y-1,step+1,1); if(map[x-1][y]) Dfsl(x-1,y,step+1,2); if(map[x][y+1]) Dfsl(x,y+1,step+1,3); } else if(face == 2){ if(map[x][y-1]) Dfsl(x,y-1,step+1,1); if(map[x-1][y]) Dfsl(x-1,y,step+1,2); if(map[x][y+1]) Dfsl(x,y+1,step+1,3); if(map[x+1][y]) Dfsl(x+1,y,step+1,4); } else if(face == 3){ if(map[x-1][y]) Dfsl(x-1,y,step+1,2); if(map[x][y+1]) Dfsl(x,y+1,step+1,3); if(map[x+1][y]) Dfsl(x+1,y,step+1,4); if(map[x][y-1]) Dfsl(x,y-1,step+1,1); } else{ if(map[x][y+1]) Dfsl(x,y+1,step+1,3); if(map[x+1][y]) Dfsl(x+1,y,step+1,4); if(map[x][y-1]) Dfsl(x,y-1,step+1,1); if(map[x-1][y]) Dfsl(x-1,y,step+1,2); } } void Dfsr(int x,int y,int step,int face){ if(flag == 1) return; if(x == ex && y == ey){ flag = 1; ansr = step; return; } if(face == 1){ if(map[x-1][y]) Dfsr(x-1,y,step+1,2); if(map[x][y-1]) Dfsr(x,y-1,step+1,1); if(map[x+1][y]) Dfsr(x+1,y,step+1,4); if(map[x][y+1]) Dfsr(x,y+1,step+1,3); } else if(face == 2){ if(map[x][y+1]) Dfsr(x,y+1,step+1,3); if(map[x-1][y]) Dfsr(x-1,y,step+1,2); if(map[x][y-1]) Dfsr(x,y-1,step+1,1); if(map[x+1][y]) Dfsr(x+1,y,step+1,4); } else if(face == 3){ if(map[x+1][y]) Dfsr(x+1,y,step+1,4); if(map[x][y+1]) Dfsr(x,y+1,step+1,3); if(map[x-1][y]) Dfsr(x-1,y,step+1,2); if(map[x][y-1]) Dfsr(x,y-1,step+1,1); } else{ if(map[x][y-1]) Dfsr(x,y-1,step+1,1); if(map[x+1][y]) Dfsr(x+1,y,step+1,4); if(map[x][y+1]) Dfsr(x,y+1,step+1,3); if(map[x-1][y]) Dfsr(x-1,y,step+1,2); } } void Bfs(){ int x,y,step; int count = 1; memset(vist,0,sizeof(vist)); vist[sx][sy] = 1; m[0].x = sx; m[0].y = sy; m[0].step = 1; q.push(m[0]); while(!q.empty()){ x = q.front().x; y = q.front().y; step = q.front().step; q.pop(); if(x == ex && y == ey){ ans = step; return; } if(!vist[x-1][y] && map[x-1][y]){ vist[x-1][y] = 1; m[count].x = x-1; m[count].y = y; m[count].step = step + 1; q.push(m[count]); count++; } if(!vist[x+1][y] && map[x+1][y]){ vist[x+1][y] = 1; m[count].x = x+1; m[count].y = y; m[count].step = step + 1; q.push(m[count]); count++; } if(!vist[x][y-1] && map[x][y-1]){ vist[x][y-1] = 1; m[count].x = x; m[count].y = y-1; m[count].step = step + 1; q.push(m[count]); count++; } if(!vist[x][y+1] && map[x][y+1]){ vist[x][y+1] = 1; m[count].x = x; m[count].y = y+1; m[count].step = step + 1; q.push(m[count]); count++; } } } int main(){ int t; int w,h; int i,j; char str[50]; scanf("%d",&t); while(t--){ while(!q.empty()) q.pop(); memset(map,0,sizeof(map)); scanf("%d%d",&w,&h); getchar(); for(i = 1;i <= h;i++){ gets(str); for(j = 0;j < w;j++){ if(str[j] == '.') map[i][j+1] = 1; if(str[j] == 'S'){ map[i][j+1] = 1; sx = i; sy = j + 1; } if(str[j] == 'E'){ map[i][j+1] = 1; ex = i; ey = j + 1; } } } flag = 0; Dfsl(sx,sy,1,1); flag = 0; Dfsr(sx,sy,1,1); Bfs(); printf("%d %d %d\n",ansl,ansr,ans); } return 0; }
相关文章推荐
- poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】
- poj Children of the Candy Corn(BFS+DFS)
- POJ 3083 Children of the Candy Corn (DFS + BFS)
- poj 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(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)
- POJ 3083:Children of the Candy Corn(DFS+BFS)
- POJ bfs+dfs之3083-Children of the Candy Corn
- POJ-3083 Children of the Candy Corn (BFS+DFS)
- poj Children of the Candy Corn(BFS+DFS)
- POJ 3083 Children of the Candy Corn (BFS+顺时针逆时针DFS)
- 【POJ】3083- A Children of the Candy Corn DFS、BFS、迷宫靠墙走
- poj 3038 Children of the Candy Corn bfs dfs
- poj 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