Children of the Candy Corn
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 这道题看了好几遍才看懂题目意思,卡在EA上了好久,明知道是数组越界但就是找不到bug,调试的过程就是痛苦啊,但到最后才发现没有给map【】【】初始化(就是上一组测试保留的一些map【】【】值在这一次测试之前还保留着,这样就会影响本次的结果,铭记!) 。大意就是从S点开始最终目标是到达E点,题目要求你从起点开始时始终保持左路优先到达终点的步数,通俗一点就是先往左边走,左边如果是墙,那就选择往前走,如果前面也是墙,那就往右边走,如果右边还是墙那就往后走退回来 |
代码:
#include
#include
#include
#include
using namespace std;
int flag;
int ex,ey,sx,sy;
int fx[]={0,0,-1,1};
int fy[]={1,-1,0,0};
queue
int map[110][110];
int dfsl(int x,int y,int step,int f)
{
if(flag==1) return 0;
if(x == ex && y == ey)
{
flag = 1;
printf("%d ",step);
return 0;
}
if(f== 1)
{
if(map[x][y-1]) dfsl(x,y-1,step+1,4);
if(map[x-1][y]) dfsl(x-1,y,step+1,1);
if(map[x][y+1]) dfsl(x,y+1,step+1,2);
if(map[x+1][y]) dfsl(x+1,y,step+1,3);
}
else if(f == 2)
{
if(map[x-1][y]) dfsl(x-1,y,step+1,1);
if(map[x][y+1]) dfsl(x,y+1,step+1,2);
if(map[x+1][y]) dfsl(x+1,y,step+1,3);
if(map[x][y-1]) dfsl(x,y-1,step+1,4);
}
else if(f == 3)
{
if(map[x][y+1]) dfsl(x,y+1,step+1,2);
if(map[x+1][y]) dfsl(x+1,y,step+1,3);
if(map[x][y-1]) dfsl(x,y-1,step+1,4);
if(map[x-1][y]) dfsl(x-1,y,step+1,1);
}
else
{
if(map[x+1][y]) dfsl(x+1,y,step+1,3);
if(map[x][y-1]) dfsl(x,y-1,step+1,4);
if(map[x-1][y]) dfsl(x-1,y,step+1,1);
if(map[x][y+1]) dfsl(x,y+1,step+1,2);
}
}
int dfsr(int x,int y,int step,int f)
{
if(flag==1) return 0;
if(x==ex&&y==ey)
{
flag=1;
printf("%d ",step);
return 0;
}
if(f==1)
{
if(map[x][y+1]) dfsr(x,y+1,step+1,2);
if(map[x-1][y]) dfsr(x-1,y,step+1,1);
if(map[x][y-1]) dfsr(x,y-1,step+1,4);
if(map[x+1][y]) dfsr(x+1,y,step+1,3);
}
if(f==2)
{
if(map[x+1][y]) dfsr(x+1,y,step+1,3);
if(map[x][y+1]) dfsr(x,y+1,step+1,2);
if(map[x-1][y]) dfsr(x-1,y,step+1,1);
if(map[x][y-1]) dfsr(x,y-1,step+1,4);
}
if(f==3)
{
if(map[x][y-1]) dfsr(x,y-1,step+1,4);
if(map[x+1][y]) dfsr(x+1,y,step+1,3);
if(map[x][y+1]) dfsr(x,y+1,step+1,2);
if(map[x-1][y]) dfsr(x-1,y,step+1,1);
}
if(f==4)
{
if(map[x-1][y]) dfsr(x-1,y,step+1,1);
if(map[x][y-1]) dfsr(x,y-1,step+1,4);
if(map[x+1][y]) dfsr(x+1,y,step+1,3);
if(map[x][y+1]) dfsr(x,y+1,step+1,2);
}
}
int bfs(int x,int y) //简单的广搜过程
{
int vis[110][110];
int step[110][110];
int u,v,i;
memset(vis,0,sizeof(vis));
vis[x][y]=1;
q.push(x);
q.push(y);
step[x][y]=1;
while(!q.empty())
{
x=q.front();
q.pop();
y=q.front();
q.pop();
if(x==ex&&y==ey)
{
printf("%d\n",step[x][y]);
return 0;
}
for(i=0;i<=3;i++)
{
u=x+fx[i];
v=y+fy[i];
if(u<1||v<1||u>40||v>40) continue;
if(map[u][v]==1&&vis[u][v]==0)
{
vis[u][v]=1;
step[u][v]=step[x][y]+1;
q.push(u);
q.push(v);
}
}
}
}
int main()
{
int t;
int m,n,i,j,x,y;
char str1[110][110];
scanf("%d",&t);
while(t--)
{
memset(map,0,sizeof(map)); //这里一定记得初始化map【】【】数组
while(!q.empty())
q.pop();
scanf("%d %d",&m,&n);
for(i=n; i>=1; i--)
{
scanf("%s",str1[i]);
for(j=0; j
if(str1[i][j]=='.') //这里的map【】【】数组保存的数据我是按照二维坐标系的方式
map[j+1][i]=1;
else if(str1[i][j]=='S') //存储数据的
{
sx=j+1;
sy=i;
map[j+1][i]=1;
}
else if(str1[i][j]=='E')
{
ex=j+1;
ey=i;
map[j+1][i]=1;
}
}
}
flag=0;
dfsl(sx,sy,1,1); //深搜时的开始方向是哪边都无所谓
flag=0;
dfsr(sx,sy,1,1);
bfs(sx,sy);
}
return 0;
}