結果

問題 No.1948 足し算するだけのパズルゲーム(1)
ユーザー tentententen
提出日時 2022-07-27 23:26:40
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,867 bytes
コンパイル時間 2,562 ms
コンパイル使用メモリ 75,048 KB
実行使用メモリ 67,612 KB
最終ジャッジ日時 2023-09-24 12:02:54
合計ジャッジ時間 17,534 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,496 KB
testcase_01 AC 47 ms
49,568 KB
testcase_02 AC 47 ms
49,644 KB
testcase_03 AC 48 ms
49,652 KB
testcase_04 AC 46 ms
49,576 KB
testcase_05 AC 47 ms
49,572 KB
testcase_06 AC 46 ms
49,604 KB
testcase_07 AC 487 ms
67,456 KB
testcase_08 AC 287 ms
59,676 KB
testcase_09 AC 576 ms
63,604 KB
testcase_10 AC 560 ms
65,072 KB
testcase_11 AC 623 ms
63,964 KB
testcase_12 AC 717 ms
64,472 KB
testcase_13 TLE -
testcase_14 AC 1,660 ms
64,576 KB
testcase_15 AC 299 ms
59,068 KB
testcase_16 AC 57 ms
50,008 KB
testcase_17 AC 61 ms
50,856 KB
testcase_18 AC 49 ms
49,604 KB
testcase_19 AC 49 ms
49,988 KB
testcase_20 TLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます

ソースコード

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[][] field = new int[h][w];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                field[i][j] = sc.nextInt();
            }
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(1, 0, 0, field[0][0]));
        queue.add(new Path(0, 1, 0, field[0][0]));
        long[][][] gains = new long[2][h][w];
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (p.value <= field[p.r][p.c]) {
                if (p.count == 1) {
                    continue;
                }
                if (p.r < h - 1) {
                    queue.add(new Path(p.r + 1, p.c, 1, p.value));
                }
                if (p.c < w - 1) {
                    queue.add(new Path(p.r, p.c + 1, 1, p.value));
                }
            } else {
                p.value += field[p.r][p.c];
                if (gains[p.count][p.r][p.c] >= p.value) {
                    continue;
                }
                gains[p.count][p.r][p.c] = p.value;
                if (p.r < h - 1) {
                    queue.add(new Path(p.r + 1, p.c, p.count, p.value));
                }
                if (p.c < w - 1) {
                    queue.add(new Path(p.r, p.c + 1, p.count, p.value));
                }
            }
        }
        if (gains[0][h - 1][w - 1] > 0 || gains[1][h - 1][w - 1] > 0) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
    
    static class Path implements Comparable<Path> {
        int r;
        int c;
        int count;
        long value;
        
        public Path(int r, int c, int count, long value) {
            this.r = r;
            this.c = c;
            this.count = count;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            if (value == another.value) {
                return 0;
            } else if (value < another.value) {
                return 1;
            } else {
                return -1;
            }
        }
    }
}
class BinaryIndexedTree {
    int size;
    int[] tree;
    
    public BinaryIndexedTree(int size) {
        this.size = size;
        tree = new int[size];
    }
    
    public void clear() {
        Arrays.fill(tree, 0);
    }
    
    public void add(int idx, int value) {
        int mask = 1;
        while (idx < size) {
            if ((idx & mask) != 0) {
                tree[idx] += value;
                idx += mask;
            }
            mask <<= 1;
        }
    }
    
    public int getSum(int from, int to) {
        return getSum(to) - getSum(from - 1);
    }
    
    public int getSum(int x) {
        int mask = 1;
        int ans = 0;
        while (x > 0) {
            if ((x & mask) != 0) {
                ans += tree[x];
                x -= mask;
            }
            mask <<= 1;
        }
        return ans;
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0