bzoj1627 / P2873 [USACO07DEC]泥水坑Mud Puddles

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

 

转载于:https://www.cnblogs.com/kafuuchino/p/9862611.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值