Children of the Candy Corn
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11222 | Accepted: 4847 |
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’, 接下来一行每行输入三个数,第一个数表示方向一直向左走,走出迷宫所需要的步数,第二个表示,方向一字向右走,走出迷宫所需要的步数,第三个就是最小的步数; 对于前两个来说,我们可以用两个 DFS 来求出,走出迷宫所需要的步数,第三种情况就需要用到,BFS 了,直接求出来最小的步数即可;
//使用DFS计算左转优先和右转优先的路径,使用BFS计算最短路径
#include<stdio.h>
#include<string.h>
struct node
{
int x,y;
int step;
} que[2500],now,temp; //now保存当前值,temp保存移动之后的值
int dirl[4][2]= {1,0,0,-1,-1,0,0,1}; //左转顺时针 //下 左 上 右 分别对应0,1,2,3
int dirr[4][2]= {1,0,0,1,-1,0,0,-1}; //右转逆时针 //下 右上 左 分别对应0,1,2,3
char maps[50][50];
int visit[50][50]; //用于bfs搜索标记
int flag,countleft,countright;
int h,w; //高度和宽度
int sx,sy; //开始节点的位置
int ex,ey; //结束节点的位置
void dfs_left(int x,int y,int dr) //左优先深度搜索
{
int tx,ty,i;
if(x==ex&&y==ey) //找到结束点
{
flag=1;
return;
}
dr=(dr+3)%4; //以当前方向为上,找到当前方向的左方向
for(i=dr; i<dr+4; i++) //从当前方向的左方向开始,顺时针遍历四个方向
{
tx=x+dirl[i%4][0];
ty=y+dirl[i%4][1];
if(tx>=0&&tx<h&&ty>=0&&ty<w&&maps[tx][ty]!='#')
{
countleft++;
dr=i%4;
dfs_left(tx,ty,dr);
if(flag)
return ;
}
}
}
void dfs_right(int x,int y,int dr) //右优先深度搜索
{
int tx,ty,i;
if(x==ex&&y==ey) //找到结束点
{
flag=1;
return ;
}
dr=(dr+3)%4; //以当前方向为上,找到当前方向的右方向
for(i=dr; i<dr+4; i++) //从当前方向的右方向开始,逆时针遍历四个方向
{
tx=x+dirr[i%4][0];
ty=y+dirr[i%4][1];
if(tx>=0&&tx<h&&ty>=0&&ty<w&&maps[tx][ty]!='#')
{
countright++;
dr=i%4;
dfs_right(tx,ty,dr);
if(flag)
return ;
}
}
}
void bfs() //广度搜索
{
int e=0,s=0,i;
que[s].x=sx;
que[s].y=sy;
que[s++].step=1;
visit[sx][sy]=1;
while(e<s)
{
now=que[e++];
if(maps[now.x][now.y]=='E')
{
printf("%d\n",now.step);
return ;
}
for(i=0; i<4; i++) //搜索四个方向,并记录
{
temp.x=now.x+dirl[i][0];
temp.y=now.y+dirl[i][1];
temp.step=now.step+1;
if(temp.x>=0&&temp.x<h&&temp.y>=0&&temp.y<w&&maps[temp.x][temp.y]!='#'&&!visit[temp.x][temp.y])
{
visit[temp.x][temp.y]=1;
que[s++]=temp;
}
}
}
}
int main()
{
int T,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&w,&h);
for(i=0; i<h; i++)
{
getchar();
for(j=0; j<w; j++)
{
scanf("%c",&maps[i][j]);
if(maps[i][j]=='S') //记录'S'的位置
{
sx=i;
sy=j;
}
if(maps[i][j]=='E') //记录'E'的位置
{
ex=i;
ey=j;
}
}
}
flag=0;
countleft=1; //默认初值为1
dfs_left(sx,sy,1); //默认初始方向为左,
printf("%d ",countleft);
flag=0;
countright=1; //默认初值为1
dfs_right(sx,sy,1); //默认初始方向为右
printf("%d ",countright);
memset(visit,0,sizeof(visit));
bfs();
}
return 0;
}
// 2
// 1 4 3
// 0
//一开始不明白为什么左优先搜索时,找到左方向,然后是顺时针旋转,
//而右优先搜索时,找到右方向,然后是逆时针搜索,
//现在已明白,因为,左优先搜索时,如当前位置为4处,1方向为4的前方,则4是由3处走来的,
//则0为左方向,若是逆时针遍历四个方向,如果0方向不可走,则逆时针旋转,就会走到3方向,
//此时就会直接回去,而忽略了1,2两个方向,右优先搜索同理