結果

問題 No.13 囲みたい!
ユーザー noriocnorioc
提出日時 2016-02-19 06:49:37
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 1,423 ms / 5,000 ms
コード長 1,046 bytes
コンパイル時間 13,014 ms
コンパイル使用メモリ 258,512 KB
実行使用メモリ 74,964 KB
最終ジャッジ日時 2024-04-28 05:18:06
合計ジャッジ時間 32,800 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 891 ms
64,748 KB
testcase_01 AC 889 ms
65,012 KB
testcase_02 AC 889 ms
64,772 KB
testcase_03 AC 1,175 ms
73,536 KB
testcase_04 AC 1,137 ms
74,964 KB
testcase_05 AC 1,203 ms
73,768 KB
testcase_06 AC 1,154 ms
74,724 KB
testcase_07 AC 1,213 ms
74,936 KB
testcase_08 AC 1,183 ms
74,232 KB
testcase_09 AC 1,194 ms
73,420 KB
testcase_10 AC 1,088 ms
65,144 KB
testcase_11 AC 1,423 ms
74,260 KB
testcase_12 AC 975 ms
65,028 KB
testcase_13 AC 1,110 ms
65,228 KB
testcase_14 AC 1,141 ms
65,360 KB
testcase_15 AC 908 ms
64,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import math._
import collection.mutable.Stack

object Main {
  def calc(rows: Int, cols: Int, g: Array[Array[Int]]): Boolean = {
    val used = Array.fill(rows, cols)(false)
    for (i <- 0 to rows-1; j <- 0 to cols-1 if !used(i)(j)) {
      val num = g(i)(j)
      val s = new Stack[(Int, Int, Int, Int)]
      s.push((i, j, -1, -1))
      while (!s.isEmpty) {
        val (r, c, pr, pc) = s.pop

        if (used(r)(c)) return true

        used(r)(c) = true
        for ((dr, dc) <- Array((1, 0), (0, 1), (-1, 0), (0, -1))) {
          val r2 = r + dr
          val c2 = c + dc
          if (0 <= r2 && r2 < rows && 0 <= c2 && c2 < cols) {
            if (g(r2)(c2) == num && (r2, c2) != (pr, pc)) {
              s.push((r2, c2, r, c))
            }
          }
        }
      }
    }
    false
  }

  def main(args: Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    val W, H = sc.nextInt
    val g = Array.fill(H, W)(sc.nextInt)
    if (calc(H, W, g))
      println("possible")
    else
      println("impossible")
  }
}
0