結果

問題 No.323 yuki国
ユーザー kusagame12
提出日時 2023-11-26 19:26:55
言語 Java
(openjdk 23)
結果
AC  
実行時間 418 ms / 5,000 ms
コード長 3,271 bytes
コンパイル時間 3,015 ms
コンパイル使用メモリ 86,880 KB
実行使用メモリ 52,328 KB
最終ジャッジ日時 2024-09-26 11:51:53
合計ジャッジ時間 11,554 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    
    private static int[] dy = {0,1,0,-1};
    private static int[] dx = {1,0,-1,0};
    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    public static void main(String[] args) throws Exception {

    	int[] h_w = readIntArray();
        int h = h_w[0];
        int w = h_w[1];
        
        int[] aInfo = readIntArray();
        int a = aInfo[0];
        int si = aInfo[1];
        int sj = aInfo[2];
        
        int[] bInfo = readIntArray();
        int b = bInfo[0];
        int gi = bInfo[1];
        int gj = bInfo[2];
        
        boolean[][] field = new boolean[h][w];
        for(int i = 0 ; i < h ; i++){
            char[] terrains = br.readLine().toCharArray();
            for(int j = 0 ;  j < w ; j++){
                if(terrains[j] == '*'){
                    field[i][j] = true;
                }
            }
        }
        
        boolean[][][] exist = new boolean[2000][h+1][w+1];   
        exist[a][si][sj] = true;
        String judge = bfs(field , a , si , sj , b , gi , gj , h , w , exist);
            
        System.out.println(judge);
		
    }
    
    private static String bfs(boolean[][] field , int firstVolume , int firstH , int firstW , int goalVolume , int goalH , int goalW , int h , int w , boolean[][][] exist){
        
        ArrayDeque<Pair> todo = new ArrayDeque<>();
        todo.addLast(new Pair(firstVolume , firstH , firstW));
        
        while(!todo.isEmpty()){
            
            Pair p = todo.pollFirst();
            int currentH = p.getY();
            int currentW = p.getX();
         
            for(int i = 0 ; i < 4 ; i++){
                
                int nextH = currentH + dy[i];
                int nextW = currentW + dx[i];
                
                if(nextH < 0 || nextH >= h || nextW < 0 || nextW >= w){
                    continue;
                }
                
                int size = p.getSize();
                
                if(field[nextH][nextW]){
                    size++;
                }else{
                    size--;
                }
                
                if(size >= 2000 || size <= 0){
                    continue;
                }
                
                if(exist[size][nextH][nextW]){
                    continue;
                }
                
                //System.out.println(size+":"+nextH+":"+nextW);
                exist[size][nextH][nextW] = true;
                todo.addLast(new Pair(size , nextH , nextW));

            }
            

        }
        
        return exist[goalVolume][goalH][goalW] ? "Yes" : "No";
                
    }
    
    private static int[] readIntArray() throws Exception{
        
		int[] intArray = Arrays.stream(br.readLine().split(" "))
							.mapToInt(Integer::parseInt).toArray();

		return intArray;

	}

}

class Pair{
    
    int size , y , x;
    
    public Pair(int size , int y  , int x){
        this.size = size;
        this.y = y;
        this.x = x;
    }
    
    public int getSize(){
        return size;
    }
    
    public int getY(){
        return y;
    }
    
    public int getX(){
        return x;
    }
    
    
}
0