Last active
October 26, 2021 16:36
-
-
Save manji-0/55b091fdf00d34ebb518267bd1a45fad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Python 3.9.1 | |
import sys | |
from collections import deque | |
def main(): | |
W, H = [ int(x) for x in input().split(" ") ] | |
if W == H == 0: | |
sys.exit() | |
MAP = [] | |
queue = deque() | |
start_w = 0 | |
start_h = 0 | |
for i in range(H): | |
row = list(input()) | |
MAP.append(row) | |
for j, value in enumerate(row): | |
if value == "@": | |
start_w = j | |
start_h = i | |
queue.append((start_h, start_w)) | |
count = 0 | |
while len(queue): | |
h, w = queue.popleft() | |
if MAP[h][w] == "#": | |
continue | |
else: | |
MAP[h][w] = "#" | |
count += 1 | |
if w != 0: | |
queue.append((h, w-1)) | |
if w != (W - 1): | |
queue.append((h, w+1)) | |
if h != 0: | |
queue.append((h-1, w)) | |
if h != (H - 1): | |
queue.append((h+1, w)) | |
print(count) | |
while True: | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Python 3.9.1 | |
import sys | |
from collections import deque | |
def main(): | |
W, H = [ int(x) for x in input().split(" ") ] | |
if W == H == 0: | |
sys.exit() | |
MAP = [] | |
stack = deque() | |
start_w = 0 | |
start_h = 0 | |
for i in range(H): | |
row = list(input()) | |
MAP.append(row) | |
for j, value in enumerate(row): | |
if value == "@": | |
start_w = j | |
start_h = i | |
stack.append((start_h, start_w)) | |
count = 0 | |
while len(stack): | |
h, w = stack.pop() | |
if MAP[h][w] == "#": | |
continue | |
else: | |
MAP[h][w] = "#" | |
count += 1 | |
if w != 0: | |
stack.append((h, w-1)) | |
if w != (W - 1): | |
stack.append((h, w+1)) | |
if h != 0: | |
stack.append((h-1, w)) | |
if h != (H - 1): | |
stack.append((h+1, w)) | |
print(count) | |
while True: | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Python 3.9.5 | |
def dfs(w, h): | |
count = 0 | |
if MAP[h][w] == "#": | |
return 0 | |
else: | |
count += 1 | |
MAP[h][w] = "#" | |
if w != 0: | |
count += dfs(w-1, h) | |
if w != W - 1: | |
count += dfs(w+1, h) | |
if h != 0: | |
count += dfs(w, h-1) | |
if h != H - 1: | |
count += dfs(w, h+1) | |
return count | |
while True: | |
W, H = [ int(x) for x in input().split(" ") ] | |
if W == H == 0: | |
break | |
MAP = [] | |
start_w = 0 | |
start_h = 0 | |
for i in range(H): | |
row = list(input()) | |
MAP.append(row) | |
for j, value in enumerate(row): | |
if value == "@": | |
start_w = j | |
start_h = i | |
print(dfs(start_w, start_h)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1130&lang=ja