【USACO 2.4.2】穿越栅栏

【描述】

农夫John在外面的田野上搭建了一个巨大的用栅栏围成的迷宫。幸运的是,他在迷宫的边界上留出了两段栅栏作为迷宫的出口。更幸运的是,他所建造的迷宫是一个“完美的”迷宫:即你能从迷宫中的任意一点找到一条走出迷宫的路。给定迷宫的宽度W(1<=W<=38)及高度H(1<=H<=100)。 2*H+1行,每行2*W+1的字符以下面给出的格式表示一个迷宫。然后计算从迷宫中最“糟糕”的那一个点走出迷宫所需的步数(就是从最“糟糕”的一点,走出迷宫的最少步数)。(即使从这一点以最优的方式走向最靠近的出口,它仍然需要最多的步数)当然了,牛们只会水平或垂直地在X或Y轴上移动,他们从来不走对角线。每移动到一个新的方格算作一步(包括移出迷宫的那一步)这是一个W=5,H=3的迷宫:

+-+-+-+-+-+
|         |
+-+ +-+ + +
|     | | |
+ +-+-+ + +
| |     |  
+-+ +-+-+-+

如上图的例子,栅栏的柱子只出现在奇数行或奇数列。每个迷宫只有两个出口。

【格式】

PROGRAM NAME: maze1

INPUT FORMAT:

(file maze1.in)

第一行: W和H(用空格隔开) 
第二行至第2 * H + 1行:  每行2 * W + 1个字符表示迷宫 

OUTPUT FORMAT:

(file maze1.out)

输出一个单独的整数,表示能保证牛从迷宫中任意一点走出迷宫的最小步数。

【分析】

直接上BFS了。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <algorithm>
 5 #include <cstring>
 6 #include <queue>
 7 const int maxh=2000;
 8 const int INF=1000000;
 9 using namespace std;
10 struct node
11 {
12        int x,y;//坐标
13        int step;//步数 
14 }Exit[17];
15 int map[maxh][maxh],w,h;
16 int dx[]={1,-1,0,0},dy[]={0,0,1,-1};//方向
17 int low[maxh][maxh];
18 
19 void init();
20 void bfs(int num);//出口编号 
21 
22 int main()
23 {
24     //文件操作
25     freopen("maze1.in","r",stdin);
26     freopen("maze1.out","w",stdout);
27     init();
28     //printf("%d %d
",Exit[0].x,Exit[0].y);
29     //printf("%d %d",Exit[1].x,Exit[1].y);
30     bfs(0);bfs(1);//分别从两个出口广搜 
31     int ans=0;
32     for (int i=1;i<=2*h+1;i++)
33     for (int j=1;j<=2*w+1;j++)
34     if (low[i][j]!=INF) ans=max(ans,low[i][j]);
35     printf("%d",ans);
36     return 0;
37 }
38 void init()
39 {
40     int point=0,i,j;
41     memset(map,0,sizeof(map));
42     memset(low,0,sizeof(low));
43     scanf("%d%d",&w,&h);
44     for (i=1;i<=2*h+1;i++)
45     {
46         getchar();//去除换行符 
47         for (j=1;j<=2*w+1;j++)
48         {
49             char temp;
50             scanf("%c",&temp);
51             map[i][j]=;
52             low[i][j]=INF;//初始化 
53             //找出口 
54             if ((i==1 || i==(2*h+1) || j==1 || j==(2*w+1)) && map[i][j]==1)
55             {
56                 Exit[point].x=i;if (i==1) Exit[point].x++;else if (i==2*h+1) Exit[point].x--;
57                 Exit[point].y=j;if (j==1) Exit[point].y++;else if (j==2*w+1) Exit[point].y--;
58                 Exit[point++].step=1;
59             }
60         }
61     }
62 }
63 void bfs(int num)
64 {
65      int i;
66      queue<node>Q;
67      while (!Q.empty()) Q.pop();
68      Q.push(Exit[num]);
69      low[Exit[num].x][Exit[num].y]=1;
70      while (!Q.empty())
71      {
72            node u=Q.front();Q.pop();
73            for (i=0;i<4;i++)
74            {
75                node v;
76                v.x=u.x+dx[i];v.y=u.y+dy[i];
77                v.step=u.step+1;
78                if (map[v.x][v.y]==0) continue;
79                v.x+=dx[i];v.y+=dy[i];//跨步 
80                if (v.step<low[v.x][v.y]) 
81                {
82                    low[v.x][v.y]=v.step;
83                    Q.push(v);
84                }
85            }
86      }
87 }
原文地址:https://www.cnblogs.com/hoskey/p/3801535.html