Java实现 蓝桥杯VIP 算法提高 我们的征途是星辰大海

算法提高 我们的征途是星辰大海
时间限制:1.0s 内存限制:256.0MB
  最新的火星探测机器人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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class 我们的征途是星辰大海 {
	static int maxn = 50;
	static char[][] maze =new char[maxn][maxn];
	static int N,Q;
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new  InputStreamReader(System.in));
		String[] ans = {"I get there!","I have no idea!","I am dizzy!","I am out!"};
		int t = Integer.parseInt(br.readLine());
		int sx=0,sy=0,tx=0,ty=0;
		while(t>0)
		{
			N=Integer.parseInt(br.readLine());
			//System.out.println("n的值"+N);
			for(int i=0;i<N;i++)
			{
				char[] ch = br.readLine().toCharArray();
				for(int j =0;j<N;j++)
				{
					maze[i][j]=ch[j];
					//System.out.print(maze[i][j]);
					if (maze[i][j] == 'S')  {sx = i; sy = j;}
					if (maze[i][j] == 'T')  {tx = i; ty = j;}
				}
			}
			Q=Integer.parseInt(br.readLine());
			//System.out.println("q的值为"+Q);
			for(int i=0;i<Q;i++)
			{
				String op=br.readLine();
				int res=solve(op,sx,sy,tx,ty);
				System.out.println(ans[res]);
			}
			t--;
		}
	}
	private static int solve(String op, int sx, int sy, int tx, int ty) {
		char[] ch1 = op.toCharArray();
		if(sx==tx&&sy==ty)
			return 0;
		for( int i = 0;i < ch1.length; i++)
		{
			if(ch1[i] == 'L')  sy -= 1;
			else if(ch1[i] == 'R') sy += 1;
			else if(ch1[i] == 'U') sx -= 1;
			else sx += 1;
		
			if( sx < 0 || sy < 0 || sx >= N || sy >= N) 
				return 3;
			if( maze[sx][sy] == '#') 
				return 2;
			if(sx == tx && sy == ty)
				return 0;
		}
		return 1;
	}


}

原文地址:https://www.cnblogs.com/a1439775520/p/12948409.html