POJ 3083:Children of the Candy Corn
Children of the Candy Corn
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11015 | Accepted: 4744 |
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
2 8 8 ######## #......# #.####.# #.####.# #.####.# #.####.# #...#..# #S#E#### 9 5 ######### #.#.#.#.# S.......E #.#.#.#.# #########
Sample Output
37 5 5 17 17 9
题意是求从S到E的最短路,有意思的是这次还要求两个问题,一个是沿着左手边走的最短路,一个是沿着右手边走的最短路。
仔细思考的话这题不难,就是自己感觉写得比较繁琐,分各种情况和方向考虑,应该可以优化成更简便的代码。
另外这个题被分到深搜里面,我一开始写的也是深搜,但最终结果是广搜更简单一些嘛。
代码:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <cstring> #include <queue> #pragma warning(disable:4996) using namespace std; #define up 1 #define down 2 #define left 3 #define right 4 int row,col; char value[50][50]; int bushu[50][50]; int dfs_left(int i,int j,int dir) { memset(bushu,0,sizeof(bushu)); queue<int>x; queue<int>y; queue<int>z; x.push(i); y.push(j); z.push(dir); int temp1,temp2,temp3; while(x.size()) { i=x.front(); j=y.front(); temp3=z.front(); x.pop(); y.pop(); z.pop(); if(temp3==up) { if(value[i][j-1]=='E') { return bushu[i][j]+1; } if(value[i][j-1]=='.') { x.push(i); y.push(j-1); z.push(left); bushu[i][j-1]=bushu[i][j]+1; } else if(value[i][j-1]=='#') { if(value[i-1][j]=='E') { return bushu[i][j]+1; } if(value[i-1][j]=='.') { x.push(i-1); y.push(j); z.push(up); bushu[i-1][j]=bushu[i][j]+1; } else if(value[i-1][j]=='#') { if(value[i][j+1]=='E') return bushu[i][j]+1; if(value[i][j+1]=='.') { x.push(i); y.push(j+1); z.push(right); bushu[i][j+1]=bushu[i][j]+1; } else if(value[i][j+1]=='#') { if(value[i+1][j]=='E') return bushu[i][j]+1; if(value[i+1][j]=='.') { x.push(i+1); y.push(j); z.push(down); bushu[i+1][j]=bushu[i][j]+1; } } } } } else if(temp3==down) { if(value[i][j+1]=='E') { return bushu[i][j]+1; } if(value[i][j+1]=='.') { x.push(i); y.push(j+1); z.push(right); bushu[i][j+1]=bushu[i][j]+1; } else if(value[i][j+1]=='#') { if(value[i+1][j]=='E') { return bushu[i][j]+1; } if(value[i+1][j]=='.') { x.push(i+1); y.push(j); z.push(down); bushu[i+1][j]=bushu[i][j]+1; } else if(value[i+1][j]=='#') { if(value[i][j-1]=='E') return bushu[i][j]+1; if(value[i][j-1]=='.') { x.push(i); y.push(j-1); z.push(left); bushu[i][j-1]=bushu[i][j]+1; } else if(value[i][j-1]=='#') { if(value[i-1][j]=='E') return bushu[i][j]+1; if(value[i-1][j]=='.') { x.push(i-1); y.push(j); z.push(up); bushu[i-1][j]=bushu[i][j]+1; } } } } } else if(temp3==left) { if(value[i+1][j]=='E') { return bushu[i][j]+1; } if(value[i+1][j]=='.') { x.push(i+1); y.push(j); z.push(down); bushu[i+1][j]=bushu[i][j]+1; } else if(value[i+1][j]=='#') { if(value[i][j-1]=='E') { return bushu[i][j]+1; } if(value[i][j-1]=='.') { x.push(i); y.push(j-1); z.push(left); bushu[i][j-1]=bushu[i][j]+1; } else if(value[i][j-1]=='#') { if(value[i-1][j]=='E') return bushu[i][j]+1; if(value[i-1][j]=='.') { x.push(i-1); y.push(j); z.push(up); bushu[i-1][j]=bushu[i][j]+1; } else if(value[i-1][j]=='#') { if(value[i][j+1]=='E') return bushu[i][j]+1; if(value[i][j+1]=='.') { x.push(i); y.push(j+1); z.push(right); bushu[i][j+1]=bushu[i][j]+1; } } } } } else if(temp3==right) { if(value[i-1][j]=='E') { return bushu[i][j]+1; } if(value[i-1][j]=='.') { x.push(i-1); y.push(j); z.push(up); bushu[i-1][j]=bushu[i][j]+1; } else if(value[i-1][j]=='#') { if(value[i][j+1]=='E') { return bushu[i][j]+1; } if(value[i][j+1]=='.') { x.push(i); y.push(j+1); z.push(right); bushu[i][j+1]=bushu[i][j]+1; } else if(value[i][j+1]=='#') { if(value[i+1][j]=='E') return bushu[i][j]+1; if(value[i+1][j]=='.') { x.push(i+1); y.push(j); z.push(down); bushu[i+1][j]=bushu[i][j]+1; } else if(value[i+1][j]=='#') { if(value[i][j-1]=='E') return bushu[i][j]+1; if(value[i][j-1]=='.') { x.push(i); y.push(j-1); z.push(left); bushu[i][j-1]=bushu[i][j]+1; } } } } } } return 0; } int dfs_right(int i,int j,int dir) { memset(bushu,0,sizeof(bushu)); queue<int>x; queue<int>y; queue<int>z; x.push(i); y.push(j); z.push(dir); int temp1,temp2,temp3; while(x.size()) { i=x.front(); j=y.front(); temp3=z.front(); x.pop(); y.pop(); z.pop(); if(temp3==up) { if(value[i][j+1]=='E') { return bushu[i][j]+1; } if(value[i][j+1]=='.') { x.push(i); y.push(j+1); z.push(right); bushu[i][j+1]=bushu[i][j]+1; } else if(value[i][j+1]=='#') { if(value[i-1][j]=='E') { return bushu[i][j]+1; } if(value[i-1][j]=='.') { x.push(i-1); y.push(j); z.push(up); bushu[i-1][j]=bushu[i][j]+1; } else if(value[i-1][j]=='#') { if(value[i][j-1]=='E') return bushu[i][j]+1; if(value[i][j-1]=='.') { x.push(i); y.push(j-1); z.push(left); bushu[i][j-1]=bushu[i][j]+1; } else if(value[i][j-1]=='#') { if(value[i+1][j]=='E') return bushu[i][j]+1; if(value[i+1][j]=='.') { x.push(i+1); y.push(j); z.push(down); bushu[i+1][j]=bushu[i][j]+1; } } } } } else if(temp3==down) { if(value[i][j-1]=='E') { return bushu[i][j]+1; } if(value[i][j-1]=='.') { x.push(i); y.push(j-1); z.push(left); bushu[i][j-1]=bushu[i][j]+1; } else if(value[i][j-1]=='#') { if(value[i+1][j]=='E') { return bushu[i][j]+1; } if(value[i+1][j]=='.') { x.push(i+1); y.push(j); z.push(down); bushu[i+1][j]=bushu[i][j]+1; } else if(value[i+1][j]=='#') { if(value[i][j+1]=='E') return bushu[i][j]+1; if(value[i][j+1]=='.') { x.push(i); y.push(j+1); z.push(right); bushu[i][j+1]=bushu[i][j]+1; } else if(value[i][j+1]=='#') { if(value[i-1][j]=='E') return bushu[i][j]+1; if(value[i-1][j]=='.') { x.push(i-1); y.push(j); z.push(up); bushu[i-1][j]=bushu[i][j]+1; } } } } } else if(temp3==left) { if(value[i-1][j]=='E') { return bushu[i][j]+1; } if(value[i-1][j]=='.') { x.push(i-1); y.push(j); z.push(up); bushu[i-1][j]=bushu[i][j]+1; } else if(value[i-1][j]=='#') { if(value[i][j-1]=='E') { return bushu[i][j]+1; } if(value[i][j-1]=='.') { x.push(i); y.push(j-1); z.push(left); bushu[i][j-1]=bushu[i][j]+1; } else if(value[i][j-1]=='#') { if(value[i+1][j]=='E') return bushu[i][j]+1; if(value[i+1][j]=='.') { x.push(i+1); y.push(j); z.push(down); bushu[i+1][j]=bushu[i][j]+1; } else if(value[i+1][j]=='#') { if(value[i][j+1]=='E') return bushu[i][j]+1; if(value[i][j+1]=='.') { x.push(i); y.push(j+1); z.push(right); bushu[i][j+1]=bushu[i][j]+1; } } } } } else if(temp3==right) { if(value[i+1][j]=='E') { return bushu[i][j]+1; } if(value[i+1][j]=='.') { x.push(i+1); y.push(j); z.push(down); bushu[i+1][j]=bushu[i][j]+1; } else if(value[i+1][j]=='#') { if(value[i][j+1]=='E') { return bushu[i][j]+1; } if(value[i][j+1]=='.') { x.push(i); y.push(j+1); z.push(right); bushu[i][j+1]=bushu[i][j]+1; } else if(value[i][j+1]=='#') { if(value[i-1][j]=='E') return bushu[i][j]+1; if(value[i-1][j]=='.') { x.push(i-1); y.push(j); z.push(up); bushu[i-1][j]=bushu[i][j]+1; } else if(value[i-1][j]=='#') { if(value[i][j-1]=='E') return bushu[i][j]+1; if(value[i][j-1]=='.') { x.push(i); y.push(j-1); z.push(left); bushu[i][j-1]=bushu[i][j]+1; } } } } } } return 0; } int dfs(int i,int j,int dir) { memset(bushu,0,sizeof(bushu)); queue<int>x; queue<int>y; x.push(i); y.push(j); while(x.size()) { i=x.front(); j=y.front(); x.pop(); y.pop(); if(value[i+1][j]=='.'&&!bushu[i+1][j]) { x.push(i+1); y.push(j); bushu[i+1][j]=bushu[i][j]+1; } if(value[i][j+1]=='.'&&!bushu[i][j+1]) { x.push(i); y.push(j+1); bushu[i][j+1]=bushu[i][j]+1; } if(value[i-1][j]=='.'&&!bushu[i-1][j]) { x.push(i-1); y.push(j); bushu[i-1][j]=bushu[i][j]+1; } if(value[i][j-1]=='.'&&!bushu[i][j-1]) { x.push(i); y.push(j-1); bushu[i][j-1]=bushu[i][j]+1; } if(value[i+1][j]=='E'||value[i][j+1]=='E'||value[i-1][j]=='E'||value[i][j-1]=='E') return bushu[i][j]+1; } return 0; } void solve() { int i,j; for(i=2;i<col;i++) { if(value[1][i]=='S') { cout<<dfs_left(1,i,down)+1<<" "; cout<<dfs_right(1,i,down)+1<<" "; cout<<dfs(1,i,down)+1<<endl; return; } } for(i=2;i<col;i++) { if(value[row][i]=='S') { cout<<dfs_left(row,i,up)+1<<" "; cout<<dfs_right(row,i,up)+1<<" "; cout<<dfs(row,i,up)+1<<endl; return; } } for(i=2;i<row;i++) { if(value[i][1]=='S') { cout<<dfs_left(i,1,right)+1<<" "; cout<<dfs_right(i,1,right)+1<<" "; cout<<dfs(i,1,right)+1<<endl; return; } } for(i=2;i<row;i++) { if(value[i][col]=='S') { cout<<dfs_left(i,col,left)+1<<" "; cout<<dfs_right(i,col,left)+1<<" "; cout<<dfs(i,col,left)+1<<endl; return; } } } int main() { int Test,i,j; scanf("%d",&Test); while(Test--) { scanf("%d%d",&col,&row); for(i=1;i<=row;i++) { scanf("%s",value[i]+1); } solve(); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。