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 ...
随机推荐
- Android 中状态栏、标题栏、View的大小及区分
1.获得状态栏的高度(状态栏相对Window的位置): Rect frame = new Rect(); getWindow().getDecorView().getWindowVisibleDisp ...
- SharpSSH
SharpSSH sharpssh is a pure .NET implementation of the SSH2 client protocol suite. It provides an AP ...
- Java并发学习 & Executor学习 & 异常逃逸 & 同步互斥Best Practice & wait/notify, conditon#await/signal
看了这篇文章:http://www.ciaoshen.com/2016/10/28/tij4-21/ 有一些Java并发的内容,另外查了一些资料. 朴素的Thread 首先,Java中关于线程Thre ...
- C#网络编程系列文章(一)之Socket实现异步TCPserver
原创性声明 本文作者:小竹zz 本文地址http://blog.csdn.net/zhujunxxxxx/article/details/44258719 转载请注明出处 文章系列文件夹 C#网络编程 ...
- String,StringBuilder性能对照
import java.util.Date; import java.util.UUID; /** * 測试String,StringBuilder性能,推断什么时候改用String,什么时候该用S ...
- python(23)- 面向对象简单介绍
面向概述 面向过程:根据业务逻辑从上到下写垒代码 面向过程的设计的核心是过程,过程即解决问题的步骤, 面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西 优点:极大降低了程序的复杂 ...
- 转:DDR原理详解
首先,我们先了解一下内存的大体结构工作流程,这样会比较容量理解这些参数在其中所起到的作用.这部分的讲述运用DDR3的简化时序图. DDR3的内部是一个存储阵列,将数据“填”进去,你可以它想象成一张表格 ...
- EC2的维护更新
2014年9月28日 近期几天.我们收到了一些客户关于我们即将进行维护更新的问题.下面是AWS全球Blog网站对这个问题的说明,供客户參照. 我们已经開始通知那些受影响的客户,关于我们即将实施的 ...
- Linux问题,磁盘分区打不开了
Metadata kept in Windows cache, refused to mount. chkdsk /f http://www.bubuko.com/infodetail-1184937 ...
- python发送邮件相关问题总结
一.发送邮件报错:554:DT:SPM 1.报错信息 2.通过查找163报错信息页面,554 DT:SPM的问题如下: 3.将邮件主题中的“test”去除,经过测试,实际上邮件主题包含“test”也能 ...