結果
| 問題 | No.13 囲みたい! | 
| コンテスト | |
| ユーザー |  vjudge1 | 
| 提出日時 | 2025-09-06 09:01:18 | 
| 言語 | Crystal (1.14.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 4 ms / 5,000 ms | 
| コード長 | 1,268 bytes | 
| コンパイル時間 | 12,548 ms | 
| コンパイル使用メモリ | 310,608 KB | 
| 実行使用メモリ | 7,720 KB | 
| 最終ジャッジ日時 | 2025-09-06 09:01:32 | 
| 合計ジャッジ時間 | 12,756 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 16 | 
ソースコード
W_MAX = 100
H_MAX = 100
w = 0
h = 0
field = Array(Array(Int32)).new(H_MAX) { Array(Int32).new(W_MAX, 0) }
used = Array(Array(Bool)).new(H_MAX) { Array(Bool).new(W_MAX, false) }
dy = [-1, 0, 1, 0]
dx = [0, 1, 0, -1]
def dfs(y : Int32, x : Int32, m : Int32, field : Array(Array(Int32)), used : Array(Array(Bool)), h : Int32, w : Int32, dy : Array(Int32), dx : Array(Int32)) : Bool
  used[y][x] = true
  cnt = 0
  
  4.times do |i|
    ny = y + dy[i]
    nx = x + dx[i]
    
    if ny < 0 || nx < 0 || ny >= h || nx >= w
      next
    end
    
    if used[ny][nx] || field[ny][nx] != m
      if field[ny][nx] == m
        cnt += 1
      end
      next
    end
    
    if dfs(ny, nx, m, field, used, h, w, dy, dx)
      return true
    end
  end
  
  cnt > 1
end
# Read input
input = gets.not_nil!.split
w = input[0].to_i
h = input[1].to_i
field = Array.new(h) { Array.new(w, 0) }
used = Array.new(h) { Array.new(w, false) }
h.times do |i|
  row = gets.not_nil!.split.map(&.to_i)
  w.times do |j|
    field[i][j] = row[j]
  end
end
found = false
h.times do |i|
  w.times do |j|
    next if used[i][j]
    if dfs(i, j, field[i][j], field, used, h, w, dy, dx)
      found = true
      break
    end
  end
  break if found
end
puts found ? "possible" : "impossible"
            
            
            
        