栅栏迷宫

田野上搭建了一个黄金大神专用的栅栏围成的迷宫。幸运的是,在迷宫的边界上留出了两段栅栏作为迷宫的出口。更幸运的是,所建造的迷宫是一个“完美的”迷宫:即你能从迷宫中的任意一点找到一条走出迷宫的路。给定迷宫的宽W(1<=W<=38)及长H(1<=H<=100)。 2*H+1行,每行2*W+1的字符以下面给出的格式表示一个迷宫。然后计算从迷宫中最“糟糕”的那一个点走出迷宫所需的步数(就是从最“糟糕”的一点,走出迷宫的最少步数)。(即使从这一点以最优的方式走向最靠近的出口,它仍然需要最多的步数)当然了,黄金大神让你必须只会水平或垂直地在X或Y轴上移动。每移动到一个新的方格算作一步(包括移出迷宫的那一步)这是一个W=5,H=3的迷宫: 
+-+-+-+-+-+
|         |
+-+ +-+ + +
|     | | |
+ +-+-+ + +
| |     |  
+-+ +-+-+-+
如上图的例子,栅栏的柱子只出现在奇数行或奇数列。每个迷宫只有两个出口。 
PROGRAM NAME: maze 
INPUT FORMAT: 
(file maze.in) 
第一行: W和H(用空格隔开) 
第二行至第2*H+1行:  每行2*W+1个字符表示迷宫 
OUTPUT FORMAT: 
(file maze.out) 
输出一个单独的整数,表示能保证牛从迷宫中任意一点走出迷宫的最小步数。
SAMPLE INPUT 
5 3
+-+-+-+-+-+
|         |
+-+ +-+ + +
|     | | |
+ +-+-+ + +
| |     |  
+-+ +-+-+-+
SAMPLE OUTPUT 
9

善良的学长:样例输入可以复制进记事本或者文本文档这样看起来更加直观!!!=v=
题目描述

 做了这道题之后,整个人都升华了。

这道题主要考察了两个方面

1)你的代码能力  2)你的读题能力

是一道好题

解法:

主要是宽搜的实现过程

我们从每个出口开始去搜索,得到到每个点的最短距离

然后max{每个点的最短距离}  就是我们的答案

在判断相邻各点之间是否联通的时候,可以分四种情况(上下左右)去判断,然后

在搜索,实现起来会好很多

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<queue>
 6 #include<cmath>
 7 using namespace std;
 8 const int N=1e3+10;
 9 int n,m,fx,fy,rx,ry;
10 char s[N][N],ch;
11 int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
12 //右,左,下,上 
13 queue<int>qx,qy;
14 int d[N][N],ans;
15 bool ok(int sx,int sy,int i)
16 {
17     if(i==0)
18     {
19         if(s[sx*2][sy*2+1]==' ') return 1;
20     }
21     if(i==1)
22     {
23         if(s[sx*2][sy*2-1]==' ') return 1;
24     }
25     if(i==2)
26     {
27         if(s[sx*2+1][sy*2]==' ') return 1;
28     }
29     if(i==3)
30     {
31         if(s[sx*2-1][sy*2]==' ') return 1;
32     }
33     return 0;
34 }
35 void bfs(int sx,int sy)
36 {
37     qx.push(sx);qy.push(sy);
38     d[sx][sy]=1;
39     while(!qx.empty())
40     {
41         fx=qx.front();qx.pop();
42         fy=qy.front();qy.pop();
43 //        cout<<"op "<<fx<<" "<<fy<<endl;
44         for(int i=0;i<=3;++i)
45         {
46             rx=fx+dx[i];ry=fy+dy[i];
47             if(rx<1 || rx>n || ry<1 || ry>m) continue;
48             if(d[rx][ry]<=d[fx][fy]+1) continue;
49             if(ok(fx,fy,i))
50             {
51                 qx.push(rx);qy.push(ry);
52                 d[rx][ry]=d[fx][fy]+1;
53             }
54         } 
55     }
56 }
57 int main()
58 {
59     freopen("maze.in","r",stdin);
60     freopen("maze.out","w",stdout);
61     scanf("%d%d",&m,&n);getchar();
62     for(int i=1;i<=n*2+1;++i)
63     {
64         for(int j=1;j<=m*2+1;++j) s[i][j]=getchar(); 
65         getchar();
66     }
67     memset(d,34,sizeof(d));
68     for(int i=1;i<=n*2+1;++i)
69      for(int j=1;j<=m*2+1;++j)
70      {
71          if(i==1 && s[i][j]==' ') bfs(1,(j+1)/2);
72          if(i==n*2+1 && s[i][j]==' ') bfs(n,(j+1)/2);
73          if(j==1 && s[i][j]==' ') bfs((i+1)/2,1);
74         if(j==m*2+1 && s[i][j]==' ') bfs((i+1)/2,m);
75      }
76     for(int i=1;i<=n;++i)
77      for(int j=1;j<=m;++j)
78       ans=max(ans,d[i][j]);
79     printf("%d",ans); 
80     return 0;
81 } 
View Code

猜你喜欢

转载自www.cnblogs.com/adelalove/p/9101893.html