poj3083 非常好 的入门级dfs+bfs

                                                                                                                    poj3083 Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8505 Accepted: 3728

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
思路:
本题思路比较清洗,第一按照左边有限的方式深搜,并计算步伐数,第二按照右边优先的方式深搜,并计算步伐数(注意:这种左边优先和右边优先的搜索方式得到的平不一定
最短的路径),第三:输出最短路径,毫不犹豫的选择了bfs.

不过怎样确定左边优先和右边优先的搜索顺序呢?

规定从上到下为 0 2 3 1,
以左边有限为例,则从3到1,左边优先搜索的顺序依次为3 2 1 0
从1到3 ,左优的顺序为2 3 0 1  ,从0到2 ,则为3 2 1 0,从2到0 ,则1 0 3 2
同理可得右边优先搜索。
程序中定义方向为:Ldir[4][2]={{-1,0},{0,-1},{1,0},{0,1}},Ldir[0][]为向上方向(从2到0的方向),Ldir[1][]为向左,Ldir[2][]为向下,Ldir[3][]向左

写法一(写的有点丑):
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
const int maxn=2000+5;
const int Ldir[4][2]={{-1,0},{0,-1},{1,0},{0,1}}; //方向
const int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
struct Node{
	int x,y;
	Node(int x=0,int y=0):x(x),y(y){} //点的构造函数
};
Node que[maxn],st,ed;
int dist[maxn],vis[45][45];
int G[45][45];
int n,m,d,lStep,rStep;  //lStep,rStep分别为左边优先和右边优先
void dfs1(int x,int y,int d)
{
	lStep++;
	if(x==ed.x&&y==ed.y){
		return;
	}
	switch(d)
	{
		case 0:{
			if(G[x+Ldir[1][0]][y+Ldir[1][1]])
				dfs1(x+Ldir[1][0],y+Ldir[1][1],1);
			else if(G[x+Ldir[0][0]][y+Ldir[0][1]])
				dfs1(x+Ldir[0][0],y+Ldir[0][1],0);
			else if(G[x+Ldir[3][0]][y+Ldir[3][1]])
				dfs1(x+Ldir[3][0],y+Ldir[3][1],3);
			else if(G[x+Ldir[2][0]][y+Ldir[2][1]])
				dfs1(x+Ldir[2][0],y+Ldir[2][1],2);
            break;
		}
		case 1:{
			if(G[x+Ldir[2][0]][y+Ldir[2][1]])
				dfs1(x+Ldir[2][0],y+Ldir[2][1],2);
			else if(G[x+Ldir[1][0]][y+Ldir[1][1]])
				dfs1(x+Ldir[1][0],y+Ldir[1][1],1);
			else if(G[x+Ldir[0][0]][y+Ldir[0][1]])
				dfs1(x+Ldir[0][0],y+Ldir[0][1],0);
			else if(G[x+Ldir[3][0]][y+Ldir[3][1]])
				dfs1(x+Ldir[3][0],y+Ldir[3][1],3);
            break;
		}
		case 2:{
			if(G[x+Ldir[3][0]][y+Ldir[3][1]])
				dfs1(x+Ldir[3][0],y+Ldir[3][1],3);
			else if(G[x+Ldir[2][0]][y+Ldir[2][1]])
				dfs1(x+Ldir[2][0],y+Ldir[2][1],2);
			else if(G[x+Ldir[1][0]][y+Ldir[1][1]])
				dfs1(x+Ldir[1][0],y+Ldir[1][1],1);
			else if(G[x+Ldir[0][0]][y+Ldir[0][1]])
				dfs1(x+Ldir[0][0],y+Ldir[0][1],0);
            break;
		}
		case 3:{
			if(G[x+Ldir[0][0]][y+Ldir[0][1]])
				dfs1(x+Ldir[0][0],y+Ldir[0][1],0);
			else if(G[x+Ldir[3][0]][y+Ldir[3][1]])
				dfs1(x+Ldir[3][0],y+Ldir[3][1],3);
			else if(G[x+Ldir[2][0]][y+Ldir[2][1]])
				dfs1(x+Ldir[2][0],y+Ldir[2][1],2);
			else if(G[x+Ldir[1][0]][y+Ldir[1][1]])
				dfs1(x+Ldir[1][0],y+Ldir[1][1],1);
            break;
		}
	}
}
void dfs2(int x,int y,int d)
{
    //cout<<x<<" "<<y<<endl;
    rStep++;
	if(x==ed.x&&y==ed.y){
		return;
	}
	switch(d)
	{
		case 0:{
			if(G[x+Ldir[3][0]][y+Ldir[3][1]])
				dfs2(x+Ldir[3][0],y+Ldir[3][1],3);
			else if(G[x+Ldir[0][0]][y+Ldir[0][1]])
				dfs2(x+Ldir[0][0],y+Ldir[0][1],0);
			else if(G[x+Ldir[1][0]][y+Ldir[1][1]])
				dfs2(x+Ldir[1][0],y+Ldir[1][1],1);
			else if(G[x+Ldir[2][0]][y+Ldir[2][1]])
				dfs2(x+Ldir[2][0],y+Ldir[2][1],2);
            break;
		}
		case 1:{
			if(G[x+Ldir[0][0]][y+Ldir[0][1]])
				dfs2(x+Ldir[0][0],y+Ldir[0][1],0);
			else if(G[x+Ldir[1][0]][y+Ldir[1][1]])
				dfs2(x+Ldir[1][0],y+Ldir[1][1],1);
			else if(G[x+Ldir[2][0]][y+Ldir[2][1]])
				dfs2(x+Ldir[2][0],y+Ldir[2][1],2);
			else if(G[x+Ldir[3][0]][y+Ldir[3][1]])
				dfs2(x+Ldir[3][0],y+Ldir[3][1],3);
            break;
		}
		case 2:{
			if(G[x+Ldir[1][0]][y+Ldir[1][1]])
				dfs2(x+Ldir[1][0],y+Ldir[1][1],1);
			else if(G[x+Ldir[2][0]][y+Ldir[2][1]])
				dfs2(x+Ldir[2][0],y+Ldir[2][1],2);
			else if(G[x+Ldir[3][0]][y+Ldir[3][1]])
				dfs2(x+Ldir[3][0],y+Ldir[3][1],3);
			else if(G[x+Ldir[0][0]][y+Ldir[0][1]])
				dfs2(x+Ldir[0][0],y+Ldir[0][1],0);
            break;
		}
		case 3:{
			if(G[x+Ldir[2][0]][y+Ldir[2][1]])
				dfs2(x+Ldir[2][0],y+Ldir[2][1],2);
			else if(G[x+Ldir[3][0]][y+Ldir[3][1]])
				dfs2(x+Ldir[3][0],y+Ldir[3][1],3);
			else if(G[x+Ldir[0][0]][y+Ldir[0][1]])
				dfs2(x+Ldir[0][0],y+Ldir[0][1],0);
			else if(G[x+Ldir[1][0]][y+Ldir[1][1]])
				dfs2(x+Ldir[1][0],y+Ldir[1][1],1);
            break;
		}
	}
}
void bfs(Node st,Node ed)
{
    int head=0,tail=0;
    dist[st.x*n+st.y]=1;
    que[head]=st;
    vis[st.x][st.y]=1;
    while(head<=tail){
        Node tmp=que[head++];
        if(tmp.x==ed.x&&tmp.y==ed.y){
            return;
        }
        for(int i=0;i<4;i++){
            int nowX=tmp.x+dir[i][0];
            int nowY=tmp.y+dir[i][1];
            if(!vis[nowX][nowY]&&G[nowX][nowY]){
                vis[nowX][nowY]=1;
                que[++tail]=Node(nowX,nowY);
                dist[nowX*n+nowY]=dist[tmp.x*n+tmp.y]+1;
            }
        }
    }
}
int main()
{
	//freopen("in.txt","r",stdin);
	char str[50];
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&m,&n);
		memset(dist,0,sizeof(dist));
		memset(vis,0,sizeof(vis));
		memset(G,0,sizeof(G));
		for(int i=1;i<=n;i++){
			scanf("%s",&str[1]);
			for(int j=1;j<=m;j++){
				if(str[j]=='.')	G[i][j]=1;
				else if(str[j]=='S'){
					if(i==1)	d=2;
					else if(i==n)	d=0;
					else if(j==1)	d=3;
					else if(j==m)	d=1;
					G[i][j]=1;
					st.x=i,st.y=j;
				}else if(str[j]=='E'){
					G[i][j]=1;
					ed.x=i,ed.y=j;
				}
			}
		}
		lStep=rStep=1;
		switch(d){
            case 0: dfs1(st.x+Ldir[0][0],st.y+Ldir[0][1],d);break;
            case 1: dfs1(st.x+Ldir[1][0],st.y+Ldir[1][1],d);break;
            case 2: dfs1(st.x+Ldir[2][0],st.y+Ldir[2][1],d);break;
            case 3: dfs1(st.x+Ldir[3][0],st.y+Ldir[3][1],d);break;
		}
		switch(d){
            case 0: dfs2(st.x+Ldir[0][0],st.y+Ldir[0][1],d);break;
            case 1: dfs2(st.x+Ldir[1][0],st.y+Ldir[1][1],d);break;
            case 2: dfs2(st.x+Ldir[2][0],st.y+Ldir[2][1],d);break;
            case 3: dfs2(st.x+Ldir[3][0],st.y+Ldir[3][1],d);break;
		}
		bfs(st,ed);
		printf("%d %d %d\n",lStep,rStep,dist[ed.x*n+ed.y]);
	}
    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值