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 ...
随机推荐
- 安装mongoDB遇见的一个路径问题
如果安装路径不存在,则不会解压EXE软件! 安装monogoDB后,它不会自动添加执行路径! 意思就是安装路径是D盘下面的mongoDB文件夹,假如不存在这个文件夹,则不会安装成功 你需要添加路径: ...
- npm 安装包无法继续下载? 卡住
一般是由于package.json配置的项目名不对
- android客户端向服务器端验证登陆方法的实现2
一.在上一篇文章中,我只是提到了其中一种方法来实现登陆 大家可以参见: http://www.apkbus.com/android-45004-1-1.html android获取web服务 ...
- Android的logger机制分析
分析安卓的Logger机制 一.概述 Logger机制是在Android系统中提供的一个轻量级的日志系统,这个日志系统是以驱动程序的形式在内核空间实现的,在用户空间分别提供了Java接口和C/C++接 ...
- web微信开发
群里接收消息时,使用广播,但需要刷新页面才能接收到广播内容. - 轮询: 定时每秒刷新一次,当群不活跃时,群里的每个客户端都在刷新,对服务端压力太大. - 长轮询:客户端连服务端,服务端一直不断开,也 ...
- mysql flush详解
http://www.cnblogs.com/ggjucheng/archive/2012/11/07/2758021.html cnblogs - FLUSH TABLES WITH READ ...
- docker安全最佳实践概述
/************************************************* * Author : Samson * Date : 08/07/2015 * Test plat ...
- C语言--函数篇
1-1.函数简单调用 1 #include<stdio.h> 2 #include<string.h> 3 #include<windows.h> 4 int ...
- 使用EA生成多层次的代码框架
最近工作期间发现了一个非常棒的UML软件[Enterprise Architect UML 建模工具]简称EA,在该软件上绘制框架层面的类之间关系后,可以自动生成相关语言的代码. EA上目前支持的语言 ...
- 升级svn 到1.7
sudo yum update sudo yum groupinstall "Development tools" sudo yum groupinstall "Addi ...