欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-15 18:23:49
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 NumberFor...
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;
	}


}


本文地址:https://blog.csdn.net/lanmaoNO1/article/details/109640233

相关标签: 算法 java