poj 3083 Children of the Candy Corn

点击打开链接

Children of the Candy Corn
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8288   Accepted: 3635

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

题目大意就是给一张地图,先输出左转的步数,再输出右转优先的步数,。最后输出最少步数

比较无脑的题,深搜2次广搜一次就0MS AC了,代码挺麻烦的

#include<stdio.h>
#include<string.h>
int step1[4][2] = { {0, -1}, {1, 0}, {0, 1}, {-1, 0} };
int step2[4][2] = { {0, -1}, {-1, 0}, {0, 1}, {1, 0} };
char map[45][45];
int s_x, s_y;
int new_x, new_y;
int dfs_left(int face, int x, int y)
{
if(map[x][y] == 'E')
return 1;
int myface = (face + 1) % 4;
if(map[x + step1[myface][0]][y + step1[myface][1]] != '#')
return dfs_left(myface, x + step1[myface][0], y + step1[myface][1]) + 1;
myface = (myface + 3) % 4;
if(map[x + step1[myface][0]][y + step1[myface][1]] != '#')
return dfs_left(myface, x + step1[myface][0], y + step1[myface][1]) + 1;
myface = (myface + 3) % 4;
if(map[x + step1[myface][0]][y + step1[myface][1]] != '#')
return dfs_left(myface, x + step1[myface][0], y + step1[myface][1]) + 1;
myface = (myface + 3) % 4;
if(map[x + step1[myface][0]][y + step1[myface][1]] != '#')
return dfs_left(myface, x + step1[myface][0], y + step1[myface][1]) + 1; }
int dfs_right(int face, int x, int y)
{
if(map[x][y] == 'E')
return 1;
int myface = (face + 1) % 4;
if(map[x + step2[myface][0]][y + step2[myface][1]] != '#')
return dfs_right(myface, x + step2[myface][0], y + step2[myface][1]) + 1;
myface = (myface + 3) % 4;
if(map[x + step2[myface][0]][y + step2[myface][1]] != '#')
return dfs_right(myface, x + step2[myface][0], y + step2[myface][1]) + 1;
myface = (myface + 3) % 4;
if(map[x + step2[myface][0]][y + step2[myface][1]] != '#')
return dfs_right(myface, x + step2[myface][0], y + step2[myface][1]) + 1;
myface = (myface + 3) % 4;
if(map[x + step2[myface][0]][y + step2[myface][1]] != '#')
return dfs_right(myface, x + step2[myface][0], y + step2[myface][1]) + 1;
}
int bfs()
{
int queue[2000][3];
int top = 0, tail = 0;
queue[tail][0] = s_x;
queue[tail][1] = s_y;
queue[tail][2] = 1;
tail++;
int x, y, st;
while(top < tail)
{
int i;
x = queue[top][0];
y = queue[top][1];
st = queue[top][2];
top++;
for(i = 0; i < 4; i++)
{
if(map[x + step1[i][0]][y + step1[i][1]] != '#')
{
if(map[x + step1[i][0]][y + step1[i][1]] == 'E')
return st + 1;
queue[tail][0] = x + step1[i][0];
queue[tail][1] = y + step1[i][1];
queue[tail][2] = st + 1;
map[x + step1[i][0]][queue[tail][1] = y + step1[i][1]] = '#';
tail ++;
}
}
}
}
int calculateFace()
{
int i;
for(i = 0; i < 4; i++)
{
if(map[s_x + step1[i][0]][s_y + step1[i][1]] == '.' )
{
new_x = s_x + step1[i][0];
new_y = s_y + step1[i][1];
return i;
}
}
}
int calculateFace2()
{
int i;
for(i = 0; i < 4; i++)
{
if(map[s_x + step2[i][0]][s_y + step2[i][1]] == '.' )
{
new_x = s_x + step2[i][0];
new_y = s_y + step2[i][1];
return i;
}
}
}
int main()
{
int n;
scanf("%d", &n);
while(n--)
{
int w, h;
scanf("%d %d", &w, &h);
getchar();
memset(map, '#', sizeof(map));
int i, j;
for(i = 1; i <= h; i++)
{
for(j = 1; j <= w; j++)
{
scanf("%c", &map[i][j]);
if(map[i][j] == 'S')
{
s_x = i;
s_y = j;
}
}
getchar();
}
int face = calculateFace();
printf("%d ", dfs_left(face, new_x, new_y) + 1);
face = calculateFace2();
printf("%d ", dfs_right(face, new_x, new_y) + 1);
printf("%d\n", bfs());
}
return 0;
}
上一篇:FastDFS配置 ***


下一篇:dart字符串处理