【BZOJ】1671: [Usaco2005 Dec]Knights of Ni 骑士(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1671
从骑士bfs一次,然后从人bfs一次即可。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1005, Q=N*N, dx[]={-1, 1, 0, 0}, dy[]={0, 0, -1, 1};
struct dat { int x, y; } q[Q];
int front, tail, d[N][N], d2[N][N], mp[N][N], n, m, xx, yy, X, Y, ans=~0u>>1;
void bfs1() {
q[0].x=xx, q[0].y=yy; ++tail;
CC(d, 0x7f);
d[xx][yy]=0;
while(tail!=front) {
dat &t=q[front++]; if(front==Q) front=0;
int x=t.x, y=t.y;
rep(i, 4) {
int fx=dx[i]+x, fy=dy[i]+y;
if(fx<1 || fy<1 || fx>n || fy>m || mp[fx][fy]==1) continue;
if(d[fx][fy]>d[x][y]+1) {
d[fx][fy]=d[x][y]+1;
q[tail].x=fx; q[tail++].y=fy; if(tail==Q) tail=0;
}
}
}
}
void bfs2() {
q[0].x=X, q[0].y=Y; front=tail=0; ++tail;
CC(d2, 0x7f);
d2[X][Y]=0;
while(tail!=front) {
dat &t=q[front++]; if(front==Q) front=0;
int x=t.x, y=t.y;
if(mp[x][y]==4) { ans=min(d2[x][y]+d[x][y], ans); continue; }
rep(i, 4) {
int fx=dx[i]+x, fy=dy[i]+y;
if(fx<1 || fy<1 || fx>n || fy>m || mp[fx][fy]==1 || mp[fx][fy]==3) continue;
if(d2[fx][fy]>d2[x][y]+1) {
d2[fx][fy]=d2[x][y]+1;
q[tail].x=fx; q[tail++].y=fy; if(tail==Q) tail=0;
}
}
}
} int main() {
read(m); read(n);
for1(i, 1, n) for1(j, 1, m) {
char ch=getchar();
for(; ch<'0'||ch>'9'; ch=getchar());
mp[i][j]=ch-'0';
if(ch=='2') X=i, Y=j;
if(ch=='3') xx=i, yy=j;
}
bfs1();
bfs2();
print(ans);
return 0;
}
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行,离骑士们不远.
贝茜可以按这样的路线完成骑士的任务:北,西,北,南,东,东,北,东,东,南,南.她在森林的西北角得到一株她需要的灌木,然后绕过障碍把它交给在东南方的骑士.
Source
【BZOJ】1671: [Usaco2005 Dec]Knights of Ni 骑士(bfs)的更多相关文章
- 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:由于各种原因而不可通 ...
- 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 骑士 BFS
[Usaco2005 Dec]Knights of Ni 骑士 Description 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...
- 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
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 ...
- BZOJ1671: [Usaco2005 Dec]Knights of Ni
1671: [Usaco2005 Dec]Knights of Ni Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 175 Solved: 107[Su ...
随机推荐
- JUnit 3.8 演示递归删除文件目录的 测试类程序 .
用递归方式来实现删除硬盘的文件或目录(空文件夹) 首先要找到递归的入口及出口,这点很重要,成败在此,呵呵! 代码实现: import java.io.File ; class RecursionDel ...
- 从缓存行出发理解volatile变量、伪共享False sharing、disruptor
volatilekeyword 当变量被某个线程A改动值之后.其他线程比方B若读取此变量的话,立马能够看到原来线程A改动后的值 注:普通变量与volatile变量的差别是volatile的特殊规则保证 ...
- xml xpath dta笔记
xml: 有且只有一个根元素 默认utf-8 如果是中文且为不是utf-8的必须指定编码 声明的编码必须和文档的内容保持一致 well-formed XML :是否符合xml语法 valid xml: ...
- javax.naming.NoInitialContextException: Need to specify class name in environment or system property
javax.naming.NoInitialContextException: Need to specify class name in environment or system property ...
- 解决在sdk manager中更新文件后出现This Android SDK requires Android Developer Toolkit version 23.1的错误
起因:在sdksdk manager中更新了adt及其它的支持库后,eclipse报错:This Android SDK requires Android Developer Toolkit vers ...
- Ubuntu中类似任务管理器的东西?
Ubuntu里面有没有类似windows中任务管理器的东西呢?怎么打开?谢谢!!! ================================ 检举| 2009-02-01 16:50提问者 ...
- C#指南,重温基础,展望远方!(7)C#结构
结构是可以包含数据成员和函数成员的数据结构,这一点与类一样:与类不同的是,结构是值类型,无需进行堆分配. 结构类型的变量直接存储结构数据,而类类型的变量存储对动态分配的对象的引用. 结构类型不支持用户 ...
- 有道翻译 / 百度翻译Api
比较推荐使用百度翻译api 不推荐有道翻译,比较水. http://ai.youdao.com/docs/doc-trans-api.s#p02 http://ai.youdao.com/docs/d ...
- JVM虚拟机(四):JVM 垃圾回收机制概念及其算法
垃圾回收概念和其算法 谈到垃圾回收(Garbage Collection)GC,需要先澄清什么是垃圾,类比日常生活中的垃圾,我们会把他们丢入垃圾箱,然后倒掉.GC中的垃圾,特指存于内存中.不会再被使用 ...
- Apache、Tomcat负载均衡与集群
一. 环境准备 1.软件下载 a) apache_2.0.55-win32-x86-no_ssl.msi: b) apache-tomcat-5.5.17.rar c) mod_jk-apache-2 ...