POJ 3626 Mud Puddles(超简单BFS)

水题,直接BFS,什么都不说了

#include<iostream>
#include<cstring>
using namespace std;
#define N 1010
int map[N][N];
const int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int ex,ey;

class node
{
public:
	int x,y,step;
	void in(int _x,int _y,int _step)
	{
		x=_x;
		y=_y;
		step=_step;
	}
}q[N*N],temp;

int bfs()
{
	int head=0,tail=1;
	temp.in(500,500,0);
	q[head]=temp;
	if( ex==500 && ey==500)
		return 0;
	while(head<tail)
	{
		temp = q[head++];
		for(int i=0;i<4;i++)
		{
			int x = temp.x + dir[i][0];
			int y = temp.y + dir[i][1];
			int step = temp.step + 1;
			if( x == ex && y == ey )
				return step;
			if( map[x][y] || x<0 || y<0 || x>1000 || y>1000)
				continue;
			map[x][y]=1;
			q[tail++].in(x,y,step);
		}
	}
	return 0;
}

int main()
{
	int i,j,u,v,n;
//	freopen("e://in.txt","r",stdin);
	while(cin>>ex>>ey>>n)
	{
		ex+=500;
		ey+=500;
		memset(map,0,sizeof(map));
		for(i=0;i<n;i++)
		{
			cin>>u>>v;
			map[u+500][v+500]=1;
		}
		cout<<(bfs())<<endl;
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值