Mud Puddles

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 3642

 

Accepted: 2016

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 (XY) (-500 ≤ X ≤ 500; -500 ≤ Y ≤ 500). He can see all N (1 ≤ N ≤ 10,000) puddles of mud, located at points (AiBi) (-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: XY, 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 7 0 2 -1 3 3 1 1 1 4 2 -1 1 2 2


Sample Output


11


Source

​USACO 2007 December Silver​

 

题意:

n个水坑,从(0,0)到(x,y)的最短距离。

分析:

简单的dfs

这题尽然卡队列,队列必须发在函数体外面(主函数)也行。

 

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
const int maxC=500;
int n,m;
int st,en;
int vis[N][N];
int mapp[N][N];
struct node
{
int x,y,step;
}a,b;
queue<node> q; //必须要移动到外面
int bfs()
{
memset(vis,0,sizeof(vis));

a.x=500;
a.y=500;
a.step=0;

q.push(a);

while(!q.empty())
{
node u=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int x1=u.x+dx[i];
int y1=u.y+dy[i];

if(vis[x1][y1]==0&&mapp[x1][y1]==0)
{
if(x1==st&&y1==en)
{
return u.step+1;
}
vis[x1][y1]=1;

node v;
v.step=u.step+1;
v.x=x1;
v.y=y1;
q.push(v);
}

}
}
}
int main()
{
scanf("%d%d%d",&st,&en,&n);
st+=500;
en+=500;
memset(mapp,0,sizeof(mapp));
for(int i=1;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
x+=500;
y+=500;
mapp[x][y]=-1;
}
printf("%d",bfs());

return 0;
}