# [USACO07DEC]泥水坑Mud Puddles BFS BZOJ 1627

## 题目描述

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 7
0 2
-1 3
3 1
1 1
4 2
-1 1
2 2

11
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 400005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-4
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;

inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
}

ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/

int X, Y, N;
struct node {
int x, y;
int stp;
}pt[maxn],cur;
int mp[1001][1001];
int ans = inf;

int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 };

bool OK(int x, int y) {
if (x <= 1000 && x >= 0 && y <= 1000 && y >= 0)return true;
return false;
}
queue<node>q;

void bfs() {
q.push(node{ 500,500,0 });
cur.x = -1; cur.y = -1;
while (!q.empty()) {
cur = q.front(); q.pop();
if (cur.stp > ans)continue;
if (mp[cur.x][cur.y])continue;
if (cur.x == X && cur.y == Y) {
ans = min(ans, cur.stp);
}
mp[cur.x][cur.y] = 1;
for (int i = 0; i < 4; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if (OK(nx, ny) && mp[nx][ny] != 1 && mp[nx][ny] != 2) {
q.push(node{ nx,ny,cur.stp + 1 });
}
}
}
}

int main() {
//	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
X = rd(); Y = rd(); N = rd();
X += 500; Y += 500;
for (int i = 1; i <= N; i++) {
pt[i].x = rd(); pt[i].y = rd();
pt[i].x += 500; pt[i].y += 500;
mp[pt[i].x][pt[i].y] = 2;
}
bfs();
cout << ans << endl;
return 0;
}


posted @ 2019-01-27 21:46 NKDEWSM 阅读( ...) 评论( ...) 编辑 收藏
11-19 50

10-27 150
11-08 52
09-17 24
06-15 54
02-27 1209
08-19 785
08-01 435
01-15 1671
01-13 1869