【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:由于各种原因而不可通 ...
随机推荐
- [NHibernate]条件查询Criteria Query
目录 写在前面 文档与系列文章 条件查询 一个例子 总结 写在前面 上篇文章介绍了HQL查询,我个人觉得使用ORM框架就是为了让少些sql,提高开发效率,而再使用HQL就好像还是使用sql,就觉得有点 ...
- Java与MySQL的连接
下载数据库驱动文件,解压并保存至任意位置 下载地址 新建Java项目,并将驱动文件添加到项目中 项目名右键-->构建路径-->配置构建路径-->添加外部Jar 在项目中新建类,编写代 ...
- JAVA关键字与保留字说明及使用
1.abstract 2.boolean 3.break 4.byte 5.case 6.catch 7.char 8.class 9.continue 10.default 11.do 12.dou ...
- PHP通用的XSS攻击过滤函数,Discuz系统中 防止XSS漏洞攻击,过滤HTML危险标签属性的PHP函数
XSS攻击在最近很是流行,往往在某段代码里一不小心就会被人放上XSS攻击的代码,看到国外有人写上了函数,咱也偷偷懒,悄悄的贴上来... 原文如下: The goal of this function ...
- Linux C 字符串函数 strlen()、strcat()、strncat()、strcmp()、strncmp()、strcpy()、strncpy() 详解
strlen(返回字符串长度) 表头文件 #include <string.h> 定义函数 size_t strlen(const char *s); 函数说明 strlen()用来计 ...
- 在Linux上挂载Windows共享文件夹,如何开机自动挂载(mount)?
按照一般的思路,我们先将文件夹挂载上去,命令如下: mkdir /mnt/share_software mount //192.9.206.43/share_software /mnt/share_s ...
- ubuntu下建立NFS共享,并用开发板挂载
安装NFS服务 apt-get install nfs-kernel-server nfs-common apt-get install portmap 在/etc/exports里加入 /home/ ...
- php,nginx重启
查看php运行目录命令:which php/usr/bin/php 查看php-fpm进程数:ps aux | grep -c php-fpm 查看运行内存/usr/bin/php -i|grep ...
- 【Python】自动生成html文件查看指定目录中的所有图片
获取本目录下的pic子目录中的所有图片(jpg,png,bmp,gif等,此处以jpg文件为例),然后生成一个image.html文件,打开该html文件即可在浏览器中查看pic子目录中的所有图片. ...
- Redis Sentinel机制与用法说明【转】
本文来自:https://segmentfault.com/a/1190000002680804 概述 Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Ma ...