poj 3626 Mud Puddles (简单BFS)
来源:互联网 发布:吉林大学网络教育学费 编辑:程序博客网 时间:2024/05/17 01:11
Description
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.
Input
* 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
Output
* Line 1: The minimum distance that Farmer John has to travel to reach Bessie without stepping in mud.
Sample Input
1 2 70 2-1 33 11 14 2-1 12 2
Sample Output
11
Source
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int x,y,n,vs[1001][1001];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
struct Node{
int x;
int y;
int counter;
};
void bfs()
{
int i;//用于选择4个方向;
queue<Node>Q;//创建一个队列;
Node p,q;
p.x=500;
p.y=500;
p.counter=0;
vs[p.x][p.y]=1;
Q.push(p);//入队;
while(!Q.empty())//当队列不为空的时候循环;
{
p=Q.front();
Q.pop();
for(i=0; i<4; i++)
{
q.x=p.x+dx[i];
q.y=p.y+dy[i];
q.counter=p.counter+1;
if(q.x>=0 && q.x<=1000 && q.y>=0 && q.y<=1000 && vs[q.x][q.y]==0)
{
if(q.x==x && q.y==y)
{
cout<<q.counter<<endl;
return;
}
vs[q.x][q.y]=1;
Q.push(q);
}
}
}
}
int main()
{
int i,c,d;
cin>>x>>y>>n;
memset(vs, 0, sizeof(vs));
x+=500;
y+=500;
for(i=0; i<n; i++)
{
cin>>c>>d;
vs[c+500][d+500]=1;
}
if(x==500 && y==500)
cout<<0<<endl;
else
bfs();
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 3126 简单bfs
- poj 3278 简单bfs
- POJ 2251 BFS 简单
- POJ 3278 简单 BFS
- poj 2251(简单BFS)
- poj 3414(简单bfs)
- POJ 2251 简单BFS
- POJ 3669 简单BFS
- i.mx51 uboot启动感悟
- Node.js学习(12)----Web应用开发
- 跟屌丝大哥学习设计模式--享元模式
- android:ellipsize属性的含义
- PERL中的数组和列表
- poj 3626 Mud Puddles (简单BFS)
- Add Two Numbers
- extjs 4.2 日期控件 选择时分秒功能
- iOS之判断滚动视图的滚动方向
- cocos2d-x + lua 处理触屏事件
- PHP入门 -- 命令行参数
- http://blog.csdn.net/fengbingchun/article/details/6793826
- C++ primer(八)--内联函数 引用变量 函数重载/模板/模板具体化
- RMAN 系列(一)