【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS
[Usaco2005 Dec]Knights of Ni 骑士
Description
Input
Output
Sample Input
4 1 0 0 0 0 1 0
0 0 0 1 0 1 0 0
0 2 1 1 3 0 4 0
0 0 0 4 1 1 1 0
INPUT DETAILS:
Width=8, height=4. Bessie starts on the third row, only a few squares away
from the Knights.
Sample Output
HINT
这片森林的长为8,宽为4.贝茜的起始位置在第3行,离骑士们不远.
贝茜可以按这样的路线完成骑士的任务:北,西,北,南,东,东,北,东,东,南,南.她在森林的西北角得到一株她需要的灌木,然后绕过障碍把它交给在东南方的骑士.
题解:双BFS,刷水有益健康。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define ok(a,b) (a>=1&&a<=w&&b>=1&&b<=h&&map[a][b]!=1)
using namespace std;
int map[1010][1010];
int w,h,ans,dis[1010][1010][2],inq[1010][1010],x1,x2,y1,y2;
int dir[][2]={{-1,0},{1,0},{0,1},{0,-1}};
queue <int> qx,qy;
int readin()
{
int ret=0; char gc;
while(gc<'0'||gc>'9') gc=getchar();
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret;
}
void bfs(int x,int y,int t)
{
dis[x][y][t]=0;
qx.push(x),qy.push(y);
int i,tx,ty;
while(!qx.empty())
{
x=qx.front(),y=qy.front();
qx.pop(),qy.pop();
for(i=0;i<4;i++)
{
tx=x+dir[i][0],ty=y+dir[i][1];
if(ok(tx,ty)&&dis[tx][ty][t]>dis[x][y][t]+1)
{
dis[tx][ty][t]=dis[x][y][t]+1;
if(!inq[tx][ty])
{
inq[tx][ty]=1;
qx.push(tx),qy.push(ty);
}
}
}
inq[x][y]=0;
}
}
int main()
{
h=readin(),w=readin();
int i,j;
for(i=1;i<=w;i++)
{
for(j=1;j<=h;j++)
{
map[i][j]=readin();
if(map[i][j]==2) x1=i,y1=j;
if(map[i][j]==3) x2=i,y2=j;
}
}
memset(dis,0x3f,sizeof(dis));
bfs(x1,y1,0),bfs(x2,y2,1);
ans=1<<30;
for(i=1;i<=w;i++)
for(j=1;j<=h;j++)
if(map[i][j]==4)
ans=min(ans,dis[i][j][0]+dis[i][j][1]);
printf("%d",ans);
return 0;
}
【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS的更多相关文章
- POJ3170 Bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士
1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 281 Solved: 180 ...
- bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士
Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...
- BZOJ 1671: [Usaco2005 Dec]Knights of Ni 骑士 (bfs)
题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1671 题解: 按题意分别从贝茜和骑士bfs然后meet_in_middle.. 把一个逗号 ...
- 1671: [Usaco2005 Dec]Knights of Ni 骑士
1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 254 Solved: 163 ...
- BZOJ1671: [Usaco2005 Dec]Knights of Ni
1671: [Usaco2005 Dec]Knights of Ni Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 175 Solved: 107[Su ...
- 【BZOJ】1671: [Usaco2005 Dec]Knights of Ni 骑士(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1671 从骑士bfs一次,然后从人bfs一次即可. #include <cstdio> # ...
- BZOJ_1671_[Usaco2005 Dec]Knights of Ni 骑士_BFS
Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...
- [Usaco2005 Dec]Knights of Ni 骑士
Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...
- bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士【bfs】
bfs预处理出每个点s和t的距离d1和d2(无法到达标为inf),然后在若干灌木丛格子(x,y)里取min(d1[x][y]+d2[x][y]) /* 0:贝茜可以通过的空地 1:由于各种原因而不可通 ...
随机推荐
- Google Map API V3开发(6) 代码
Google Map API V3开发(1) Google Map API V3开发(2) Google Map API V3开发(3) Google Map API V3开发(4) Google M ...
- ubuntu常用命令
<一> 安装文件 一.deb包的安装方式 sudo dpkg -i *.deb 二.编译安装方式 建立编译环境sudo apt-get install build-essential 到源 ...
- 15个JavaScript本地存储技术的函数库和工具
当构建更复杂的JavaScript应用程序运行在用户的浏览器是非常有用的,它可以在浏览器中存储信息,这样的信息可以被共享在不同的页面,浏览会话. 在最近的过去,这将有可能只被cookies文本文件保存 ...
- 【Json】关于json解析时异常org.json.JSONException: A JSONObject text must begin with '{' at character 1 of {的解决方法
遇到这种异常有几种情况: 1.JSON格式有问题,检查一下格式. 2.格式没问题,仍然报错,这个是因为你的json文件头里带有编码字符(如UTF-8等),读取字符串时json串是正常的,但是解析就有异 ...
- Android Activity的加载的模式
---恢复内容开始--- 本文来自http://www.cnblogs.com/lwbqqyumidi/p/3771542.html launchMode在多个Activity跳转的过程中扮演着重要的 ...
- Lua手动编译姿势
LUA-5.3.3.tar.gz Lua源码+链接2016年5月30日更新 手动编译姿势: 已经装有VS2010 使用VS自带的 cl.exe以及 VS命令簿 打开文件地址 运行自己的bat文件 my ...
- 【JSOI2010】Group 部落划分 BZOJ 1821
1821: [JSOI2010]Group 部落划分 Group Time Limit: 10 Sec Memory Limit: 64 MB 聪聪研究发现,荒岛野人总是过着群居的生活,但是 ...
- poj 3468(线段树)
http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线 ...
- Unity3D 摄像机的Transform通过摇杆输出的方向
要解决的问题是:摄像机的方向不固定,当摇杆向前(0,1)推时,主角要往摄像机的朝向(忽略Y方向)走,当摇杆往右(1,0)推的时,主角朝摄像机的右方向 /// <summary> /// 摄 ...
- [Android Pro] ScrollView使用fillViewport设置高度为MatchParent
reference to : http://blog.csdn.net/u012975705/article/details/49275001 之前遇到一问题,就是当使用ScrollView时,Scr ...