POJ 3626 Mud Puddles(BFS)【模板】
来源:互联网 发布:微信淘宝客怎么赚钱 编辑:程序博客网 时间:2024/05/22 05:06
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
【题解】 宽搜题,原本第一次看过去以为是图呢,没想到是宽搜,10000个点。。。结果宽搜竟然过了,还很快,这是道宽搜的模板题型,下面附上代码。
【AC代码】
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<queue>using namespace std;const int N=500;int mapp[2005][2005];int p[2005][2005];int dx[4]={0,0,1,-1},dy[4]={-1,1,0,0};int ans;struct tree{ int x,y; int step;};int main(){ int st,ed,n,m,nx,ny; scanf("%d%d%d",&st,&ed,&n); st+=500,ed+=500; tree node,next; for(int i=1;i<=n;++i) { scanf("%d%d",&nx,&ny); nx+=500; ny+=500; mapp[nx][ny]=1; } node.x=500; node.y=500; node.step=0; memset(p,0,sizeof(p)); p[500][500]=1; queue<tree>q; while(!q.empty()) q.pop(); q.push(node); ans=0; while(!q.empty()) { node=q.front(); q.pop(); for(int i=0;i<4;++i) { int xx=node.x+dx[i]; int yy=node.y+dy[i]; if(xx>=0&&xx<=1000&&yy>=0&&yy<=1000&&!p[xx][yy]&&!mapp[xx][yy]) { next.step=node.step+1; next.x=xx; next.y=yy; p[xx][yy]=1; if(p[st][ed]==1) { ans=next.step; goto loop; } q.push(next); } } } loop: printf("%d\n",ans); 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 1915 bfs 模板化
- BFS(模板)
- poj 3626 简单bfs
- Poj 3626 BFS
- POJ 3626 BFS
- POJ-2056(BFS)
- POJ-1198(BFS)
- POJ 3984(bfs)
- POJ 3278(BFS)
- 浏览器缓存
- thinkphp(3.2 很老 但是历史问题没办法升级) 写了个model D()方法提示"Think\\Model:filterParam方法不存在!
- log长度受限,无法完整打印
- 以下程序输出结果是____。
- 获取项目当前路径
- POJ 3626 Mud Puddles(BFS)【模板】
- 计算机网络面试题
- Classes More Than 5 Students
- Spring中指定applicationContext.xml等资源配置文件位置
- 详解Spring Cloud Config
- [LeetCode]167. Two Sum II
- Java中关键字的大致含义
- mybatis基础--作用域和生命周期
- 《javascrip编程精解》第二版习题练习(未完,根据自己学习进度更新)