解题思路:
注意事项:
参考代码:
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 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复