POJ3170 Bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士
1671: [Usaco2005 Dec]Knights of Ni 骑士
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 281 Solved: 180
[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.
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行,离骑士们不远.
贝茜可以按这样的路线完成骑士的任务:北,西,北,南,东,东,北,东,东,南,南.她在森林的西北角得到一株她需要的灌木,然后绕过障碍把它交给在东南方的骑士.
从起点和终点各进行一次SPFA,记录各自到达每个灌木处的距离,之后枚举每个灌木位置,求其到起点和终点的最小距离和
1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<queue> 8 using namespace std; 9 const int mxn=1000; 10 int w,h; 11 int sx1,sy1,sx2,sy2,tx,ty; 12 int tar[mxn][3],cnt; 13 int mp[mxn][mxn]; 14 int mx[5]={0,1,0,-1,0}, 15 my[5]={0,0,1,0,-1}; 16 int dis[mxn][mxn][2]; 17 bool vis[mxn][mxn]; 18 queue<pair<int,int> >q; 19 void BFS(int sx,int sy,int mode){ 20 dis[sx][sy][mode]=0; 21 vis[sx][sy]=1; 22 q.push(pair<int,int>(sx,sy)); 23 while(!q.empty()){ 24 int x=q.front().first,y=q.front().second; 25 q.pop(); 26 for(int i=1;i<=4;i++){ 27 int nx=x+mx[i],ny=y+my[i]; 28 if(nx>0 && nx<=h && ny>0 && ny<=w) 29 if(!vis[nx][ny] && (mp[nx][ny]==0 || mp[nx][ny]==4)){ 30 dis[nx][ny][mode]=dis[x][y][mode]+1; 31 vis[nx][ny]=1; 32 q.push(pair<int,int>(nx,ny)); 33 } 34 } 35 } 36 } 37 int main(){ 38 scanf("%d%d",&w,&h); 39 int i,j; 40 for(i=1;i<=h;i++) 41 for(j=1;j<=w;j++){ 42 scanf("%d",&mp[i][j]); 43 if(mp[i][j]==2)sx1=i,sy1=j; 44 if(mp[i][j]==3)sx2=i,sy2=j; 45 if(mp[i][j]==4)tar[++cnt][1]=i,tar[cnt][2]=j; 46 } 47 BFS(sx1,sy1,0); 48 memset(vis,0,sizeof vis); 49 BFS(sx2,sy2,1); 50 int ans=0x5fffff; 51 for(i=1;i<=cnt;i++){ 52 if(dis[tar[i][1]][tar[i][2]][1]!=0 && dis[tar[i][1]][tar[i][2]][0]!=0) 53 ans=min(ans,dis[tar[i][1]][tar[i][2]][1]+dis[tar[i][1]][tar[i][2]][0]); 54 } 55 printf("%d\n",ans); 56 return 0; 57 }