java 0分,全部测试点RE,下载用例跑完正确,何解
查看原帖
java 0分,全部测试点RE,下载用例跑完正确,何解
915148
XieLee楼主2023/2/14 17:17
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayDeque;
import java.util.Queue;

public class Main {
	static BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer st =new StreamTokenizer(bf);
	static int X;
	static int Y;	
	static PrintWriter pw =new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
	static char[][]a;
	static boolean[][]vis;
	static int []dx= {1,0,-1,0};
	static int []dy= {0,1,0,-1};
	static int sx,sy;
	static Queue<Node>que=new ArrayDeque<>();
    public static void main(String[] args) throws IOException {
    	X=nextInt();
    	Y=nextInt();
    	a=new char[X][Y];
    	vis=new boolean[X][Y];
    	bf.readLine();
    	for(int i=0;i<X;i++)
    	{
    		String s=bf.readLine();
    		for(int j=0;j<s.length();j++)
    		{
    			a[i][j]=s.charAt(j);
    			if(a[i][j]=='@')
    			{
    				sx=i;sy=j;
    			}
    		}
    	}
    	que.add(new Node(sx,sy,0));
    	run();
    	pw.flush();
    }
    
    private static void run() {
    	while(!que.isEmpty())
    	{
    		Node node=que.peek();
    		que.remove();
    		if(a[node.x][node.y]=='=')
    		{
    			pw.print(node.t);
    			return;
    		}
    		if(a[node.x][node.y]>='A'&&a[node.x][node.y]<='Z')
    		{
    			node=go_anther(node.x,node.y,node.t);
    		}
    		for(int i=0;i<=3;i++)
    		{
    			int nx=node.x+dx[i];
    			int ny=node.y+dy[i];
    			if(nx>=0&&nx<X&&ny>=0&&ny<Y&&a[nx][ny]!='#'&&!vis[nx][ny])
    			{
    				vis[nx][ny]=true;
    				que.add(new Node(nx,ny,node.t+1));
    			}
    		}
    	}
		
	}

	private static Node go_anther(int nx, int ny, int t) {
    	for(int i=0;i<X;i++)
        {
            for(int j=0;j<Y;j++)
            {
                if(a[i][j]==a[nx][ny]&&(i!=nx||j!=ny))
                {
                    return new Node(i,j,t);
                }
            }
        }
    	return null;
	}

	static int nextInt() throws IOException {
    	st.nextToken();
    	return (int)st.nval;
    }
}
class Node{
	int x;
	int y;
	int t;
	public Node(int x, int y, int t) {
		super();
		this.x = x;
		this.y = y;
		this.t = t;
	}
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY(){
		return y;
	}
	public void setY(int y){
		this.y = y;
	}
	public int getT() {
		return t;
	}
	public void setT(int t) {
		this.t = t;
	}
}

2023/2/14 17:17
加载中...