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. hdu-3790最短路径问题

    Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的.   Inp ...

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

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

  3. java.lang.String小测试

    还记得java.lang.String么,如果现在给你一个小程序,你能说出它的结果么 public static String ab(String a){ return a + "b&quo ...

  4. [深入React] 3.JSX的神秘面纱

    <div> <List /> </div> 会被编译为: React.createElement("div",null, React.creat ...

  5. Python 获取Facebook用户的Friends的爱好中的Top10

    CODE; #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-12 @author: guaguastd @name: f ...

  6. HTML5硕士学习笔记

    如今,该集团经过培训的同事给大家HTML5,他出席了两个5训练日,大概过一次给我们,在一个很形象.同事们更感兴趣的是. 课后共享所有的课件.在热情的新技术,我想工作有一个良好的早晨,我决定重新学习课件 ...

  7. android中正确保存view的状态

    英文原文: http://trickyandroid.com/saving-android-view-state-correctly/ 转载此译文须注明出处. 今天我们聊一聊安卓中保存和恢复view状 ...

  8. VritualBox 中Debian安装tool

    VritualBox 中Debian安装tool 环境 Debian 8 VirtualBox 配置Debian的源 #163源 deb http://mirrors.163.com/debian/ ...

  9. C#、.NET和ASP.NET三者之间的区别

    刚毕业后出去找工作面试的时候就遇到这个问题!.回答不上来.回来网上查的如下: 那么 .NET.C#和ASP.NET这三者之间区别不清楚,到底它们之间有什么联系呢? 1..NET是一个平台,一个抽象的平 ...

  10. ISO3834认证所需的部分标准

    SO9606-1 焊工考试——熔化焊——第一部分:钢 ISO9606-2 焊工考试——熔化焊——第二部分:铝及铝合金 ISO14732 焊接人员——金属材料全机械化及自动化焊接的熔化焊操作攻击电阻焊安 ...