穿越泥地(mud) (BFS)
问题 C: 穿越泥地(mud)
时间限制: 1 Sec 内存限制: 128 MB
提交: 16 解决: 10
[提交][状态][讨论版]
题目描述
清早6:00,FJ就离开了他的屋子,开始了他的例行工作:为贝茜挤奶。前一天晚上,整个农场刚经受过一场瓢泼大雨的洗礼,于是不难想象,FJ现在面对的
是一大片泥泞的土地。FJ的屋子在平面坐标(0,0)的位置j贝茜所在的牛棚则位于坐标(x,y) (-500≤x≤500;
-500≤Y≤500)处。当然,FJ也看到了地上的所有N(1≤N≤10000)个泥塘,第i个泥塘的坐标为(A_i,B_i)
(-500≤A_i≤500:-500≤B_i≤500)。每个泥塘都只占据了它所在的那个格子。
FJ自然不愿意弄脏他新买的靴子,但他同时想尽快到达贝茜所在的位置。为了数那些讨厌的泥塘,他已经耽搁了一些时间了。如果FJ只能平行于坐标轴移动,并
且只在x、y均为整数的坐标处转弯,童那么他从屋子门口出发,最少要走多少路才能到贝茜所在的牛棚呢?你可以认为从FJ的屋子到牛棚总是存在至少一条不经
过任何泥塘的路径。
输入
第1行:3个用空格隔开的整数:X,Y和N:
第2~N+1行:第i+l行为2个用空格隔开的整数:A_i和B_i。
输出
输出1个整数,即FJ在不踏进泥塘的情况下,到达贝茜所在牛棚所需要走过的最小距离。
样例输入
1 2 7
0 2
-1 3
3 1
1 1
4 2
-1 1
2 2
样例输出
11
提示
样例说明:贝茜所在牛棚的坐标为(1,2)。FJ能看到7个泥塘,它们的坐标分别为(0,2),(-1,3),(3,1),(1,1),(4,2),(-l,1),(2,2)。
以下为农场的简图(*为FJ的屋子,B为贝茜呆的牛棚):
y
↑
4| . . . . . . . .
3| . M . . . . . .
2| . . M B M . M .
1| . M . M . M . .
0| . . * . . . . .
-1| . . . . . . . .
--------------------→ x
-2-1 0 1 2 3 4 5
FJ的最佳路线是:(0,0),(-1,0),(-2,0),(-2,1),(-2,2),(-2,3),(-2,4),
(-1,4),(0,4),(0,3),(1,3),(1,2)。
【分析】由于某些坐标是负数,我们把每个坐标+500就成正的了。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
using namespace std;
int n,m,flag=,a,b;
int dis[][]= {,,,,-,,,-};
int vis[][];
int w[][];
struct man
{
int x,y,step;
};
queue<man>q;
void bfs(man s)
{
q.push(s);
vis[s.x][s.y]=;
while(!q.empty())
{
man t=q.front();
q.pop();
//printf("%d %d %d\n",t.x,t.y,t.step);system("pause");
if(t.x==a&&t.y==b){printf("%d\n",t.step);flag=;return;}
for(int i=;i<;i++)
{
int xx=t.x+dis[i][];int yy=t.y+dis[i][];//
//printf("%d %d\n",xx,yy);
if(xx>=&&xx<=&&yy>=&&yy<=&&w[xx][yy]!=&&vis[xx][yy]==)
{
man k;k.x=xx;k.y=yy;k.step=t.step+;q.push(k);vis[xx][yy]=;
}
}
}
}
int main()
{
int g,h;
memset(vis,,sizeof(vis));
memset(w,,sizeof(w));
scanf("%d%d%d",&a,&b,&n);man s;
a+=;b+=;
for(int i=;i<n;i++){scanf("%d%d",&g,&h);w[g+][h+]=;}
s.x=;s.y=;s.step=;
bfs(s);
//if(flag==0)printf("-1\n");
return ;
}
穿越泥地(mud) (BFS)的更多相关文章
- 穿越泥地(mud)
穿越泥地(mud) 题目描述 清早6:00,FJ就离开了他的屋子,开始了他的例行工作:为贝茜挤奶.前一天晚上,整个农场刚经受过一场瓢泼大雨的洗礼,于是不难想象,FJ现在面对的 是一大片泥泞的土地.FJ ...
- BZOJ_1627_[Usaco2007_Dec]_穿越泥地_(bfs)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1627 网格图,给出起点,终点,障碍,求最短路. 分析 简单的宽搜. #include < ...
- 【BZOJ】1627: [Usaco2007 Dec]穿越泥地(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1627 裸bfs不解释.. #include <cstdio> #include < ...
- bzoj 1627: [Usaco2007 Dec]穿越泥地【bfs】
在洛谷上被卡了一个点开了O2才过= = bfs即可,为方便存储,把所有坐标+500 #include<iostream> #include<cstdio> #include&l ...
- BZOJ 1627: [Usaco2007 Dec]穿越泥地( BFS )
BFS... --------------------------------------------------------------------------------------- #incl ...
- BZOJ1627: [Usaco2007 Dec]穿越泥地
1627: [Usaco2007 Dec]穿越泥地 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 478 Solved: 303[Submit][Sta ...
- 1627: [Usaco2007 Dec]穿越泥地
1627: [Usaco2007 Dec]穿越泥地 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 504 Solved: 325[Submit][Sta ...
- bzoj1627 / P2873 [USACO07DEC]泥水坑Mud Puddles
P2873 [USACO07DEC]泥水坑Mud Puddles bfs入门. 对于坐标为负的情况,我们可以给数组下标加上$abs(min(minx,miny))$转正(根据题意判断) #includ ...
- 洛谷 P2873 [USACO07DEC]泥水坑Mud Puddles
P2873 [USACO07DEC]泥水坑Mud Puddles 题目描述 Farmer John is leaving his house promptly at 6 AM for his dail ...
随机推荐
- Map学习
1.Query Operations(查询操作) int size();boolean isEmpty(); boolean containsKey(Object key);boolean conta ...
- English test for certificate
<英语口译全真试题精解> GPA 3.5 (MTI) (CPA) ( GRE ) 1350分+3.5以上 SAT ( TOEFL ) 100分以上 TOEIC BEC (剑桥商务英语 ...
- 【转发】linux yum命令详解
linux yum命令详解 yum(全 称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理, ...
- [转]Putty中文乱码解决方法
from: http://www.putty.ws/putty-luanma putty使用的过程中,你是否遇到过putty出现中文乱码的情况呢?putty终端出现乱码,是情况,多数是由于系统或软件缺 ...
- 隐藏与显示:display/visibility/visible区别
说到标签的隐藏,你们会用到什么呢?display?visibility?还是服务器控件的visible? 显然,这三者都能起到隐藏与显示的效果,但是用途确完全不一样,请看用法与区别: <div ...
- .net 小问题集结
1 .net中新建的项目,调试时,提示"由于未在web.config文件中启用调试,因此无法在调试模式下运行该页.您希望做什么?" 解决办法: 在web.config文件中,将&l ...
- julia与python中的列表解析.jl
julia与python中的列表解析.jl #=julia与python中的列表解析.jl 2016年3月16日 07:30:47 codegay julia是一门很年轻的科学计算语言 julia文档 ...
- iframe和form表单的target应用简单例子
iframe和form表单的target属性 Problem: 刷新主页面中的其中一个iframe,其他内容不变 Solution: main.jsp <body onload=" ...
- BZOJ 2111 排列计数
f[i]=f[l]*f[r]*C(size[l]+size[r],size[l]). 需要lucas. #include<iostream> #include<cstdio> ...
- HDOJ-三部曲一(搜索、数学)- A Knight's Journey
A Knight's Journey Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...