結果

問題 No.323 yuki国
ユーザー kusagame12kusagame12
提出日時 2023-11-26 19:26:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 386 ms / 5,000 ms
コード長 3,271 bytes
コンパイル時間 2,729 ms
コンパイル使用メモリ 83,456 KB
実行使用メモリ 65,816 KB
最終ジャッジ日時 2023-11-26 19:27:06
合計ジャッジ時間 10,125 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
54,740 KB
testcase_01 AC 65 ms
54,788 KB
testcase_02 AC 63 ms
54,756 KB
testcase_03 AC 93 ms
58,572 KB
testcase_04 AC 73 ms
55,148 KB
testcase_05 AC 78 ms
55,152 KB
testcase_06 AC 72 ms
55,140 KB
testcase_07 AC 63 ms
54,760 KB
testcase_08 AC 243 ms
65,692 KB
testcase_09 AC 245 ms
65,692 KB
testcase_10 AC 63 ms
54,644 KB
testcase_11 AC 71 ms
54,756 KB
testcase_12 AC 65 ms
54,760 KB
testcase_13 AC 220 ms
65,716 KB
testcase_14 AC 235 ms
65,756 KB
testcase_15 AC 174 ms
65,740 KB
testcase_16 AC 260 ms
65,708 KB
testcase_17 AC 301 ms
65,716 KB
testcase_18 AC 133 ms
61,280 KB
testcase_19 AC 204 ms
63,636 KB
testcase_20 AC 366 ms
65,736 KB
testcase_21 AC 386 ms
65,816 KB
testcase_22 AC 378 ms
65,740 KB
testcase_23 AC 376 ms
65,756 KB
testcase_24 AC 195 ms
65,732 KB
testcase_25 AC 215 ms
65,700 KB
testcase_26 AC 259 ms
65,692 KB
testcase_27 AC 251 ms
65,720 KB
testcase_28 AC 269 ms
65,724 KB
testcase_29 AC 256 ms
65,716 KB
testcase_30 AC 67 ms
54,764 KB
testcase_31 AC 68 ms
54,880 KB
testcase_32 AC 82 ms
55,040 KB
testcase_33 AC 68 ms
54,764 KB
testcase_34 AC 79 ms
55,156 KB
testcase_35 AC 64 ms
54,616 KB
testcase_36 AC 73 ms
55,152 KB
testcase_37 AC 72 ms
55,028 KB
権限があれば一括ダウンロードができます

ソースコード

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