結果
| 問題 | No.124 門松列(3) |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-01-10 16:24:28 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,346 bytes |
| 記録 | |
| コンパイル時間 | 4,533 ms |
| コンパイル使用メモリ | 86,472 KB |
| 実行使用メモリ | 47,416 KB |
| 最終ジャッジ日時 | 2024-06-13 03:33:40 |
| 合計ジャッジ時間 | 10,184 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 24 WA * 2 |
ソースコード
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
public class yukicoder123 {
class Tuple {
int x;
int y;
int[] his;
Tuple(int x, int y, int[] his) {
this.x = x;
this.y = y;
this.his = his;
}
boolean checkSequence(int c) {
return isKadomatsuSequence(his[1], his[2], c);
}
@Override
public String toString() {
return "" + x + ":" + y + Arrays.toString(this.his);
}
}
int[][] vec = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
boolean isKadomatsuSequence(int a, int b, int c) {
if (a == c) {
return false;
}
return (a - b) * (b - c) < 0;
}
int is(boolean b) {
return b ? 1 : 0;
}
public yukicoder123() {
Scanner scanner = new Scanner(System.in);
int W = scanner.nextInt();
int H = scanner.nextInt();
int[][] matrix = new int[H][W];
int[][][] checked = new int[H][W][2];
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
matrix[i][j] = scanner.nextInt();
Arrays.fill(checked[i][j], Integer.MAX_VALUE);
}
}
LinkedList<Tuple> nextQueue = new LinkedList<>();
LinkedList<Tuple> queue = new LinkedList<>();
checked[0][0][0] = 0;
checked[0][0][1] = 0;
if (matrix[0][0] != matrix[0][1]) {
int index = is(matrix[0][0] > matrix[0][1]);
checked[0][1][index] = 1;
queue.add(new Tuple(1, 0, new int[]{0, matrix[0][0], matrix[0][1]}));
}
if (matrix[0][0] != matrix[1][0]) {
int index = is(matrix[0][0] > matrix[1][0]);
checked[1][0][index] = 1;
queue.add(new Tuple(0, 1, new int[]{0, matrix[0][0], matrix[1][0]}));
}
int turn = 2;
MAIN:
while (queue.size() > 0) {
while (queue.size() > 0) {
Tuple tuple = queue.pop();
for (int[] v : vec) {
int x = tuple.x + v[0];
int y = tuple.y + v[1];
if (!(0 <= x && x < W && 0 <= y && y < H)) {
continue;
}
if (!tuple.checkSequence(matrix[y][x])) {
continue;
}
int flag = is(tuple.his[2] - matrix[y][x] > 0);
if (checked[y][x][flag] <= turn) {
continue;
}
checked[y][x][flag] = turn;
if (y == H - 1 && x == W - 1) {
break MAIN;
}
nextQueue.add(new Tuple(x, y, new int[]{tuple.his[1], tuple.his[2], matrix[y][x]}));
}
}
queue.addAll(nextQueue);
nextQueue.clear();
turn++;
}
int result = Math.min(checked[H - 1][W - 1][0], checked[H - 1][W - 1][1]);
if (result != Integer.MAX_VALUE) {
System.out.println(result);
} else {
System.out.println(-1);
}
}
public static void main(String[] args) {
yukicoder123 hoge = new yukicoder123();
}
}