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. poj 3182 The Grove bfs

    思路:如果要围绕一圈,必须经过一条竖线上的一点,把竖线左端封住,bfs一次,枚举点,再把竖线右端封住,再bfs回起点. #include <iostream> #include <c ...

  2. C# Tips:获得当前登录计算机的用户(本地用户/域用户)

    须要using的namespace: using System.Security.Principal; 获得登录计算机的用户: WindowsIdentity windowsIdentity = Wi ...

  3. somethings about QSplitter

    m_splitter = new QSplitter(Qt::Horizontal);    m_splitter->addWidget(this->m_leftWidget);    m ...

  4. 关于driver_register做了些什么

    现在进入driver_register()函数去看看.在driver_register() 中,调用了driver_find(drv->name, drv->bus)函数,这里是干啥呢?这 ...

  5. linux/unix运行级别

    在SYSTEM V 风格的UNIX系统中,系统被分为不同的运行级别,这和BSD分支的UNIX有所不同,常用的为0~6七个级别:0关机 1单用户 2不带网络的多用户 3带网络的多用户 4保留,用户可以自 ...

  6. .NET基础拾遗(7)多线程开发基础3

    一.如何使用异步模式? 异步模式是在处理流类型时经常采用的一种方式,其应用的领域相当广阔,包括读写文件.网络传输.读写数据库,甚至可以采用异步模式来做任何计算工作.相对于手动编写线程代码,异步模式是一 ...

  7. CodeFirst解决数据迁移问题

    CodeFirst解决数据迁移问题 分类: 数据库 设计模式 c#2013-04-24 17:56 137人阅读 评论(0) 收藏 举报 工程用的MVC + Entity Framework,根据Co ...

  8. (转)C#中的委托,匿名方法和Lambda表达式

    简介 在.NET中,委托,匿名方法和Lambda表达式很容易发生混淆.我想下面的代码能证实这点.下面哪一个First会被编译?哪一个会返回我们需要的结果?即Customer.ID=5.答案是6个Fir ...

  9. 新Android学习计划

    最近,在学习Android Design Support Library提供的新控件过程中,我感受到了原来的学习方式的缺点: 学习内容过于随意,在工作过程中碰到的新问题都想去掌握,心血来潮就想写一篇相 ...

  10. Sql时间函数

    一.sql server日期时间函数 Sql Server中的日期与时间函数  1.  当前系统日期.时间   select getdate()    2. dateadd  在向指定日期加上一段时间 ...