poj.3083

这道题同时涉及到深搜和广搜,是很好的搜索入门题,利用广搜快速求最短的查找次数。利用深搜求向左和向右方向的查找次数。

下面是代码:

#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <queue>
#define Max 41
using namespace std;
typedef struct Point{
	int deep;
	int x;
	int y;
}point;
bool vi[Max][Max];
bool trag[Max][Max];
int n,h,w;
int ans,leftans,rightans;
int startx,starty,endx,endy;
void Bfs(){
	memset(vi,0,sizeof(vi));
    vi[startx][starty]=1;
    queue<point> que;
	point p;
	p.deep=1;
	p.x=startx;
	p.y=starty;
	que.push(p);
	while(!que.empty()){
		point q=que.front();
		que.pop();
		if(q.x==endx && q.y==endy){
			ans=q.deep;
			return ;
		}
		point temp;
		if(q.x+1>=1 && q.x+1<=h && trag[q.x+1][q.y] && !vi[q.x+1][q.y]){
			temp.x=q.x+1;
			temp.y=q.y;
			temp.deep=q.deep+1;
			vi[temp.x][temp.y]=1;
			que.push(temp);
		}
		if(q.x-1>=1 && q.x-1<=h && trag[q.x-1][q.y] &&  !vi[q.x-1][q.y]){
			temp.x=q.x-1;
			temp.y=q.y;
			temp.deep=q.deep+1;
			vi[temp.x][temp.y]=1;
			que.push(temp);
		}
		if(q.y+1>=1 && q.y+1<=w && trag[q.x][q.y+1] && !vi[q.x][q.y+1]){
			temp.x=q.x;
			temp.y=q.y+1;
			temp.deep=q.deep+1;
			vi[temp.x][temp.y]=1;
			que.push(temp);
		}
		if(q.y-1>=1 && q.y-1<=w && trag[q.x][q.y-1] && !vi[q.x][q.y-1]){
			temp.x=q.x;
			temp.y=q.y-1;
			temp.deep=q.deep+1;
			vi[temp.x][temp.y]=1;
			que.push(temp);
		}
	}
	
}

void Dfs_left(char dir,int x,int y){
	if(x==endx && y==endy)
		return ;
	switch(dir){
case 'N':
	if(y-1>=1 && y-1<=w && trag[x][y-1]){
		leftans++;
		Dfs_left('W',x,y-1);
	}
	else if(x-1>=1 && x-1<=h && trag[x-1][y]){
		leftans++;
		Dfs_left('N',x-1,y);
	}
	else if(y+1>=1 && y+1<=w && trag[x][y+1]){
		leftans++;
		Dfs_left('E',x,y+1);
	}
	else{
		leftans++;
		Dfs_left('S',x+1,y);
	}
	break;
case 'S':
	if(y+1>=1 && y+1<=w && trag[x][y+1]){
		leftans++;
		Dfs_left('E',x,y+1);
	}
	else if(x+1>=1 && x+1<=h && trag[x+1][y]){
		leftans++;
		Dfs_left('S',x+1,y);
	}
	else if(y-1>=1 && y-1<=w && trag[x][y-1]){
		leftans++;
		Dfs_left('W',x,y-1);
	}
	else{
        leftans++;
	    Dfs_left('N',x-1,y);
	}
	break;
case 'W':
	if(x+1>=1 && x+1<=h && trag[x+1][y]){
		leftans++;
	    Dfs_left('S',x+1,y);
	}
	else if(y-1>=1 && y-1<=w && trag[x][y-1]){
		leftans++;
		Dfs_left('W',x,y-1);
	}
	else if(x-1>=1 && x-1<=h && trag[x-1][y]){
		leftans++;
		Dfs_left('N',x-1,y);
	}
	else{
		leftans++;
	    Dfs_left('E',x,y+1);
	}
	break;
default:
	if(x-1>=1 && x-1<=h && trag[x-1][y]){
		leftans++;
		Dfs_left('N',x-1,y);
	}
	else if(y+1>=1 && y+1<=w && trag[x][y+1]){
		leftans++;
	    Dfs_left('E',x,y+1);
	}
	else if(x+1>=1 && x+1<=h && trag[x+1][y]){
		leftans++;
		Dfs_left('S',x+1,y);
	}
	else{
		leftans++;
		Dfs_left('W',x,y-1);
	}
	}
}
void Dfs_right(char dir,int x,int y){
	if(x==endx && y==endy)
		return ;
	switch(dir){
case 'N':
	if(y+1>=1 && y+1<=w && trag[x][y+1]){
		rightans++;
		Dfs_right('E',x,y+1);
	}
	else if(x-1>=1 && x-1<=h && trag[x-1][y]){
		rightans++;
		Dfs_right('N',x-1,y);
	}
	else if(y-1>=1 && y-1<=w && trag[x][y-1]){
		rightans++;
		Dfs_right('W',x,y-1);
	}
	else{
		rightans++;
		Dfs_right('S',x+1,y);
	}
	break;
case 'S':
	if(y-1>=1 && y-1<=w && trag[x][y-1]){
		rightans++;
		Dfs_right('W',x,y-1);
	}
	else if(x+1>=1 && x+1<=h && trag[x+1][y]){
		rightans++;
		Dfs_right('S',x+1,y);
	}
	else if(y+1>=1 && y+1<=w && trag[x][y+1]){
		rightans++;
		Dfs_right('E',x,y+1);
	}
	else{
        rightans++;
	    Dfs_right('N',x-1,y);
	}
	break;
case 'W':
	if(x-1>=1 && x-1<=h && trag[x-1][y]){
		rightans++;
		Dfs_right('N',x-1,y);
	}
	else if(y-1>=1 && y-1<=w && trag[x][y-1]){
		rightans++;
		Dfs_right('W',x,y-1);
	}
	else if(x+1>=1 && x+1<=h && trag[x+1][y]){
		rightans++;
	    Dfs_right('S',x+1,y);
	}
	else{
		rightans++;
	    Dfs_right('E',x,y+1);
	}
	break;
default:
	if(x+1>=1 && x+1<=h && trag[x+1][y]){
		rightans++;
		Dfs_right('S',x+1,y);
	}
	else if(y+1>=1 && y+1<=w && trag[x][y+1]){
		rightans++;
	    Dfs_right('E',x,y+1);
	}
	else if(x-1>=1 && x-1<=h && trag[x-1][y]){
		rightans++;
		Dfs_right('N',x-1,y);
	}
	else{
		rightans++;
		Dfs_right('W',x,y-1);
	}
	}
}
void Init(){
	scanf("%d%d",&w,&h);
	memset(trag,1,sizeof(trag));
	char temp;
	for(int i=1;i<=h;i++){
	    getchar();
		for(int j=1;j<=w;j++){
			temp=getchar();
			if(temp=='#')
				trag[i][j]=0;
			if(temp=='S'){
				startx=i;
				starty=j;
			}
			if(temp=='E'){
				endx=i;
				endy=j;
			}
		}
	}
}		
int main(){
	scanf("%d",&n);
	while(n--){
	Init();
	Bfs();
	trag[startx][starty]=0;
	char dir;
	if(startx==1)
		dir='S';
	else if(startx==h)
		dir='N';
	else if(starty==1)
		dir='E';
	else if(starty==w)
		dir='W';
	leftans=1;
	Dfs_left(dir,startx,starty);
	rightans=1;
	Dfs_right(dir,startx,starty);
	printf("%d %d %d\n",leftans,rightans,ans);
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值