結果

問題 No.1949 足し算するだけのパズルゲーム(2)
ユーザー tentententen
提出日時 2022-09-27 10:48:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 431 ms / 3,000 ms
コード長 3,297 bytes
コンパイル時間 3,466 ms
コンパイル使用メモリ 74,936 KB
実行使用メモリ 65,496 KB
最終ジャッジ日時 2023-08-24 03:43:15
合計ジャッジ時間 9,273 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,636 KB
testcase_01 AC 42 ms
49,772 KB
testcase_02 AC 42 ms
49,820 KB
testcase_03 AC 43 ms
49,684 KB
testcase_04 AC 43 ms
49,912 KB
testcase_05 AC 43 ms
49,508 KB
testcase_06 AC 43 ms
49,656 KB
testcase_07 AC 379 ms
58,932 KB
testcase_08 AC 293 ms
55,788 KB
testcase_09 AC 431 ms
61,572 KB
testcase_10 AC 404 ms
61,416 KB
testcase_11 AC 398 ms
61,708 KB
testcase_12 AC 363 ms
62,048 KB
testcase_13 AC 288 ms
60,244 KB
testcase_14 AC 357 ms
60,604 KB
testcase_15 AC 358 ms
61,400 KB
testcase_16 AC 208 ms
57,092 KB
testcase_17 AC 425 ms
65,496 KB
testcase_18 AC 196 ms
57,340 KB
testcase_19 AC 42 ms
49,516 KB
testcase_20 AC 44 ms
49,824 KB
testcase_21 AC 42 ms
49,480 KB
testcase_22 AC 366 ms
61,408 KB
testcase_23 AC 44 ms
49,548 KB
testcase_24 AC 250 ms
57,508 KB
testcase_25 AC 408 ms
58,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int h = sc.nextInt();
        int w = sc.nextInt();
        int x = sc.nextInt() - 1;
        int y = sc.nextInt() - 1;
        int[][] field = new int[h][w];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                field[i][j] = sc.nextInt();
            }
        }
        boolean[][] visited = new boolean[h][w];
        visited[x][y] = true;
        long current = field[x][y];
        PriorityQueue<Path> queue = new PriorityQueue<>();
        if (x > 0) {
            queue.add(new Path(x - 1, y, field[x - 1][y]));
            visited[x - 1][y] = true;
        }
        if (x < h - 1) {
            queue.add(new Path(x + 1, y, field[x + 1][y]));
            visited[x + 1][y] = true;
        }
        if (y > 0) {
            queue.add(new Path(x, y - 1, field[x][y - 1]));
            visited[x][y - 1] = true;
        }
        if (y < w - 1) {
            queue.add(new Path(x, y + 1, field[x][y + 1]));
            visited[x][y + 1] = true;
        }
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (current <= p.value) {
                System.out.println("No");
                return;
            }
            current += p.value;
            if (p.r > 0 && !visited[p.r - 1][p.c]) {
                queue.add(new Path(p.r - 1, p.c, field[p.r - 1][p.c]));
                visited[p.r - 1][p.c] = true;
            }
            if (p.r < h - 1 && !visited[p.r + 1][p.c]) {
                queue.add(new Path(p.r + 1, p.c, field[p.r + 1][p.c]));
                visited[p.r + 1][p.c] = true;
            }
            if (p.c > 0 && !visited[p.r][p.c - 1]) {
                queue.add(new Path(p.r, p.c - 1, field[p.r][p.c - 1]));
                visited[p.r][p.c - 1] = true;
            }
            if (p.c < w - 1 && !visited[p.r][p.c + 1]) {
                queue.add(new Path(p.r, p.c + 1, field[p.r][p.c + 1]));
                visited[p.r][p.c + 1] = true;
            }
        }
        System.out.println("Yes");
    }
    
    static class Path implements Comparable<Path> {
        int r;
        int c;
        int value;
        
        public Path(int r, int c, int value) {
            this.r = r;
            this.c = c;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
    }
}
    
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0