結果
| 問題 |
No.424 立体迷路
|
| コンテスト | |
| ユーザー |
norioc
|
| 提出日時 | 2016-09-22 22:49:48 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 31 ms / 2,000 ms |
| コード長 | 2,253 bytes |
| コンパイル時間 | 4,818 ms |
| コンパイル使用メモリ | 115,256 KB |
| 実行使用メモリ | 27,520 KB |
| 最終ジャッジ日時 | 2024-07-05 07:04:20 |
| 合計ジャッジ時間 | 3,007 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 21 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static int ReadInt() { return int.Parse(Console.ReadLine()); }
static int[] ReadInts() { return Console.ReadLine().Split().Select(int.Parse).ToArray(); }
static string[] ReadStrings() { return Console.ReadLine().Split(); }
struct D {
public int r;
public int c;
}
static bool Calc(int srow, int scol, int grow, int gcol, int[,] g) {
int rows = g.GetLength(0);
int cols = g.GetLength(1);
Func<int, int, bool> inRange = (r, c) => 0 <= r && r < rows && 0 <= c && c < cols;
var delta = new[,] { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
var used = new bool[rows, cols];
var q = new Queue<D>();
q.Enqueue(new D { r = srow-1, c = scol-1 });
while (q.Count > 0) {
var d = q.Dequeue();
if (d.r == grow-1 && d.c == gcol-1) return true;
if (used[d.r, d.c]) continue;
used[d.r, d.c] = true;
for (int i = 0; i < 4; i++) {
int r = d.r + delta[i, 0];
int c = d.c + delta[i, 1];
if (inRange(r, c) && Math.Abs(g[r, c] - g[d.r, d.c]) <= 1) {
q.Enqueue(new D { r = r, c = c });
}
// 2 つ先のブロックに移動する
if (inRange(r, c) && g[d.r, d.c] > g[r, c]) {
int r2 = d.r + delta[i, 0] * 2;
int c2 = d.c + delta[i, 1] * 2;
if (inRange(r2, c2) && g[r2, c2] == g[d.r, d.c]) {
q.Enqueue(new D { r = r2, c = c2 });
}
}
}
}
return false;
}
static void Main() {
var hw = ReadInts();
var xs = ReadInts();
int rows = hw[0], cols = hw[1];
int srow = xs[0], scol = xs[1], grow = xs[2], gcol = xs[3];
var g = new int[rows, cols];
for (int i = 0; i < rows; i++) {
var s = Console.ReadLine();
for (int j = 0; j < cols; j++) {
g[i, j] = s[j] - '0';
}
}
Console.WriteLine(Calc(srow, scol, grow, gcol, g) ? "YES" : "NO");
}
}
norioc