結果
問題 | No.424 立体迷路 |
ユーザー |
![]() |
提出日時 | 2016-11-27 15:08:29 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 81 ms / 2,000 ms |
コード長 | 2,833 bytes |
コンパイル時間 | 2,916 ms |
コンパイル使用メモリ | 80,312 KB |
実行使用メモリ | 38,592 KB |
最終ジャッジ日時 | 2024-07-05 07:13:16 |
合計ジャッジ時間 | 5,081 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 21 |
ソースコード
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.StringTokenizer;public class Main {static BufferedReader in;static PrintWriter out;static StringTokenizer tok;void solve() throws IOException {h = ni();w = ni();sx = ni() - 1;sy = ni() - 1;gx = ni() - 1;gy = ni() - 1;b = new int[h][w];for (int i = 0; i < h; i++) {b[i] = ns().chars().map(c -> c - '0').toArray();}used = new boolean[h][w];out.println(rec(sx, sy) ? "YES" : "NO");}int h, w, sx, sy, gx, gy;int[][] b;boolean[][] used;int[] dx = {1, 0, -1, 0};int[] dy = {0, 1, 0, -1};int[] dx2 = {2, 0, -2, 0};int[] dy2 = {0, 2, 0, -2};boolean rec(int i, int j) {if (used[i][j]) return false;used[i][j] = true;if (i == gx && j == gy) {return true;}for (int k = 0; k < 4; k++) {if (isin(i + dx[k], j + dy[k]) && Math.abs(b[i][j] - b[i + dx[k]][j + dy[k]]) <= 1 && rec(i + dx[k], j + dy[k])) {return true;}if (isin(i + dx2[k], j + dy2[k]) && b[i][j] == b[i + dx2[k]][j + dy2[k]] && b[i][j] > b[i + dx[k]][j + dy[k]] && rec(i + dx2[k], j +dy2[k])) {return true;}}return false;}boolean isin(int i, int j) {return 0 <= i && i <= h - 1 && 0 <= j && j <= w - 1;}String ns() throws IOException {while (!tok.hasMoreTokens()) {tok = new StringTokenizer(in.readLine(), " ");}return tok.nextToken();}int ni() throws IOException {return Integer.parseInt(ns());}long nl() throws IOException {return Long.parseLong(ns());}double nd() throws IOException {return Double.parseDouble(ns());}String[] nsa(int n) throws IOException {String[] res = new String[n];for (int i = 0; i < n; i++) {res[i] = ns();}return res;}int[] nia(int n) throws IOException {int[] res = new int[n];for (int i = 0; i < n; i++) {res[i] = ni();}return res;}long[] nla(int n) throws IOException {long[] res = new long[n];for (int i = 0; i < n; i++) {res[i] = nl();}return res;}public static void main(String[] args) throws IOException {in = new BufferedReader(new InputStreamReader(System.in));out = new PrintWriter(System.out);tok = new StringTokenizer("");Main main = new Main();main.solve();out.close();}}