Last active
May 21, 2017 23:15
-
-
Save sat0b/afce7e8dce6ae962099a654afb702e4e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # 水たまりのカウント | |
| # 蟻本 p.35 | |
| lake = """\ | |
| W........WW. | |
| .WWW.....WWW | |
| ....WW...WW. | |
| .........WW. | |
| .........W.. | |
| ..W......W.. | |
| .W.W.....WW. | |
| W.W.W.....W. | |
| W.W.W.....W. | |
| .W.W......W. | |
| ..W.......W.""" | |
| lake = [list(l) for l in lake.split("\n")] | |
| N = len(lake) | |
| M = len(lake[0]) | |
| def dfs(y, x): | |
| lake[y][x] = '.' | |
| for dx in (-1, 0, 1): | |
| for dy in (-1, 0, 1): | |
| if (dx, dy) == (0, 0): | |
| continue | |
| nx = x + dx | |
| ny = y + dy | |
| if 0 <= ny < N and 0 <= nx < M: | |
| if lake[ny][nx] == 'W': | |
| dfs(ny, nx) | |
| def solve(): | |
| res = 0 | |
| for y in range(N): | |
| for x in range(M): | |
| if lake[y][x] == 'W': | |
| dfs(y, x) | |
| res += 1 | |
| return res | |
| ret = solve() | |
| print(ret) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment