[Usaco2005 Dec]Knights of Ni 骑士
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.
贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为了能安全地离开,贝茜不得不按照骑士们的要求,在森林寻找一种特殊的灌木并带一棵给他们.当然,贝茜想早点离开这可怕的森林,于是她必须尽快完成骑士们给的任务,贝茜随身带着这片森林的地图,地图上的森林被放入了直角坐标系,并按x,y轴上的单位长度划分成了W×H(1≤W,H≤1000)块,贝茜在地图上查出了她自己以及骑士们所在的位置,当然地图上也标注了她所需要的灌木生长的区域.某些区域是不能通过的(比如说沼泽地,悬崖,以及食人兔的聚居地).在没有找到灌木之前,贝茜不能通过骑士们所在的那个区域,为了确保她自己不会迷路,贝茜只向正北、正东、正南、正西四个方向移动(注意,她不会走对角线).她要走整整一天,才能从某块区域走到与它相邻的那块区域. 输入数据保证贝茜一定能完成骑士的任务.贝茜希望你能帮她计算一下,她最少需要多少天才可脱离这可怕的地方?
Input
第1行输入2个用空格隔开的整数,即题目中提到的W、H.
接下来输入贝茜持有的地图,每一行用若干个数字代表地图上对应行的地形.第1行描述了地图最北的那一排土地;最后一行描述的则是最南面的.相邻的数字所对应的区域是相邻的.如果地图的宽小于或等于40,那每一行数字恰好对应了地图上的一排土地.如果地图的宽大于40,那每行只会给出40个数字,并且保证除了最后一行的每一行都包含恰好40个数字.没有哪一行描述的区域分布在两个不同的行里.
地图上的数字所对应的地形:
0:贝茜可以通过的空地
1:由于各种原因而不可通行的区域
2:贝茜现在所在的位置
3:骑士们的位置
4:长着贝茜需要的灌木的土地
Output
输出一个正整数D,即贝茜最少要花多少天才能完成骑士们给的任务.
Sample Input
8 4
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
Sample Output
11
HINT
这片森林的长为8,宽为4.贝茜的起始位置在第3行,离骑士们不远.
贝茜可以按这样的路线完成骑士的任务:北,西,北,南,东,东,北,东,东,南,南.她在森林的西北角得到一株她需要的灌木,然后绕过障碍把它交给在东南方的骑士.
这题其实并不难想,两遍bfs,从贝茜的位置跑一遍,再从骑士的位置跑一遍,然后扫到有灌木的点随便搞一下就可以了
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x>=10) print(x/10);
putchar(x%10+'0');
}
const int N=1e3;
const int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
int map[N+10][N+10],dis[N+10][N+10],dist[N+10][N+10];
int hx[N*N+10],hy[N*N+10];
int n,m,O1x,O1y,O2x,O2y;
bool in_map(int x,int y){return x>0&&x<=n&&y>0&&y<=m;}
void bfs1(int x,int y){
int head=1,tail=1;
memset(dis,-1,sizeof(dis));
hx[1]=x,hy[1]=y,dis[x][y]=0;
for (;head<=tail;head++){
int Nx=hx[head],Ny=hy[head];
for (int i=0;i<4;i++){
int tx=Nx+dx[i],ty=Ny+dy[i];
if (!in_map(tx,ty)||dis[tx][ty]!=-1||map[tx][ty]==1) continue;
dis[tx][ty]=dis[Nx][Ny]+1;
hx[++tail]=tx,hy[tail]=ty;
}
}
}
void bfs2(int x,int y){
int head=1,tail=1;
memset(dist,-1,sizeof(dist));
hx[1]=x,hy[1]=y,dist[x][y]=0;
for (;head<=tail;head++){
int Nx=hx[head],Ny=hy[head];
for (int i=0;i<4;i++){
int tx=Nx+dx[i],ty=Ny+dy[i];
if (!in_map(tx,ty)||dist[tx][ty]!=-1||map[tx][ty]==1) continue;
dist[tx][ty]=dist[Nx][Ny]+1;
hx[++tail]=tx,hy[tail]=ty;
}
}
}
int work(){
int Min=inf;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++){
if (map[i][j]!=4||dis[i][j]==-1||dist[i][j]==-1) continue;
Min=min(Min,dis[i][j]+dist[i][j]);
}
return Min;
}
int main(){
m=read(),n=read();
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++){
map[i][j]=read();
if (map[i][j]==2) O1x=i,O1y=j;
if (map[i][j]==3) O2x=i,O2y=j;
}
bfs1(O1x,O1y),bfs2(O2x,O2y);
printf("%d\n",work());
return 0;
}
[Usaco2005 Dec]Knights of Ni 骑士的更多相关文章
- 【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> # ...
- 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 ...
- 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 ...
随机推荐
- 细说分布式Redis架构设计和踩过的那些坑
细说分布式Redis架构设计和踩过的那些坑_redis 分布式_ redis 分布式锁_分布式缓存redis 细说分布式Redis架构设计和踩过的那些坑
- 【Nginx】Nginx的配置
配置文件为.conf文件 一.块配置项 块配置项由一个块配置项名和一对大括号组成.具体如下: events{ ... } http{ upstream backend{ server 127.0.0. ...
- Deepin-文件目录介绍
请参见这篇文件:来自一个强大的网站 我主要介绍的就是: 下面所列文件,全部添加进了path目录(Linux查找命令,请参见man.linux,无论是find 或者是 which等) Deepin默认可 ...
- oracle获取字符串长度函数length()和lengthb()
oracle获取字符串长度函数length()和lengthb() lengthb(string)计算string所占的字节长度:返回字符串的长度,单位是字节 length(string)计算st ...
- 深度学习笔记之关于总结、展望、参考文献和Deep Learning学习资源(五)
不多说,直接上干货! 十.总结与展望 1)Deep learning总结 深度学习是关于自动学习要建模的数据的潜在(隐含)分布的多层(复杂)表达的算法.换句话来说,深度学习算法自动的提取分类需要的低层 ...
- VC2010 利用 def 文件生成 dll 文件的方法
近期有个需求,要生成一个dll 文件.文件里的函数都是採用 stdcall 函数调用约定,可是不希望函数名被修饰(add 被修饰成 add@8). 这时就要用def 文件了. 比方我有以下两个函数: ...
- [思考]我们应该怎样建设企业IT
从人员架构上来看,要不要企业自己的IT团队?如果要,应该有什么样的架构?运维,开发,管理,项目? 从是否外包角度看,要不要外包?如果外包,如何建立外包流程? 从业务角度看,企业IT的发展应该是围绕业务 ...
- perl BEGIN block and END block
1 本质上就是一段代码 BEGIN在程序运行前执行,END在程序运行之后执行. 2 BEGIN END的行为和所在的位置无关 也就是说,无论BEGIN和END block位于代码的哪里,最先执行的是B ...
- O(n²)、O(n)、O(1)、O(nlogn)
大体上和 @丁戍 说的差不多. 简单说O(n²)表示当n很大的时候,复杂度约等于Cn²,C是某个常数,简单说就是当n足够大的时候,n的线性增长,复杂度将沿平方增长. O(n)也是差不多的意思,也就是说 ...
- qemu-kvm磁盘读写的缓冲(cache)的五种模式
qemu-kvm磁盘读写的缓冲(cache)模式一共有五种,分别是writethrough, wirteback, none, unsafe, directsync当你对VM读写磁盘的性能有不同的要求 ...