1671: [Usaco2005 Dec]Knights of Ni

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 175  Solved: 107
[Submit][Status][Discuss]

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. 给出一张W*H(1<=W,H<=1000)的地图,每个位置都标有0..4 的数字。其中0 表示可以通过,1 表示不能通过,2 表示Kitty 的起始位置,3 表示Kitty的心爱的骑士起始位置,4 表示树丛。现在要求求出一条最短的路,使骑士经过树丛至少一次,且能够到达Kitty 所在位置,完成这次寻亲过程。。。。。。。。

Input

* Line 1: Two space-separated integers: W and H. * Lines 2..?: These lines describe the map, row by row. The first line describes the most northwest part of the map; the last line describes the most southeast part of the map. Successive integers in the input describe columns of the map from west to east. Each new row of a map's description starts on a new input line, and each input line contains no more than 40 space-separated integers. If W <= 40, then each input line describes a complete row of the map. If W > 40, then more than one line is used to describe a single row, 40 integers on each line except potentially the last one. No input line ever describes elements of more than one row. The integers that describe the map come from this set: 0: Square through which Bessie can travel 1: Impassable square that Bessie cannot traverse 2: Bessie's starting location 3: Location of the Knights of Ni 4: Location of a shrubbery

Output

* Line 1: D, the minimum number of days it will take Bessie to reach a shrubbery and bring it to the Knights of Ni.

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

INPUT DETAILS:

Width=8, height=4. Bessie starts on the third row, only a few squares away
from the Knights.

Sample Output

11

OUTPUT DETAILS:

Bessie can move in this pattern to get a shrubbery for the Knights:
N, W, N, S, E, E, N, E, E, S, S. She gets the shrubbery in the northwest
corner and then makes her away around the barriers to the east and then
south to the Knights.

HINT

 

Source

题解:
这题比较有意思,至少经过1次4,所以我们从起点bfs一次,从终点bfs一次,枚举这个4取min即可
类似于次短路的做法
代码:(copy)
 #include<cstdio>
#include<cstring>
struct target{
int x,y;
}t[];
struct queue{
int x,y;
}q[];
const int mx[]={,,,-};
const int my[]={,,-,};
int n,m,cnt,x1,y1,x2,y2,head,tail,ans=;
int map[][];
int dis1[][];
int dis2[][];
bool mrk[][];
inline int min(int a,int b)
{return a<b?a:b;}
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void bfs1(int x,int y)
{
head=;tail=;mrk[x][y]=;
q[].x=x;q[].y=y;
while (head<tail)
{
int nx=q[++head].x,ny=q[head].y;
for (int k=;k<;k++)
{
int xx=nx+mx[k],yy=ny+my[k];
if (xx<||xx>n||yy<||yy>m)continue;
if (mrk[xx][yy]||map[xx][yy]==) continue;
dis1[xx][yy]=dis1[nx][ny]+;
q[++tail].x=xx;q[tail].y=yy;
mrk[xx][yy]=;
}
}
}
inline void bfs2(int x,int y)
{
memset(q,,sizeof(q));
memset(mrk,,sizeof(mrk));
head=;tail=;mrk[x][y]=;
q[].x=x;q[].y=y;
while (head<tail)
{
int nx=q[++head].x,ny=q[head].y;
for (int k=;k<;k++)
{
int xx=nx+mx[k],yy=ny+my[k];
if (xx<||xx>n||yy<||yy>m)continue;
if (mrk[xx][yy]||map[xx][yy]==) continue;
dis2[xx][yy]=dis2[nx][ny]+;
q[++tail].x=xx;q[tail].y=yy;
mrk[xx][yy]=;
}
}
}
int main()
{
m=read();n=read();
for (int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
map[i][j]=read();
if (map[i][j]==)
{
t[++cnt].x=i;
t[cnt].y=j;
}else
if (map[i][j]==)
{
x1=i;
y1=j;
map[i][j]=;
}else
if (map[i][j]==)
{
x2=i;
y2=j;
map[i][j]=;
}
}
bfs1(x1,y1);
bfs2(x2,y2);
for (int i=;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的更多相关文章

  1. POJ3170 Bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士

    1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 281  Solved: 180 ...

  2. bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士

    Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...

  3. 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS

    [Usaco2005 Dec]Knights of Ni 骑士 Description  贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...

  4. 1671: [Usaco2005 Dec]Knights of Ni 骑士

    1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 254  Solved: 163 ...

  5. 【BZOJ】1671: [Usaco2005 Dec]Knights of Ni 骑士(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1671 从骑士bfs一次,然后从人bfs一次即可. #include <cstdio> # ...

  6. 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 ...

  7. [Usaco2005 Dec]Knights of Ni 骑士

    Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...

  8. BZOJ 1671: [Usaco2005 Dec]Knights of Ni 骑士 (bfs)

    题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1671 题解: 按题意分别从贝茜和骑士bfs然后meet_in_middle.. 把一个逗号 ...

  9. 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:由于各种原因而不可通 ...

随机推荐

  1. 查看MySQL数据库的默认编码

    查看MySQL数据库的默认编码 1.使用status命令能够显示数据库的相关系信息,示例如下: mysql> status;————–mysql Ver 14.12 Distrib 5.0.77 ...

  2. erlang mnesia 数据库查询

    Mnesia是一个分布式数据库管理系统,适合于电信和其它需要持续运行和具备软实时特性的Erlang应用,越来越受关注和使用,但是目前Mnesia资料却不多,很多都只有官方的用户指南.下面的内容将着重说 ...

  3. java随机数生成器

    一.java.lang.Math.Random 调用这个Math.Random()函数能够返回带正号的double值,取值范围是[0.0,1.0)的左闭右开区间,并在该范围内(近似)均匀分布. 二.j ...

  4. Spring 基于注解零配置开发

    本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...

  5. OSI七层结构

  6. C#中对于接口的实现方式

    转载: C#中对于接口的实现方式有隐式接口和显式接口两种: 隐式地实现接口成员创建一个接口,IChinese,包含一个成员 Speak;我们创建一个类Speaker,实现接口Chinese //隐藏式 ...

  7. js函数收藏:获取cookie值

    //先设置一段子cookie var d = new Date(); d.setMonth(d.getMonth() + 1); d = d.toGMTString(); var a = " ...

  8. JAXB--学习2

    一.Jaxb处理java对象和xml之间转换常用的annotation有: @XmlType @XmlElement @XmlRootElement @XmlAttribute @XmlAccesso ...

  9. ZOJ 3511 不相交切切多边形 线段树求最大边数

    题意: n多凸边形 m刀 (把n切m刀,问切完后的图形中 最多的边数 是多少) 切a点-b点 数据保证切的刀不会相交 思路: 2点之间的剩余点数就是边数, 把a-b距离 近 排序 切完一刀就统计一下切 ...

  10. yum安装于卸载软件常见命令

    1.使用yum安装和卸载软件,有个前提是yum安装的软件包都是rpm格式的. 安装的命令是,yuminstall ~,yum会查询数据库,有无这一软件包,如果有,则检查其依赖冲突关系,如果没有依赖冲突 ...