【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:由于各种原因而不可通 ...
随机推荐
- 11月8日PHP练习《留言板》
一.要求 二.示例页面 三.网页代码及网页显示 1.denglu.php 登录页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...
- C# 的TCP Socket (同步方式)
简单的c# TCP通讯(TcpListener) C# 的TCP Socket (同步方式) C# 的TCP Socket (异步方式) C# 的tcp Socket设置自定义超时时间 C# TCP ...
- Python学习笔记——条件和循环
1.条件表达式 >>> x = 3 >>> x = 1 if x<3 else 2 >>> x 2 2.for语句用于序列类型 <1& ...
- css之图片路径
关于背景图片url路径:图片和调用文件在两个不同根下的(不在同一个包(文件夹)中),要用相对路径,举例"../images/cq.gif":图片和调用文件是在一个根下的直接孩子,用 ...
- 2015.4.20 Canvas Jquery 移动端 JavaScript
1.分享效果:弹窗Canvas渲染大图. 2.进度条中表现进度百分比的数值d%,根据进度的增长“字体颜色”为了表示清晰也随着变化 解决方法:参考Demo. 3.输入框保持查询参数,结果列表局部 ...
- Bash 为何要发明 shopt 命令
在 Bash 中,有两个内置命令用来控制 Bash 的各种可配置行为的开关(打开或关闭),这些开关称之为选项(option).其中一个命令是 set,set 命令有三种功能:显示所有的变量和函数:修改 ...
- /etc/rc.d/rc与/etc/rc.d/init.d的关系
在这里先解释一下 /etc/rc.d/init.d 里面放的都是什么东西.这个目录存放的是一些脚本,一般是Linux以rpm包安装时设定的一些服务的启动/关闭脚本.系统在安装时装了好多rpm包,这里面 ...
- Swift3.0P1 语法指南——控制流
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- 【krpano】汉化Web VR设置界面
欢迎加入qq群551278936讨论krpano解密技术以及获取最新软件 krpano 1.19支持了Web VR功能,允许以VR的方式查看全景图,配合上VR设备可以实现VR效果. 在VR方式查看时, ...
- tomcate端口设定和服务器虚拟目录设定
设定端口和 虚拟目录 在server.xml <?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apa ...