BZOJ1671: [Usaco2005 Dec]Knights of Ni

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

 

posted @ 2014-09-21 00:59  ZYF-ZYF  Views(163)  Comments(0Edit  收藏  举报