結果
| 問題 |
No.1948 足し算するだけのパズルゲーム(1)
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2022-07-27 23:26:40 |
| 言語 | Java (openjdk 23) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,867 bytes |
| コンパイル時間 | 2,646 ms |
| コンパイル使用メモリ | 79,208 KB |
| 実行使用メモリ | 63,672 KB |
| 最終ジャッジ日時 | 2024-07-17 12:57:50 |
| 合計ジャッジ時間 | 15,166 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 20 TLE * 1 -- * 6 |
ソースコード
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();
}
}
tenten