結果

問題 No.13 囲みたい!
ユーザー norioc
提出日時 2016-02-19 06:49:37
言語 Scala(Beta)
(3.6.2)
結果
AC  
実行時間 1,352 ms / 5,000 ms
コード長 1,046 bytes
コンパイル時間 9,752 ms
コンパイル使用メモリ 269,940 KB
実行使用メモリ 75,992 KB
最終ジャッジ日時 2024-11-16 17:11:32
合計ジャッジ時間 30,743 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

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