POJ 3626 Mud Puddles(bfs)
来源:互联网 发布:iphone自动读书软件 编辑:程序博客网 时间:2024/05/22 04:39
Farmer John is leaving his house promptly at 6 AM for his daily milking of Bessie. However, the previous evening saw a heavy rain, and the fields are quite muddy. FJ starts at the point (0, 0) in the coordinate plane and heads toward Bessie who is located at (X, Y) (-500 ≤ X ≤ 500; -500 ≤ Y ≤ 500). He can see all N (1 ≤ N ≤ 10,000) puddles of mud, located at points (Ai, Bi) (-500 ≤ Ai ≤ 500; -500 ≤ Bi ≤ 500) on the field. Each puddle occupies only the point it is on.
Having just bought new boots, Farmer John absolutely does not want to dirty them by stepping in a puddle, but he also wants to get to Bessie as quickly as possible. He's already late because he had to count all the puddles. If Farmer John can only travel parallel to the axes and turn at points with integer coordinates, what is the shortest distance he must travel to reach Bessie and keep his boots clean? There will always be a path without mud that Farmer John can take to reach Bessie.
* Line 1: Three space-separate integers: X, Y, and N.
* Lines 2..N+1: Line i+1 contains two space-separated integers: Ai and Bi
* Line 1: The minimum distance that Farmer John has to travel to reach Bessie without stepping in mud.
1 2 70 2-1 33 11 14 2-1 12 2
11
题解:
一题很简单的bfs,就是由于横纵坐标有负数,所有坐标加上500就是了
代码:
#include<algorithm>#include<iostream>#include<cstring>#include<stdio.h>#include<math.h>#include<string>#include<stdio.h>#include<queue>#include<stack>#include<map>#include<deque>using namespace std;struct node{ int x,y; int step;};int p[1005][1005];//存图int dirx[4]={0,0,-1,1};//方向数组int diry[4]={1,-1,0,0};queue<node>q;int main(){ int etx,ety,i,j,k,n,m,x,y,s,xx,yy; node now,next; memset(p,0,sizeof(p)); scanf("%d%d%d",&etx,&ety,&n); etx+=500; ety+=500;//由于有负数,加上500 for(i=0;i<n;i++) { scanf("%d%d",&x,&y); x+=500; y+=500; p[x][y]=1; } now.x=500; now.y=500; now.step=0; q.push(now); p[500][500]=1; s=0; while(!q.empty())//日常bfs { now=q.front(); q.pop(); for(i=0;i<4;i++) { xx=now.x+dirx[i]; yy=now.y+diry[i]; if(xx>=0&&yy>=0&&xx<=1000&&yy<=1000&&!p[xx][yy]) { next.step=now.step+1; next.x=xx; next.y=yy; p[xx][yy]=1; if(p[etx][ety]) { s=next.step; goto loop; } q.push(next); } } } loop: printf("%d\n",s); return 0;}
- POJ 3626 Mud Puddles (BFS)
- POJ 3626 Mud Puddles(bfs)
- POJ 3626 Mud Puddles(BFS)【模板】
- POJ 3626 Mud Puddles(超简单BFS)
- poj 3626 Mud Puddles (简单BFS)
- POJ 3626 Mud Puddles
- POJ 3626 Mud Puddles 练习 BFS 宽度优先搜索
- USACO 2007 Dec Silver Mud Puddles bfs or A-star
- poj3626 Mud Puddles
- poj 3626 简单bfs
- Poj 3626 BFS
- POJ 3626 BFS
- POJ-2056(BFS)
- POJ-1198(BFS)
- POJ 3984(bfs)
- POJ 3278(BFS)
- POJ 3126 (BFS)
- POJ 3414 (BFS)
- Hibernate一级缓存管理-evict和clear的用法
- LightOJ 1173 The Vindictive Coach(DP计数)
- 8月10日相约阿里云数据库MySQL金融版线上发布会!
- YII2学习
- 云架构下的性能分析两实例
- POJ 3626 Mud Puddles(bfs)
- Linux(debian)网络配置
- jQuery与js绑定事件的几种写法(以单击事件示例)
- ViewPager fragment android tab选项卡的使用
- 31.Next Permutation 下一个排列
- 集成极光推送之后报错 Manifest merger failed with multiple errors, see logs
- 第四章 基于TCP的服务器端/客户端(1)
- 缓冲区溢出漏洞攻击演示实验(CProxy 6.2缓冲区溢出漏洞)
- 求解集合内相同元素个数的C#解法