蓝桥杯 我们的征途是星辰大海 模拟

问题描述
最新的火星探测机器人curiosity被困在了一个二维迷宫里,迷宫由一个个方格组成。
  共有四种方格:
  ‘.’ 代表空地,curiosity可以穿过它
  ‘#’ 代表障碍物,不可穿越,不可停留
  ‘S’ 代表curiosity的起始位置
  ‘T’ 代表curiosity的目的地
  NASA将会发送一系列的命令给curiosity,格式如下:“LRUD”分别代表向左,向右,向上,向下走一步。由于地球和火星之间最近时也有55000000km!所以我们必须提前判断这一系列的指令会让curiosity最终处在什么样的状态,请编程完成它。
输入格式
  第一行是一个整数T,代表有几个测试样例
  每个测试样例第一行是一个整数N(1<=N<=50))代表迷宫的大小(N*N)。随后的N行每行由N个字符串组成,代表迷宫。接下来的一行是一个整数Q,代表有多少次询问,接下来的Q行每行是一个仅由“LRUD”四个字母的组成的字符串,字符转长度小于1000.
输出格式
  对于每个询问输出单独的一行:
  “I get there!”:执行给出的命令后curiosity最终到达了终点。
  “I have no idea!”:执行给出的命令后curiosity未能到达终点。
  “I am dizzy!”:curiosity在执行命令的过程中撞到了障碍物。
  “I am out!”:代表curiosity在执行命令的过程中走出了迷宫的边界。
Sample Input 
  2
  2
  S.
  #T
  2
  RD
  DR
  3
  S.#
  .#.
  .T#
  3
  RL
  DDD
  DDRR
Sample Output
  I get there!
  I am dizzy!
  I have no idea!
  I am out!
  I get there!
猛男落泪,我做不出来。
转载自https://blog.dotcpp.com/a/62942
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 char mp[60][60];
 4 int main() {
 5     int t;
 6     cin >> t;
 7     while (t--) {
 8         int n;
 9         cin >> n;
10         int sx, sy; //S的坐标
11         for (int i = 0; i < n; i++) {
12             for (int j = 0; j < n; j++) {
13                 cin >> mp[i][j];
14                 if (mp[i][j] == 'S') {
15                     sx = i;
16                     sy = j;
17                 }
18             }
19         }
20         int q;
21         cin >> q;
22         while (q--) {
23             bool flag = false;
24             string s;
25             cin >> s;
26             int nowx = sx;
27             int nowy = sy;
28             for (int i = 0; i < s.length(); i++) {
29                 switch (s[i]) {
30                     case 'L':
31                         nowy--;
32                         break;
33                     case 'R':
34                         nowy++;
35                         break;
36                     case 'U':
37                         nowx--;
38                         break;
39                     case 'D':
40                         nowx++;
41                         break;
42                 }
43                 if (mp[nowx][nowy] == '#') {
44                     cout << "I am dizzy!" << endl;
45                     flag = true;
46                     break;
47                 } else if (nowx < 0 || nowx >= n || nowy < 0 || nowy >= n) {
48                     cout << "I am out!" << endl;
49                     flag = true;
50                     break;
51                 } else if (mp[nowx][nowy] == 'T') {
52                     cout << "I get there!" << endl;
53                     flag = true;
54                     break;
55                 }
56             }
57             if (!flag){
58                 cout << "I have no idea!" << endl;
59             }
60         }
61     }
62     return 0;
63 }

猜你喜欢

转载自www.cnblogs.com/fx1998/p/12724316.html