BZOJ_1671_[Usaco2005 Dec]Knights of Ni 骑士_BFS
Description
Bessie is in Camelot and has encountered a sticky situation: she needs to pass through the forest that is guarded by the Knights of Ni. In order to pass through safely, the Knights have demanded that she bring them a single shrubbery. Time is of the essence, and Bessie must find and bring them a shrubbery as quickly as possible. Bessie has a map of of the forest, which is partitioned into a square grid arrayed in the usual manner, with axes parallel to the X and Y axes. The map is W x H units in size (1 <= W <= 1000; 1 <= H <= 1000). The map shows where Bessie starts her quest, the single square where the Knights of Ni are, and the locations of all the shrubberies of the land. It also shows which areas of the map can be traverse (some grid blocks are impassable because of swamps, cliffs, and killer rabbits). Bessie can not pass through the Knights of Ni square without a shrubbery. In order to make sure that she follows the map correctly, Bessie can only move in four directions: North, East, South, or West (i.e., NOT diagonally). She requires one day to complete a traversal from one grid block to a neighboring grid block. It is guaranteed that Bessie will be able to obtain a shrubbery and then deliver it to the Knights of Ni. Determine the quickest way for her to do so.
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行,离骑士们不远.
贝茜可以按这样的路线完成骑士的任务:北,西,北,南,东,东,北,东,东,南,南.她在森林的西北角得到一株她需要的灌木,然后绕过障碍把它交给在东南方的骑士.
用每个灌木更新答案即可。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 1050
int tx[]={0,1,0,-1};
int ty[]={-1,0,1,0};
int map[1050][1050],n,Q[2050000],l,r,dis[N][N][2],s_x,s_y,t_x,t_y,vis[N][N],m;
void bfs(int x,int y,int idx) {
memset(vis,0,sizeof(vis));
dis[x][y][idx]=0;
l=r=0; Q[r++]=x; Q[r++]=y;
while(l<r) {
x=Q[l++]; y=Q[l++];int i; vis[x][y]=1;
for(i=0;i<4;i++) {
int dx=x+tx[i],dy=y+ty[i];
if(dx>=1&&dx<=n&&dy>=1&&dy<=m&&vis[dx][dy]==0&&map[dx][dy]!=1&&map[dx][dy]!=3) {
dis[dx][dy][idx]=dis[x][y][idx]+1;
Q[r++]=dx; Q[r++]=dy;
vis[dx][dy]=1;
}
}
}
}
int main() {
scanf("%d%d",&m,&n);
int i,j;
for(i=1;i<=n;i++) {
for(j=1;j<=m;j++) {
scanf("%d",&map[i][j]);
if(map[i][j]==2) s_x=i,s_y=j;
if(map[i][j]==3) t_x=i,t_y=j;
}
}
memset(dis,0x3f,sizeof(dis));
bfs(s_x,s_y,0); bfs(t_x,t_y,1);
int ans=1<<30;
for(i=1;i<=n;i++) {
for(j=1;j<=m;j++) {
if(map[i][j]==4) {
ans=min(ans,dis[i][j][0]+dis[i][j][1]);
}
}
}
printf("%d\n",ans);
}
BZOJ_1671_[Usaco2005 Dec]Knights of Ni 骑士_BFS的更多相关文章
- 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS
[Usaco2005 Dec]Knights of Ni 骑士 Description 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...
- 1671: [Usaco2005 Dec]Knights of Ni 骑士
1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 254 Solved: 163 ...
- 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)
http://www.lydsy.com/JudgeOnline/problem.php?id=1671 从骑士bfs一次,然后从人bfs一次即可. #include <cstdio> # ...
- [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.. 把一个逗号 ...
- 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:由于各种原因而不可通 ...
- BZOJ1671: [Usaco2005 Dec]Knights of Ni
1671: [Usaco2005 Dec]Knights of Ni Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 175 Solved: 107[Su ...
随机推荐
- 【层次查询】Hierarchical Queries之亲兄弟间的排序(ORDER SIBLINGS BY)
http://blog.itpub.net/519536/viewspace-624176 有关层次查询之前的文章参考如下. [层次查询]Hierarchical Queries之"树的遍历 ...
- python pyqtgraph 保存图片到本地
pyqtgraph官方给的示例居然会报错2333 官方文档传送门:#####pyqtgraph export pyqtgraph支持在可视化窗口中右键保存(Exporting from the GUI ...
- 并行程序设计---cuda memory
CUDA存储器模型: GPU片内:register,shared memory: host 内存: host memory, pinned memory. 板载显存:local memory,cons ...
- vForum 2014点滴随笔
vForum2014 的口号:NO Limits 纵横无限 一条好消息:VMware 将在中国建立亚洲研究院,并在5年内投资10亿美元. VMware宋先生的演讲再次印证了Redhat会议上的趋势: ...
- POJ 3978(求素数)
知识点: 1.求素数的test,从2~sqrt(n): 2.假设数据非常多,能够用素数表记录,然后sum=prime[m]-prime[n]求得! ! !! !!! !! ...
- js 宽和高
网页可见区域宽: document.body.clientWidth; 网页可见区域高: document.body.clientHeight; 网页可见区域宽: document.body.offs ...
- DB 【ACID】
http://blog.csdn.net/shuaihj/article/details/14163713 http://blog.csdn.net/dief913975849/article/det ...
- Android------Intent.createChooser
Intent的匹配过程中有三个步骤,包含Action , category与data 的匹配. 假设匹配出了多个结果.系统会显示一个dialog让用户来选 择.例如以下图: 那么今天我们主要是解 ...
- vue详细操作目录-基础篇
目录结构:-lib-main.js -lib-vue.js index.html 每个网页第一个均为HTML页面,第二个为js文件(主要文件) 1.vue的安装以及语法介绍 2.v-for指令 3.v ...
- kubernetes安装过程中遇到问题及解决
系列目录 根据机器环境的不同,有的可能一次就安装成功,有的则可能遇到各种各样的坑需要排查.建议不熟悉linux的用户使用全新的环境来安装kubernetes.以下记录本人在安装过程中遇到的问题及解决方 ...