首页 > 其他 > 详细

bzoj1627 / P2873 [USACO07DEC]泥水坑Mud Puddles

时间:2018-10-27 18:31:12      阅读:146      评论:0      收藏:0      [点我收藏+]

P2873 [USACO07DEC]泥水坑Mud Puddles

bfs入门。

对于坐标为负的情况,我们可以给数组下标加上$abs(min(minx,miny))$转正(根据题意判断)

技术分享图片
 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

 

bzoj1627 / P2873 [USACO07DEC]泥水坑Mud Puddles

原文:https://www.cnblogs.com/kafuuchino/p/9862611.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!