bzoj1671 [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.
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行,离骑士们不远.
贝茜可以按这样的路线完成骑士的任务:北,西,北,南,东,东,北,东,东,南,南.她在森林的西北角得到一株她需要的灌木,然后绕过障碍把它交给在东南方的骑士.
题意是从“2”到“3”途径至少一个“4”,而“1”是路障不能走。第一眼看上去像网络流……但是一看到n、m1000的范围……
后来想了一下发现我逗比了……只要从起点搜一遍、从终点搜一遍,求到每一个点的距离,然后枚举中途的“4”,取距离最小就可以了
wa了一次……因为没用正确的姿势开数组……
#include<cstdio>
#include<cstring>
struct target{
int x,y;
}t[1000001];
struct queue{
int x,y;
}q[1000001];
const int mx[4]={0,1,0,-1};
const int my[4]={1,0,-1,0};
int n,m,cnt,x1,y1,x2,y2,head,tail,ans=100000000;
int map[1001][1001];
int dis1[1001][1001];
int dis2[1001][1001];
bool mrk[1001][1001];
inline int min(int a,int b)
{return a<b?a:b;}
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline void bfs1(int x,int y)
{
head=0;tail=1;mrk[x][y]=1;
q[1].x=x;q[1].y=y;
while (head<tail)
{
int nx=q[++head].x,ny=q[head].y;
for (int k=0;k<4;k++)
{
int xx=nx+mx[k],yy=ny+my[k];
if (xx<1||xx>n||yy<1||yy>m)continue;
if (mrk[xx][yy]||map[xx][yy]==1) continue;
dis1[xx][yy]=dis1[nx][ny]+1;
q[++tail].x=xx;q[tail].y=yy;
mrk[xx][yy]=1;
}
}
}
inline void bfs2(int x,int y)
{
memset(q,0,sizeof(q));
memset(mrk,0,sizeof(mrk));
head=0;tail=1;mrk[x][y]=1;
q[1].x=x;q[1].y=y;
while (head<tail)
{
int nx=q[++head].x,ny=q[head].y;
for (int k=0;k<4;k++)
{
int xx=nx+mx[k],yy=ny+my[k];
if (xx<1||xx>n||yy<1||yy>m)continue;
if (mrk[xx][yy]||map[xx][yy]==1) continue;
dis2[xx][yy]=dis2[nx][ny]+1;
q[++tail].x=xx;q[tail].y=yy;
mrk[xx][yy]=1;
}
}
}
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]==4)
{
t[++cnt].x=i;
t[cnt].y=j;
}else
if (map[i][j]==2)
{
x1=i;
y1=j;
map[i][j]=0;
}else
if (map[i][j]==3)
{
x2=i;
y2=j;
map[i][j]=0;
}
}
bfs1(x1,y1);
bfs2(x2,y2);
for (int i=1;i<=cnt;i++)
{
int nx=t[i].x,ny=t[i].y;
if (!(dis1[nx][ny]+dis2[nx][ny]))continue;
ans=min(ans,dis1[nx][ny]+dis2[nx][ny]);
}
printf("%d",ans);
}
bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士的更多相关文章
- 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 骑士 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 ...
- BZOJ1671: [Usaco2005 Dec]Knights of Ni
1671: [Usaco2005 Dec]Knights of Ni Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 175 Solved: 107[Su ...
- 【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 ...
- [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:由于各种原因而不可通 ...
随机推荐
- SQLServer 安装以前的某个程序安装已在安装计算机上创建挂起的文件操作 解决办法
http://wenku.baidu.com/view/6732fe09844769eae009ede2.html SQL Server 安装以前的某个程序安装已在安装计算机上创建挂起的文件操作 安装 ...
- 使IE6同样支持圆角效果
之前写到过,IE6不支持:hover效果的解决办法,其它这个跟它一样.IE6(7/8)不支持border-radius属性,所以其中的圆角效果显示不出来,可以通过引用ie-css3.htc的方法解决. ...
- 【转】10分钟了解设计模式(C#)
10分钟了解设计模式(C#) 最近把<大话设计模式>重温了下(看完序才知道原来作者是也是博客园园友,这本书的最早博客版本在这里).体会最深的就是面向接口编程的重要性,如何在自己的项目中进行 ...
- JavaScript学习总结(二)
JavaScript学习总结(二) ---- 对象 在JavaScript中,几乎用到的每个js都离不开它的对象.下面我们深入了解一下js对象. js中对象的分类跟之前我们学过的语言中函数的分类一样, ...
- UVa 10701 - Pre, in and post
题目:已知树的前根序,中根序遍历转化成后根序遍历. 分析:递归,DS.依据定义递归求解就可以. 前根序:根,左子树,右子树: 中根序:左子树,根,右子树: 每次,找到根.左子树.右子树,然后分别递归左 ...
- Dynamics CRM 常用 C# 方法集合
Plugin(C#) 分派 AssignRequest assign = new AssignRequest(); assign.Assignee = prEntity["ownerid&q ...
- jquery之null的数组
去掉null的数组 function ClearNullArr(arr) { for (var i = 0; i < arr.length; i++) { if(arr[ ...
- Android测试框架1(非原创)
1.继承AndroidTestCase :public class JunitTest3 extends AndroidTestCase {} 2.在AndroidManifest.xml清单文件中添 ...
- java HashMap的原理
HashMap的数据结构: 在java编程语言中,最基本的结构就是两种,一个是数组,另外一个是模拟指针(引用),所有的数据结构都可以用这两个基本结构来构造的,HashMap也不例外.HashMap实际 ...
- call()与apply()传参需要注意的一点
call()与apply()是用来改变函数体内的this指向,第一个参数是调用函数的母对象,他是调用上下文,函数体内通过this来获得对它的引用,换句话说就是第一参数===函数中的this. 但是如下 ...