結果

問題 No.697 池の数はいくつか
ユーザー むらためむらため
提出日時 2019-01-24 06:06:14
言語 Nim
(2.0.2)
結果
MLE  
実行時間 -
コード長 2,070 bytes
コンパイル時間 2,393 ms
コンパイル使用メモリ 66,984 KB
実行使用メモリ 462,856 KB
最終ジャッジ日時 2023-08-16 22:59:50
合計ジャッジ時間 9,277 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,084 KB
testcase_01 AC 2 ms
5,032 KB
testcase_02 AC 2 ms
5,080 KB
testcase_03 AC 2 ms
5,048 KB
testcase_04 AC 2 ms
5,052 KB
testcase_05 AC 2 ms
5,080 KB
testcase_06 AC 2 ms
5,036 KB
testcase_07 AC 2 ms
5,088 KB
testcase_08 AC 2 ms
5,036 KB
testcase_09 AC 2 ms
4,952 KB
testcase_10 AC 2 ms
5,040 KB
testcase_11 AC 2 ms
5,092 KB
testcase_12 AC 2 ms
5,028 KB
testcase_13 AC 2 ms
5,124 KB
testcase_14 AC 2 ms
5,052 KB
testcase_15 AC 2 ms
5,040 KB
testcase_16 AC 2 ms
5,072 KB
testcase_17 AC 2 ms
5,040 KB
testcase_18 AC 2 ms
5,056 KB
testcase_19 AC 2 ms
5,052 KB
testcase_20 AC 2 ms
5,100 KB
testcase_21 AC 2 ms
5,056 KB
testcase_22 AC 2 ms
5,020 KB
testcase_23 AC 2 ms
5,020 KB
testcase_24 AC 65 ms
10,440 KB
testcase_25 AC 64 ms
10,456 KB
testcase_26 AC 65 ms
10,448 KB
testcase_27 AC 65 ms
10,428 KB
testcase_28 AC 65 ms
10,448 KB
testcase_29 MLE -
testcase_30 AC 545 ms
21,020 KB
testcase_31 MLE -
testcase_32 AC 543 ms
20,928 KB
testcase_33 AC 547 ms
21,068 KB
testcase_34 AC 541 ms
21,048 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 8) Warning: imported and not used: 'sequtils' [UnusedImport]

ソースコード

diff #

import sequtils

proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': break
    result = 10 * result + k.ord - '0'.ord

template useStack() =
  type Stack*[T] = ref object
    data: seq[T]
    size: int
    index: int
  proc newStack*[T](size: int = 64): Stack[T] =
    new(result)
    result.data = newSeq[T](size)
    result.size = size
    result.index = -1
  proc isEmpty*[T](self: Stack[T]): bool = self.index < 0
  proc isValid*[T](self: Stack[T]): bool = self.index >= 0 and self.index < self.size
  proc len*[T](self: Stack[T]): int =
    if self.isEmpty(): return 0
    return self.index + 1
  proc top*[T](self: Stack[T]): T =
    assert self.isValid()
    return self.data[self.index]
  proc pop*[T](self: var Stack[T]): T {.discardable.} =
    assert self.index >= 0
    result = self.top()
    self.index -= 1
  proc push*[T](self: var Stack[T], elem: T) =
    self.index += 1
    if self.index < self.size:
      self.data[self.index] = elem
    else:
      self.data.add(elem)
      self.size += 1

useStack()

let h = scan()
let w = scan()

var isWater : array[3001,array[3001,bool]]
for y in 0..<h:
  for x in 0..<w:
    isWater[x][y] = getchar_unlocked() == '1'
    discard getchar_unlocked()
var checked : array[3001,array[3001,bool]]
const dxdy4 :seq[tuple[x,y:int]] = @[(0,1),(1,0),(0,-1),(-1,0)]
proc check(sx,sy:int) : bool =
  if checked[sx][sy] : return false
  if not isWater[sx][sy]: return false
  var X = newStack[int]()
  var Y = newStack[int]()
  X.push(sx)
  Y.push(sy)
  while not X.isEmpty():
    let x = X.pop()
    let y = Y.pop()
    if checked[x][y] : continue
    checked[x][y] = true
    for d in dxdy4:
      let (nx,ny) = (x+d.x,y+d.y)
      if nx < 0 or nx >= w : continue
      if ny < 0 or ny >= h : continue
      if checked[nx][ny] : continue
      if not isWater[nx][ny] : continue
      X.push(nx)
      Y.push(ny)
  return true
var ans = 0
for x in 0..<w:
  for y in 0..<h:
    if check(x,y) : ans += 1
echo ans
0