# bzoj1627 / P2873 [USACO07DEC]泥水坑Mud Puddles

P2873 [USACO07DEC]泥水坑Mud Puddles

bfs入门。

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<queue>
5 #define re register
6 using namespace std;
7 #define M 502
8 const int d1[4]={0,1,0,-1};
9 const int d2[4]={1,0,-1,0};
10 struct node{int x,y,t;}; queue <node> h;
11 int tx,ty,n; bool vis[M*2+5][M*2+5];
12 int main(){
13     scanf("%d%d%d",&tx,&ty,&n); tx+=M;ty+=M; int q1,q2;
14     for(int i=1;i<=n;++i) scanf("%d%d",&q1,&q2),vis[q1+M][q2+M]=1;
15     h.push((node){M,M,0}); vis[M][M]=1;
16     while(!h.empty()){
17         node u=h.front(); h.pop();
18         for(int i=0;i<4;++i){
19             int r1=u.x+d1[i],r2=u.y+d2[i];
20             if(vis[r1][r2]) continue;
21             if(r1==tx&&r2==ty){
22                 printf("%d",u.t+1);
23                 return 0;
24             }h.push((node){r1,r2,u.t+1}),vis[r1][r2]=1;
25         }
26     }
27 }
View Code

posted @ 2018-10-27 18:15  kafuuchino  阅读(110)  评论(0编辑  收藏  举报