演算法提高 我們的征途是星辰大海

藍帽NO1發表於2020-11-12
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Main {
	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;
	}


}


相關文章