結果
| 問題 |
No.1949 足し算するだけのパズルゲーム(2)
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2022-09-27 10:48:46 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 454 ms / 3,000 ms |
| コード長 | 3,297 bytes |
| コンパイル時間 | 2,367 ms |
| コンパイル使用メモリ | 78,540 KB |
| 実行使用メモリ | 52,740 KB |
| 最終ジャッジ日時 | 2024-12-22 16:39:02 |
| 合計ジャッジ時間 | 9,946 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
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();
}
}
tenten