• POJ3083 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


           题目链接:http://poj.org/problem?id=3083

           解法类型:DFS+BFS

           解题思路:这题最短路直用BFS。left和right搜索只是方向问题,用vis数组记录每个点的方向,搜索下一个点时,根据上一个点的方向来分别选择left和right移动方向即可。

           算法实现:

    //STATUS:C++_AC_0MS_188K
    #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;
    }






  • 相关阅读:
    ASP.NET MVC5+EF6+EasyUI 后台管理系统(20)-权限管理系统-根据权限获取菜单
    ASP.NET MVC5+EF6+EasyUI 后台管理系统(19)-权限管理系统-用户登录
    ASP.NET MVC5+EF6+EasyUI 后台管理系统(18)-权限管理系统-表数据
    ASP.NET MVC5+EF6+EasyUI 后台管理系统(17)-LinQ动态排序
    构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(16)-权限管理系统-漂亮的验证码
    构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(15)-权限管理系统准备
    MVC解决Json DataGrid返回的日期格式是/Date(20130450000365)
    Easyui 让DataGrid适应浏览器宽度
    Easyui 让Window弹出居中与最大化后居中
    构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(14)-EasyUI缺陷修复与扩展
  • 原文地址:https://www.cnblogs.com/zhsl/p/2743634.html
Copyright © 2020-2023  润新知