Our journey is the sea of stars: topic
The latest Mars exploration robot curiosity is trapped in a two-dimensional maze composed of squares.
There are four kinds of squares:
‘.’ Represents the open space, and curiosity can pass through it
'#' stands for obstacles, which cannot be crossed or stayed
'S' represents the starting position of curiosity
'T 'represents the destination of curiosity
NASA will send a series of commands to curiosity in the following format: "LRUD" represents one step to the left, right, up and down respectively. Because there are 55 million km between earth and Mars recently! Therefore, we must judge in advance what kind of state this series of instructions will make the curiosity in. Please complete it by programming.
Input format
The first line is an integer T, representing several test samples
The first line of each test sample is an integer n (1 < = n < = 50)) representing the size of the maze (N*N). The next N lines, each composed of N strings, represent the maze. The next line is an integer Q, which represents the number of queries. Each line of the next q is a string composed of only four letters of "LRUD", and the character conversion length is less than 1000
Output format
Output a separate line for each query:
“I get there!”: After executing the given command, the curiosity finally reaches the end point.
“I have no idea!”: The curiosity failed to reach the destination after executing the given command.
“I am dizzy!”: curiosity hit an obstacle while executing the command.
“I am out!”: On behalf of curiosity, he walked out of the boundary of the maze in the process of executing the command.
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!
Experience:
This is a very friendly topic for algorithm competition, which can train students' ability to master two-dimensional data coordinates. It is very helpful for understanding deep search, wide search and dynamic planning. All must practice well.
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]); } } }