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)
  1. #include<cstdio>
  2. #include<cstring>
  3. struct target{
  4. int x,y;
  5. }t[];
  6. struct queue{
  7. int x,y;
  8. }q[];
  9. const int mx[]={,,,-};
  10. const int my[]={,,-,};
  11. int n,m,cnt,x1,y1,x2,y2,head,tail,ans=;
  12. int map[][];
  13. int dis1[][];
  14. int dis2[][];
  15. bool mrk[][];
  16. inline int min(int a,int b)
  17. {return a<b?a:b;}
  18. inline int read()
  19. {
  20. int x=,f=;char ch=getchar();
  21. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  22. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  23. return x*f;
  24. }
  25. inline void bfs1(int x,int y)
  26. {
  27. head=;tail=;mrk[x][y]=;
  28. q[].x=x;q[].y=y;
  29. while (head<tail)
  30. {
  31. int nx=q[++head].x,ny=q[head].y;
  32. for (int k=;k<;k++)
  33. {
  34. int xx=nx+mx[k],yy=ny+my[k];
  35. if (xx<||xx>n||yy<||yy>m)continue;
  36. if (mrk[xx][yy]||map[xx][yy]==) continue;
  37. dis1[xx][yy]=dis1[nx][ny]+;
  38. q[++tail].x=xx;q[tail].y=yy;
  39. mrk[xx][yy]=;
  40. }
  41. }
  42. }
  43. inline void bfs2(int x,int y)
  44. {
  45. memset(q,,sizeof(q));
  46. memset(mrk,,sizeof(mrk));
  47. head=;tail=;mrk[x][y]=;
  48. q[].x=x;q[].y=y;
  49. while (head<tail)
  50. {
  51. int nx=q[++head].x,ny=q[head].y;
  52. for (int k=;k<;k++)
  53. {
  54. int xx=nx+mx[k],yy=ny+my[k];
  55. if (xx<||xx>n||yy<||yy>m)continue;
  56. if (mrk[xx][yy]||map[xx][yy]==) continue;
  57. dis2[xx][yy]=dis2[nx][ny]+;
  58. q[++tail].x=xx;q[tail].y=yy;
  59. mrk[xx][yy]=;
  60. }
  61. }
  62. }
  63. int main()
  64. {
  65. m=read();n=read();
  66. for (int i=;i<=n;i++)
  67. for(int j=;j<=m;j++)
  68. {
  69. map[i][j]=read();
  70. if (map[i][j]==)
  71. {
  72. t[++cnt].x=i;
  73. t[cnt].y=j;
  74. }else
  75. if (map[i][j]==)
  76. {
  77. x1=i;
  78. y1=j;
  79. map[i][j]=;
  80. }else
  81. if (map[i][j]==)
  82. {
  83. x2=i;
  84. y2=j;
  85. map[i][j]=;
  86. }
  87. }
  88. bfs1(x1,y1);
  89. bfs2(x2,y2);
  90. for (int i=;i<=cnt;i++)
  91. {
  92. int nx=t[i].x,ny=t[i].y;
  93. if (!(dis1[nx][ny]+dis2[nx][ny]))continue;
  94. ans=min(ans,dis1[nx][ny]+dis2[nx][ny]);
  95. }
  96. printf("%d",ans);
  97. }

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. Junit 学习

    一. 断言核心方法 示例代码: package com.test; import org.junit.Assert; import org.junit.Test; /** * @Title: test ...

  2. android与javascript相互调用

    下面这一节来介绍android和javascript是怎么相互调用的,这样我们的UI界面设计起来就简单多了,而且UI设计起来也可以跨平台.现在有好多web app前台框架了,比如sencha和jque ...

  3. 我对Laravel ThinkPHP Yii symfony2 CI cakephp 的看法

    这是我的真心体会,在尝试使用Laravel.ThinkPHP.Yii.symfony2.CI.cakephp.Yii2 之后的真实想法(default7#zbphp.com). 1)ThinkPHP ...

  4. EA+svn实现UML的版本号控制

    一.安装软件 1.VisualSvn Server svnserver 2.Tortoise Svn svnclient 3.Slik-Subversion-1.7.8-x64版本号控制插件 4.En ...

  5. 关于 视频同步vsync 信号在不同一时候钟域採样问题

    今天调试 视频 4k(3840 x 1920)的vsync信号(时钟为 297Mhz) 进入 170Mhz 的时钟域, 发现输出来的信号信号抖动特别厉害.后来才发现这是不同一时候钟域 造成的影响. 快 ...

  6. Java基础知识强化36:StringBuffer类之StringBuffer的概述

    1. StringBuffer类概述: (1)String的缺陷: 我们如果对字符串进行拼接操作,每次拼接,都会构造一个新的String对象,既耗时,又浪费空间.如下图: (2)StringBuffe ...

  7. JavaScripts学习日记——DOM

    DOM Document Object Model 文档对象模型  整合js和html css.控制html文档行为.DOM就是把页面当中所有内容全部封装成对象.HTML文档中万物皆对象.1.对象的分 ...

  8. netstat 命令详解

    netstat命令是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表.实际的网络连接以及每一个网络接口设备的状态信息,在我的计算机上执行netstat后,其输出结果为:netstat命令是一 ...

  9. linux stat命令

    在Linux中,文件没有“创建时间”这个说法.Linux中的文件的时间属性只有三个:atime(Access time).mtime(Modified time).ctime(Change time) ...

  10. 重学《C#高级编程》(序)

    小生码农一枚,以前只是看别人写博客,从来没有想过要自己写博文,突然之间“脑抽”想自己也写点什么,遂在博客园开通这个博客. 简单介绍下自己吧,本人90后,父母对我没有大的想法,只是希望我平安成长,多学习 ...