bzoj 1627: [Usaco2007 Dec]穿越泥地(BFS)
2017-09-04 14:08
281 查看
1627: [Usaco2007 Dec]穿越泥地
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 768 Solved: 517
[Submit][Status][Discuss]
Description
清早6:00,Farmer John就离开了他的屋子,开始了他的例行工作:为贝茜挤奶。前一天晚上,整个农场刚经受过一场瓢泼大雨的洗礼,于是不难想见,FJ 现在面对的是一大片泥泞的土地。FJ的屋子在平面坐标(0, 0)的位置,贝茜所在的牛棚则位于坐标(X,Y) (-500 <= X <= 500; -500 <= Y <= 500)处。当然咯,FJ也看到了地上的所有N(1 <= N <= 10,000)个泥塘,第i个泥塘的坐标为 (A_i, B_i) (-500 <= A_i <= 500;-500 <= B_i <= 500)。每个泥塘都只占据了它所在的那个格子。 Farmer John自然不愿意弄脏他新买的靴子,但他同时想尽快到达贝茜所在的位置。为了数那些讨厌的泥塘,他已经耽搁了一些时间了。如果Farmer John 只能平行于坐标轴移动,并且只在x、y均为整数的坐标处转弯,那么他从屋子门口出发,最少要走多少路才能到贝茜所在的牛棚呢?你可以认为从FJ的屋子到牛棚总是存在至少一条不经过任何泥塘的路径。
Input
* 第1行: 3个用空格隔开的整数:X,Y 和 N* 第2..N+1行: 第i+1行为2个用空格隔开的整数:A_i 和 B_i
Output
* 第1行: 输出1个整数,即FJ在不踏进泥塘的情况下,到达贝茜所在牛棚所需要 走过的最小距离Sample Input
1 2 70 2
-1 3
3 1
1 1
4 2
-1 1
2 2
Sample Output
11水题
因为距离固定为1,BFS到终点就直接弹出
#include<stdio.h> #include<queue> using namespace std; typedef struct { int t; int x, y; }Res; Res now, temp; queue<Res> q; int flag[1015][1015], dir[4][2] = {1,0,-1,0,0,1,0,-1}; int main(void) { int x, y, a, b, n, i; scanf("%d%d%d", &x, &y, &n); while(n--) { scanf("%d%d", &a, &b); flag[a+503][b+503] = 1; } flag[503][503] = 1; now.x = now.y = now.t = 0; q.push(now); while(q.empty()==0) { now = q.front(); q.pop(); if(now.x==x && now.y==y) { while(q.empty()==0) q.pop(); break; } temp.t = now.t+1; for(i=0;i<=3;i++) { temp.x = now.x+dir[i][0]; temp.y = now.y+dir[i][1]; if(flag[temp.x+503][temp.y+503] || temp.x<-502 || temp.x>502 || temp.y<-502 || temp.y>502) continue; flag[temp.x+503][temp.y+503] = 1; q.push(temp); } } printf("%d\n", now.t); return 0; }
相关文章推荐
- bzoj 1627: [Usaco2007 Dec]穿越泥地【bfs】
- 【BZOJ】1627: [Usaco2007 Dec]穿越泥地(bfs)
- BFS-BZOJ-1627-[Usaco2007 Dec]穿越泥地
- BZOJ 1627: [Usaco2007 Dec]穿越泥地( BFS )
- BZOJ_1627_[Usaco2007_Dec]_穿越泥地_(bfs)
- [BZOJ] 1627: [Usaco2007 Dec]穿越泥地
- 【BZOJ 1627】 [Usaco2007 Dec]穿越泥地
- bzoj1627【Usaco2007 Dec】穿越泥池
- BZOJ 1627: [Usaco2007 Dec]穿越泥地
- bzoj1627[Usaco2007 Dec]穿越泥地
- BZOJ 1627: [Usaco2007 Dec]穿越泥地
- bzoj1627[Usaco2007 Dec]穿越泥地
- 1627: [Usaco2007 Dec]穿越泥地 (BFS)
- 1627: [Usaco2007 Dec]穿越泥地
- [Usaco2007 Dec]穿越泥地[bfs][水]
- BFS(当模板用)——[Usaco2007 Dec]穿越泥地
- [bzoj1626][Usaco2007 Dec][最小生成树]修建道路
- 【BZOJ】【1640】【USACO2007 Nov】/【1692】【USACO2007 Dec】队列变换
- BZOJ 1690 【USACO 2007 Dec奶牛观光】
- BZOJ1691: [Usaco2007 Dec]挑剔的美食家