1256:献给阿尔吉侬的花束

传送门:http://ybt.ssoier.cn:8088/problem_show.php?pid=1256

【题目描述】

阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫。今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪。现在研究员们想知道,如果阿尔吉侬足够聪明,它最少需要多少时间就能吃到奶酪。

迷宫用一个R×C的字符矩阵来表示。字符S表示阿尔吉侬所在的位置,字符E表示奶酪所在的位置,字符#表示墙壁,字符.表示可以通行。阿尔吉侬在1个单位时间内可以从当前的位置走到它上下左右四个方向上的任意一个位置,但不能走出地图边界。

【输入】

第一行是一个正整数T(1 ≤ T ≤ 10),表示一共有T组数据。

每一组数据的第一行包含了两个用空格分开的正整数R和C(2 ≤ R, C ≤ 200),表示地图是一个R×C的矩阵。

接下来的R行描述了地图的具体内容,每一行包含了C个字符。字符含义如题目描述中所述。保证有且仅有一个S和E。

【输出】

对于每一组数据,输出阿尔吉侬吃到奶酪的最少单位时间。若阿尔吉侬无法吃到奶酪,则输出“oop!”(只输出引号里面的内容,不输出引号)。每组数据的输出结果占一行。

【输入样例】

3
3 4
.S..
###.
..E.
3 4
.S..
.E..
....
3 4
.S..
####
..E.

【输出样例】

5
1
oop!



 1 #include<iostream>
 2 #include<queue>
 3 #include<cstring>
 4 using namespace std; 
 5 #define N 200+1
 6 struct node{
 7     int x,y,t;
 8 };
 9 int r,c,sx,sy,ex,ey,t;
10 char map[N][N];
11 bool maps[N][N];
12 int xs[]={0,0,1,-1};
13 int ys[]={1,-1,0,0};
14 void bfs()
15 {
16     bool ok=true;
17     queue <node> q;
18     node st;
19     st.x=sx;
20     st.y=sy;
21     st.t=0;
22     q.push(st);
23     while(!q.empty())
24     {
25         node s=q.front();q.pop();
26         if(s.x==ex&&s.y==ey)
27         {
28             cout<<s.t<<endl;
29             ok=false;
30             break;
31         }
32         for(int i=0;i<4;i++)
33         {
34             int x=s.x+xs[i];
35             int y=s.y+ys[i];
36             if(maps[x][y]==true&&map[x][y]!='#'&&x>=1&&x<=r&&y>=1&&y<=c)
37             {
38                 node tmp;
39                 tmp.x=x;
40                 tmp.y=y;
41                 tmp.t=s.t+1;
42                 maps[x][y]=false;
43                 q.push(tmp);
44             }
45         }
46     }
47     if(ok){
48         cout<<"oop!"<<endl;
49     }
50     return;
51 }
52 int main()
53 {
54     cin>>t;
55     while(t--)
56     {
57         memset(maps,true,sizeof(maps));
58         cin>>r>>c;
59         for(int i=1;i<=r;i++)
60             for(int j=1;j<=c;j++)
61                 cin>>map[i][j];
62         for(int i=1;i<=r;i++)
63             for(int j=1;j<=c;j++)
64             {
65                 if(map[i][j]=='S')
66                 {
67                     sx=i;
68                     sy=j;
69                 }
70                 if(map[i][j]=='E')
71                 {
72                     ex=i;
73                     ey=j;
74                 }
75             }
76         bfs();
77     }
78 }

猜你喜欢

转载自www.cnblogs.com/jzxnl/p/11138928.html
今日推荐