付永康


私信TA

用户名:dotcpp0605582

访问量:811

签 名:

等  级
排  名 3367
经  验 1872
参赛次数 1
文章发表 13
年  龄 0
在职情况 学生
学  校 鄂州职业大学
专  业

  自我简介:

TA的其他文章

解题思路:

注意事项:

参考代码:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TT1522 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        for (int i = 0; i < a; i++) {
            int b = sc.nextInt();
            int x = 0,y = 0,v = 0,l = 0;
            //创建地图
            String[][] arr1 = new String[b][b];
            for (int j = 0; j < b; j++) {
                String str1 = sc.next();
                for (int k = 0; k < str1.length(); k++) {
                    if ((str1.charAt(k) + "").equals("S")) {
                        v = j;
                        l = k;
                    }
                    arr1[j][k] = str1.charAt(k) + "";
                }
            }

            int c = sc.nextInt();
            List<String> list = new ArrayList<>(); //list集合用来接收Q发送的命令
            for (int j = 0; j < c; j++) {
                x = v;y = l; //每组数据循环完之后要初始化curiosity的起始位置
                String str2 = sc.next(); //用字符串接收当前这组的命令
//                list = new ArrayList<String>(); //完成上一组命令后重置list集合准备接收下一组命令
                //然后将这组命令添加到list集合中
                for (int k = 0; k < str2.length(); k++) {
                    list.add(str2.charAt(k) + "");
                }
                int index = 0; //index用来记录完成命令后是否到达了终点
                for (int k = 0; k < list.size(); k++) {
                    index++;
                    if (list.get(k).equals("U")) {
                        x--; //因为数组是已[x][y]记录的,所以往上走是x--
                        try {
                            if (arr1[x][y].equals("#")) {
                                System.out.println("I am dizzy!");
                                list.clear(); //碰到障碍物之后需要清空list集合,不然下一层for循环会继续接收list元素的下一条命令
                                              //也是当前list结束的条件
                            } else if (arr1[x][y].equals("T")) {
                                System.out.println("I get there!");
                                list.clear(); //到达终点后也是同理
                            } else if (index == list.size() && (arr1[x][y].equals(".") || arr1[x][y].equals("S"))) {
                                System.out.println("I have no idea!");
                                list.clear(); //index == list.size()表示指令已经接收完毕但是没有碰到障碍物或者没有达到终点
                                              //此时curiosity所处的位置可能为"."或者"S"
                            }
                        } catch (IndexOutOfBoundsException e) {
                            System.out.println("I am out!");
                            list.clear(); //用try catch IndexOutOfBoundsException处理越界的异常
                        }
                        //以下代码皆为同理
                    } else if (list.get(k).equals("D")) {
                        x++; //因为数组是已[x][y]记录的,所以往下走是x++
                        try {
                            if (arr1[x][y].equals("#")) {
                                System.out.println("I am dizzy!");
                                list.clear();
                            } else if (arr1[x][y].equals("T")) {
                                System.out.println("I get there!");
                                list.clear();
                            } else if (index == list.size() && arr1[x][y].equals(".")) {
                                System.out.println("I have no idea!");
                                list.clear();
                            }
                        } catch (IndexOutOfBoundsException e) {
                            System.out.println("I am out!");
                            list.clear();
                        }
                    } else if (list.get(k).equals("L")) {
                        y--; //因为数组是已[x][y]记录的,所以往左走是y--
                        try {
                            if (arr1[x][y].equals("#")) {
                                System.out.println("I am dizzy!");
                                list.clear();
                            } else if (arr1[x][y].equals("T")) {
                                System.out.println("I get there!");
                                list.clear();
                            } else if (index == list.size() && (arr1[x][y].equals(".") || arr1[x][y].equals("S"))) {
                                System.out.println("I have no idea!");
                                list.clear();
                            }
                        } catch (IndexOutOfBoundsException e) {
                            System.out.println("I am out!");
                            list.clear();
                        }
                    } else {
                        y++; //因为数组是已[x][y]记录的,所以往上走是y++
                        try {
                            if (arr1[x][y].equals("#")) {
                                System.out.println("I am dizzy!");
                                list.clear();
                            } else if (arr1[x][y].equals("T")) {
                                System.out.println("I get there!");
                                list.clear();
                            } else if (index == list.size() && (arr1[x][y].equals(".") || arr1[x][y].equals("S"))) {
                                System.out.println("I have no idea!");
                                list.clear();
                            }
                        } catch (IndexOutOfBoundsException e) {
                            System.out.println("I am out!");
                            list.clear();
                        }
                    }
                }
            }
        }
    }
}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区