poj3626

/*
分析:
    基础广搜。


                               2012-07-30 09:46
*/










#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;


int map[1011][1011];
int flag[1011][1011];
int s_x=500,s_y=500;
int e_x,e_y;
int dir[4][2]={1,0, -1,0, 0,1, 0,-1};


struct node
{
	int x,y;
	int step;
};


int judge(int x,int y)
{
	if(x<0 || y<0 || x>1000 || y>1000)	return 1;
	if(map[x][y]==-1)	return 1;
	if(flag[x][y])		return 1;
	return 0;
}
int BFS()
{
	queue<node>q;
	node cur,next;
	int i;


	cur.x=s_x;
	cur.y=s_y;
	cur.step=0;
	flag[cur.x][cur.y]=1;
	q.push(cur);


	while(!q.empty())
	{
		cur=q.front();
		q.pop();
		if(cur.x==e_x)
		{
			if(cur.y==e_y)
				return cur.step;
		}


		for(i=0;i<4;i++)
		{
			next.x=cur.x+dir[i][0];
			next.y=cur.y+dir[i][1];


			if(judge(next.x,next.y))	continue;
			flag[next.x][next.y]=1;
			next.step=cur.step+1;
			q.push(next);
		}
	}
	return -1;
}


int main()
{
	int a,b;
	int n;


	while(scanf("%d%d%d",&e_x,&e_y,&n)!=-1)
	{
		e_x+=500;
		e_y+=500;
		memset(map,0,sizeof(map));
		memset(flag,0,sizeof(flag));
		while(n--)
		{
			scanf("%d%d",&a,&b);
			a+=500;
			b+=500;
			map[a][b]=-1;
		}


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


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值