4 solutions
-
0
w, h = map(int, input().split()) m = [] s = (-1, -1) for i in range(h): line = input() m.append(line) if("@" in line): s = (i, line.index("@")) ans = 1 direct = [(1, 0), (0, 1), (-1, 0), (0, -1)] vis = [[True]*w for _ in range(h)] def check(x, y): if(x<0 or y<0 or x>=h or y>=w): return False if(not vis[x][y]): return False if(m[x][y]!='.'): return False return True ans = 1 def dfs(x, y): global ans for dx, dy in direct: nx = x + dx ny = y + dy if(check(nx, ny)): vis[nx][ny] = False ans+=1 dfs(nx, ny) dfs(*s) print(ans)
bfs
from collections import deque ans = 1 def bfs(s): global ans q = deque() q.append(s) while (len(q) != 0): tx, ty = q.popleft() for dx, dy in direct: nx = tx + dx ny = ty + dy if (check(nx, ny)): vis[nx][ny] = False ans += 1 q.append((nx, ny)) bfs(s) print(ans)
Information
- ID
- 1233
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 4
- Tags
- # Submissions
- 175
- Accepted
- 80
- Uploaded By