「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行,离骑士们不远.
贝茜可以按这样的路线完成骑士的任务:北,西,北,南,东,东,北,东,东,南,南.她在森林的西北角得到一株她需要的灌木,然后绕过障碍把它交给在东南方的骑士.
题解
BZOJ的翻译错了。
总之就是在地图上从2出发,走到3,途中要至少经过一个4,需要注意的是在经过4之前不能经过3。
这就很明显的BFS嘛,2为起点一次,3为起点一次,最后枚举每个4,计算路程,取最小。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
#include<cstdio> #include<cstring> #define inf 0x7fffffff int bx2,by2,bx3,by3,qx[500001],qy[500001],mp[1001][1001]; int xx[4]={1,-1,0,0},yy[4]={0,0,1,-1}; int n,m,ans; int d1[1001][1001],d2[1001][1001]; void bfs1() { int t=0,w=1; qx[0]=bx2;qy[0]=by2;d1[bx2][by2]=0; while(t<w) { int x=qx[t],y=qy[t]; for(int k=0;k<4;k++) { int nowx=x+xx[k],nowy=y+yy[k]; if(nowx<1||nowy<1||nowx>n||nowy>m||mp[nowx][nowy]==1||d1[nowx][nowy]!=-1)continue; qx[w]=nowx;qy[w]=nowy;d1[nowx][nowy]=d1[x][y]+1; w++; } t++; } } void bfs2() { int t=0,w=1; qx[0]=bx3;qy[0]=by3;d2[bx3][by3]=0; while(t<w) { int x=qx[t],y=qy[t]; for(int k=0;k<4;k++) { int nowx=x+xx[k],nowy=y+yy[k]; if(nowx<1||nowy<1||nowx>n||nowy>m||mp[nowx][nowy]==1||d2[nowx][nowy]!=-1)continue; qx[w]=nowx;qy[w]=nowy;d2[nowx][nowy]=d2[x][y]+1; w++; } t++; } } void getans() { ans=inf; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { if(d1[i][j]==-1||d2[i][j]==-1||mp[i][j]!=4)continue; if(d1[i][j]+d2[i][j]<ans)ans=d1[i][j]+d2[i][j]; } } int main() { scanf("%d%d",&m,&n); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { d1[i][j]=d2[i][j]=-1; scanf("%d",&mp[i][j]); if(mp[i][j]==2){bx2=i;by2=j;} if(mp[i][j]==3){bx3=i;by3=j;} } bfs1(); bfs2(); getans(); printf("%d",ans); return 0; } |