POJ 3626 Mud Puddles (BFS)

来源:互联网 发布:个性化推荐算法 编辑:程序博客网 时间:2024/05/21 19:08

点击打开链接

将所有的数据都+500,然后模版。。

#include"stdio.h"#include"string.h"#include"queue"using namespace std;int d[4][2]={1,0,0,1,-1,0,0,-1};int x,y,n,map[1001][1001];struct node{int x,y,step;};void bfs(){int i;queue<node>Q;node q,p;p.x=500;p.y=500;p.step=0;Q.push(p);map[p.x][p.y]=1;while(!Q.empty()){p=Q.front();Q.pop();for(i=0;i<4;i++){q.x=p.x+d[i][0];q.y=p.y+d[i][1];q.step=p.step+1;if(q.x>=0&&q.x<=1000&&q.y>=0&&q.y<=1000&&map[q.x][q.y]==0){if(q.x==x&&q.y==y){printf("%d\n",q.step);return ;}map[q.x][q.y]=1;Q.push(q);}}}}int main(){int i,a,b;while(scanf("%d%d%d",&x,&y,&n)!=-1){x+=500;y+=500;memset(map,0,sizeof(map));for(i=0;i<n;i++){scanf("%d%d",&a,&b);map[a+500][b+500]=1;}if(x==500&&y==500)printf("0\n");else bfs();}return 0;}


原创粉丝点击