我们的征途是星辰大海 蓝桥杯 Java组

我们的征途是星辰大海:题目


最新的火星探测机器人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!


心得:

这是一道对于算法比赛非常友好的题目,可以训练学生们对二维数据坐标的掌握能力。对后面理解深度搜索、广度搜索、动态规划都有非常大的帮助。所有一定要好好的练一练。


import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        String[]shabi=new String[999];
        int scount=0;
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        for (int i = 0; i < a; i++) {
            int b = sc.nextInt();
            char[][] bb = new char[b][b];
            for (int j = 0; j < b; j++) {
                bb[j] = sc.next().toCharArray();

            }

            int c = sc.nextInt();
            String[] cc = new String[c];
            for (int j = 0; j < cc.length; j++) {
                cc[j] = sc.next();
            }
            char[][] ccc = new char[c][1000];

            for (int j = 0; j < c; j++) {
                for (int j2 = 0; j2 < cc[j].length(); j2++) {
                    ccc[j][j2] = cc[j].charAt(j2);
                }
            }

            za: for (int j = 0; j < c; j++) {
                int h = 0;
                int s = 0;
                zb: for (int j2 = 0; j2 < 1000; j2++) {
                    zc: for (int k = 0; k < b; k++) {
                        for (int k2 = 0; k2 < b; k2++) {
                            if (j2 == 0 && bb[k][k2] == 'S') {
                                h = k2;
                                s = k;
                                // shabi[scount++]=(k+"  "+k2);
                                break zc;
                            }
                        }
                    }
                    if (ccc[j][j2] >= 'A' & ccc[j][j2] <= 'Z') {
                        if (ccc[j][j2] == 'L') {
                            h--;
                            if (s < 0 | s == b | h < 0 | h == b) {
                                shabi[scount++]="I am out!";
                                break zb;
                            } else if (bb[s][h] == '#') {
                                shabi[scount++]="I am dizzy!";
                                break zb;
                            } else if (bb[s][h] == 'T') {
                                shabi[scount++]="I get there!";
                                break zb;
                            }
                        } else if (ccc[j][j2] == 'R') {
                            h++;
                            if (s < 0 | s == b | h < 0 | h == b) {
                                shabi[scount++]="I am out!";
                                break zb;
                            } else if (bb[s][h] == '#') {
                                shabi[scount++]="I am dizzy!";
                                break zb;
                            } else if (bb[s][h] == 'T') {
                                shabi[scount++]="I get there!";
                                break zb;
                            }
                        } else if (ccc[j][j2] == 'U') {
                            s--;
                            if (s < 0 | s == b | h < 0 | h == b) {
                                shabi[scount++]="I am out!";
                                break zb;
                            } else if (bb[s][h] == '#') {
                                shabi[scount++]="I am dizzy!";
                                break zb;
                            } else if (bb[s][h] == 'T') {
                                shabi[scount++]="I get there!";
                                break zb;
                            }
                        } else if (ccc[j][j2] == 'D') {
                            s++;
                            if (s < 0 | s == b | h < 0 | h == b) {
                                shabi[scount++]="I am out!";
                                break zb;
                            } else if (bb[s][h] == '#') {
                                shabi[scount++]="I am dizzy!";
                                break zb;
                            } else if (bb[s][h] == 'T') {
                                shabi[scount++]="I get there!";
                                break zb;
                            }
                        }

                    } else {
                        shabi[scount++]="I have no idea!";
                        break zb;
                    }

                }
            }

        }
        
        
        
        for (int i = 0; i <scount; i++) {
            System.out.println(shabi[i]);
        }
    }

}

 

上一篇:python3实现配置文件差异对比脚本


下一篇:python常用算法(6)——贪心算法,欧几里得算法