第一行输入两个整数
——纸张的行数与列数。
接下来
行,每行输入一个长度为
的由 `'.'` 与 `'*'` 组成的字符串,描述残缺纸张的形状:
`'.'` 代表该方格已被剪去;
`'*'` 代表该方格仍保留。
输出一个整数,表示被剪下来的图案中长方形的数量。
4 10 *.*.*...** ...***.*.. .**..*.*.. *..*****..
4
可以看出,图中恰有一个正方形,三个长方形,共计四个长方形。
from collections import deque n, m = map(int, input().split()) g = [] for _ in range(n): g.append(input()) visited = [[False] * m for _ in range(n)] dirs = [(0,-1),(0,1),(-1,0),(1,0)] def bfs(start_r, start_c): queue = deque([(start_r, start_c)]) bound = [start_r, start_r, start_c, start_c] count = 0 while queue: r, c = queue.popleft() # BFS: take from front if r < 0&nbs***bsp;r >= n&nbs***bsp;c < 0&nbs***bsp;c >= m: continue if visited[r][c]&nbs***bsp;g[r][c] == '*': continue visited[r][c] = True count += 1 bound[0] = min(bound[0], r) bound[1] = max(bound[1], r) bound[2] = min(bound[2], c) bound[3] = max(bound[3], c) for y, x in dirs: queue.append((r+y, c+x)) return count, bound ans = 0 for i in range(n): for j in range(m): if not visited[i][j] and g[i][j] == '.': cnt, bound = bfs(i, j) area = (bound[1] - bound[0] + 1) * (bound[3] - bound[2] + 1) if cnt == area: ans += 1 print(ans)