POJ-3083 Children of the Candy Corn 解题报告(搜索) 广搜深搜走迷宫

B - Children of the Candy Corn

Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u
 
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

Hint

 

终于把题目意思看懂了 = =!

左优先:每将行一步时,优先行走它的左方向;右优先:每将行一步时,优先行走它的右方向;

若优先方向行不通时,再按图示方向依次判断;

报告出处:http://blog.csdn.net/angle555945/article/details/7303207
 

#include<stdio.h>
#include<memory.h>
const int MAXN=50;
int L_DFS();
int R_DFS();
int S_BFS();
int w,h,x1,y1,x2,y2,q[MAXN*MAXN],fa[MAXN][MAXN]
,dlx[4]={0,-1,0,1},dly[4]={-1,0,1,0}
,drx[4]={0,-1,0,1},dry[4]={1,0,-1,0};
char map[MAXN][MAXN],vis[MAXN][MAXN]={{0}};
int main()
{
//	freopen("in.txt","r",stdin);
	int t,i,j;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&w,&h);
		for(i=0;i<h;i++){
			scanf("%s",map[i]);
			for(j=0;j<w;j++)
				if(map[i][j]=='S')x1=i,y1=j;
				else if(map[i][j]=='E')x2=i,y2=j;
		}

		printf("%d %d %d\n",L_DFS(),R_DFS(),S_BFS());
	}
	return 0;
}

int L_DFS()  //搜索left
{
	int i,j,x,y,nx,ny,tot;
	for(i=0;i<4;i++){ //寻找起点出口
		nx=x1+dlx[i],ny=y1+dly[i];
		if(nx>=0&&nx<h && ny>=0&&ny<w
			&& map[nx][ny]=='.'){vis[nx][ny]=(i+3)%4;break;}
	}

	for(x=nx,y=ny,tot=2;x!=x2||y!=y2;){
		for(i=vis[x][y],j=0;j<4;j++,i=(i+1)%4){ //i=vis[x][y]保证从左路开始搜
			nx=x+dlx[i],ny=y+dly[i];
			if(map[nx][ny]=='.'||map[nx][ny]=='E'){
				vis[nx][ny]=(i+3)%4; //方向选择i的左方向
				x=nx,y=ny;
				tot++;
				break;
			}
		}
	}
	return tot;
}

int R_DFS()  //搜索right
{
	int i,j,x,y,nx,ny,tot;
	for(i=0;i<4;i++){ //寻找起点出口
		nx=x1+drx[i],ny=y1+dry[i];
		if(nx>=0&&nx<h && ny>=0&&ny<w
			&& map[nx][ny]=='.'){vis[nx][ny]=(i+3)%4;break;}
	}

	for(x=nx,y=ny,tot=2;x!=x2||y!=y2;){  //深度搜索
		for(i=vis[x][y],j=0;j<4;j++,i=(i+1)%4){  //i=vis[x][y]保证从右路开始搜
			nx=x+drx[i],ny=y+dry[i];
			if(map[nx][ny]=='.'||map[nx][ny]=='E'){
				vis[nx][ny]=(i+3)%4; //方向i的右方向
				x=nx,y=ny;
				tot++;
				break;
			}
		}
	}
	return tot;
}

int S_BFS()  //搜索最短路,队列优先
{
	memset(vis,0,sizeof(vis));
	memset(fa,0,sizeof(fa));
	int i,u,x,y,nx,ny,tot,front=0,rear=0;
	q[rear++]=x1*w+y1;  
	vis[x1][y1]=1;
	while(front<rear)
	{
		u=q[front++];
		x=u/w,y=u%w;
		if(x==x2&&y==y2)break;
		for(i=0;i<4;i++){
			nx=x+dlx[i],ny=y+dly[i];
			if(nx>=0&&nx<h && ny>=0&&ny<w && !vis[nx][ny] && (map[nx][ny]=='.'||map[nx][ny]=='E')){
				vis[nx][ny]=1;
				q[rear++]=nx*w+ny;
				fa[nx][ny]=x*w+y;
			}
		}
	}
	for(nx=x2,ny=y2,tot=1;nx!=x1||ny!=y1;tot++){ //计算最短路
		x=fa[nx][ny]/w;
		ny=fa[nx][ny]%w;
		nx=x;
	}
	return tot;
}


 这个是自己的版本,感觉改进了一点。

#include<stdio.h>
#include<memory.h>
const int MAXN=50;
int LR_DFS(int kg);
int BFS();
int w,h,x1,y1,x2,y2,q[MAXN*MAXN],dis[MAXN*MAXN],vis[MAXN*MAXN];
//int fa[MAXN][MAXN],vis[MAXN][MAXN];
int dlx[4]={0,-1,0,1},dly[4]={-1,0,1,0}
,drx[4]={0,-1,0,1},dry[4]={1,0,-1,0};	//dlx[i]=drx[i], dly[i]=dry[(i+2)%4] ;
char map[MAXN][MAXN];
int main()
{
//	freopen("in.txt","r",stdin);
	int t,i,j;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&w,&h);
		for(i=0;i<h;i++){
			scanf("%s",map[i]);
			for(j=0;j<w;j++)
				if(map[i][j]=='S')x1=i,y1=j;
				else if(map[i][j]=='E')x2=i,y2=j;
		}

		printf("%d %d %d\n",LR_DFS(0),LR_DFS(1),BFS());
	}
	return 0;
}

int LR_DFS(int kg)			//设置一个开关控制左搜或右搜;
{
	int i=0,x,y,nx,ny,step;
	x=x1,y=y1,step=1;
	for(;x!=x2||y!=y2;){

		for(;;i=(i+1)%4){ 
			if(kg==0) {nx=x+dlx[i],ny=y+dly[i];}			//i=vis[x][y]保证从左路开始搜
			else if(kg==1) {nx=x+dlx[i],ny=y+dly[(i+2)%4];} //i=vis[x][y]保证从右路开始搜
			if(map[nx][ny]=='.'||map[nx][ny]=='E'){
				i=(i+3)%4;									//方向选择i的左or右方向
				x=nx,y=ny;
				step++;
				break;	}
		}
	}
	return step;
}

int BFS()    
{   
	memset(q,0,sizeof(q));
	memset(dis,0,sizeof(dis));
	memset(vis,0,sizeof(vis));
    int x,y,z,nx,ny,nz;
	int front=0,rear=1;				//树层的前后控制指针;
    z=q[0]=x1*w+y1,vis[z]=1;dis[0]=1; //起点坐标初始化; q[树结点位置] = 一维坐标, dis[树结点位置] = 距离; 
	//printf("%d %d %d\n",x1,y1,z);  
    while(front<rear){				//一层一层遍历树;      
        z=q[front];      
        x=z/w;y=z%w;				//还原二维坐标;    
        for(int i=0;i<4;i++){    
            nx=x+dlx[i];ny=y+dly[i];nz=nx*w+ny;					//新试探坐标,二维坐标一维化 ;      
            if(!vis[nz] && map[nx][ny]=='.'||map[nx][ny]=='E' ) //注意短路运算符;  
            { vis[nz]=1;q[rear]=nz;dis[rear++]=dis[front]+1;	//printf("%d %d\n",nx,ny); 
			  if(map[nx][ny]=='E') {return dis[front]+1;}
			}     
        }							//将结点加入队列,记录结点距离; 
        front++;   
    }    
    return 0; 
}
/*
				0
			 /	|  \
			1	2	3
		 /	|	| \	  \
		4	5	6  7   8
要考虑的问题!
  ①BFS时,防止走过的重新走,需用一个vis数组标记,并且在判断时要避免短路运算符的干扰;
  ②搜索时,找到出口之后不需再找了,通过全局变量ok来控制,特别体现在DFS;
  ③多用printf调试中间过程找到错误原因;
 */


总结:

    0
    / |  \
   1 2 3
   / | | \   \
  4 5 6  7   8

(BFS图)
要考虑的问题!
  ①BFS时,防止走过的重新走,需用一个vis数组标记,并且在判断时要避免短路运算符的干扰;
  ②搜索时,找到出口之后不需再找了,通过全局变量ok来控制,特别体现在DFS;
  ③多用printf调试中间过程找到错误原因;
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值