from sequtils import map, mapIt
from strutils import split, parseInt, parseFloat
import macros
macro unpack*(input: seq; count: static[int]): untyped =
result = quote do: ()
when NimMinor <= 13:
for i in 0..<count: result[0].add quote do: `input`[`i`]
else:
for i in 0..<count: result.add quote do: `input`[`i`]
## count == 0 のとき unpackしない(seq)
## count > 0 のとき count個分 unpack した結果の tuple を返す
type UnselectableTypeError = object of Exception
template input*(typ: typedesc; count: Natural = 0): untyped =
let line = stdin.readLine.split
when count == 0:
when typ is int: line.map(parseInt)
elif typ is float: line.map(parseFloat)
elif typ is string: line
else: raise newException(UnselectableTypeError, "You selected a type other than int, float or string")
else:
when typ is int: line.map(parseInt).unpack(count)
elif typ is float: line.map(parseFloat).unpack(count)
elif typ is string: line.unpack(count)
else: raise newException(UnselectableTypeError, "You selected a type other than int, float or string")
template inputs*(typ: typedesc; count = 0; rows = 1): untyped =
(1..rows).mapIt(input(typ, count))
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
template secretEcho*(x: varargs[string, `$`]) =
for v in x:
stderr.write(v)
stderr.writeLine ""
template secretEcho*[T](x: seq[seq[T]]) =
for v in x: secretEcho v
template secretEcho*(x: seq[string]) =
for v in x: secretEcho v
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from sequtils import newSeqWith
import queues
from math import sum
from strutils import count
const
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
let
(H, W) = input(int, 2)
s = (1..H).mapIt(input(string, 1))
var
maze = newSeqWith(H, newSeqWith(W, -1))
q = initQueue[tuple[x, y: int]]()
q.enqueue((0, 0))
maze[0][0] = 1
while q.len > 0:
let v = q.dequeue
for i, _ in dx:
let
x = v.x + dx[i]
y = v.y + dy[i]
if 0 <= x and x < W and 0 <= y and y < H:
if s[y][x] == '.' and maze[y][x] < 0:
maze[y][x] = maze[v.y][v.x] + 1
q.enqueue((x, y))
if maze[^1][^1] < 0:
echo -1
else:
echo H * W - maze[^1][^1] - s.mapIt(it.count('#')).sum