|
| 1 | +# https://maang.in/problems/Connected-Component-Size-684?resourceUrl=cs214-cp873-pl3338-rs684&returnUrl=%5B%22%2Fcourses%2FGraph-Level-1-214%3Ftab%3Dchapters%22%5D |
| 2 | + |
| 3 | +""" |
| 4 | +Description |
| 5 | +You have a 2-D array of size N×M. Consider connected 0's (which share a common edge) as one single component and 1's as walls. Replace 0's with the size of the connected component but if the size of the component is one, then leave it with 0. |
| 6 | +
|
| 7 | +Input Format |
| 8 | +The first line contains a single integer t, the number of test cases. For each test case, the first line contains two integers |
| 9 | +N and M and then there are N lines containing N×M binary matrix. |
| 10 | +
|
| 11 | +Output Format |
| 12 | +For each test case, print the final matrix after replacing all the 0's accordingly. |
| 13 | +
|
| 14 | +Constraints |
| 15 | +(N×M) over all test cases ≤2×105 |
| 16 | +
|
| 17 | +0≤Ai≤1 |
| 18 | +
|
| 19 | +Sample Input 1 |
| 20 | +2 |
| 21 | +2 2 |
| 22 | +0 1 |
| 23 | +1 0 |
| 24 | +6 5 |
| 25 | +1 0 0 1 0 |
| 26 | +0 1 0 0 0 |
| 27 | +0 0 1 1 0 |
| 28 | +0 1 1 0 1 |
| 29 | +1 1 1 1 1 |
| 30 | +0 1 0 0 0 |
| 31 | +Sample Output 1 |
| 32 | +0 1 |
| 33 | +1 0 |
| 34 | +1 7 7 1 7 |
| 35 | +4 1 7 7 7 |
| 36 | +4 4 1 1 7 |
| 37 | +4 1 1 0 1 |
| 38 | +1 1 1 1 1 |
| 39 | +0 1 3 3 3 |
| 40 | +""" |
| 41 | + |
| 42 | + |
| 43 | +import sys |
| 44 | +from collections import deque |
| 45 | + |
| 46 | +input = sys.stdin.readline |
| 47 | +print = sys.stdout.write |
| 48 | + |
| 49 | +direction = [(1, 0), (0, 1), (-1, 0), (0, -1)] |
| 50 | + |
| 51 | + |
| 52 | +def list_ints(): |
| 53 | + return list(map(int, input().split())) |
| 54 | + |
| 55 | + |
| 56 | +def bfs(x, y, grid): |
| 57 | + vis = set() |
| 58 | + vis.add((x, y)) |
| 59 | + |
| 60 | + q = deque([(x, y)]) |
| 61 | + size = 0 |
| 62 | + |
| 63 | + while q: |
| 64 | + x, y = q.popleft() |
| 65 | + size += 1 |
| 66 | + |
| 67 | + for dx, dy in direction: |
| 68 | + nx, ny = x + dx, y + dy |
| 69 | + |
| 70 | + if 0 <= nx < len(grid) and 0 <= ny < len(grid[0]): |
| 71 | + if (nx, ny) in vis or grid[nx][ny] == "1": |
| 72 | + continue |
| 73 | + |
| 74 | + vis.add((nx, ny)) |
| 75 | + q.append((nx, ny)) |
| 76 | + |
| 77 | + return size |
| 78 | + |
| 79 | + |
| 80 | +def solve(): |
| 81 | + t = int(input()) |
| 82 | + |
| 83 | + for _ in range(t): |
| 84 | + n, m = list_ints() |
| 85 | + |
| 86 | + grid = [] |
| 87 | + for _ in range(n): |
| 88 | + grid.append(input().strip().split()) |
| 89 | + |
| 90 | + ans = [] |
| 91 | + |
| 92 | + for i in range(n): |
| 93 | + row = [] |
| 94 | + for j in range(m): |
| 95 | + if grid[i][j] == "1": |
| 96 | + row.append("1") |
| 97 | + else: |
| 98 | + size = bfs(i, j, grid) |
| 99 | + row.append("0" if size == 1 else str(size)) |
| 100 | + |
| 101 | + ans.append(" ".join(row)) |
| 102 | + |
| 103 | + print("\n".join(ans) + "\n") |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + solve() |
| 108 | + |
| 109 | + import sys |
| 110 | +from collections import deque |
| 111 | + |
| 112 | +input = sys.stdin.readline |
| 113 | +print = sys.stdout.write |
| 114 | + |
| 115 | +direction = [(1, 0), (0, 1), (-1, 0), (0, -1)] |
| 116 | + |
| 117 | + |
| 118 | +def list_ints(): |
| 119 | + return list(map(int, input().split())) |
| 120 | + |
| 121 | + |
| 122 | +def solve(): |
| 123 | + t = int(input()) |
| 124 | + |
| 125 | + for _ in range(t): |
| 126 | + n, m = list_ints() |
| 127 | + |
| 128 | + # for "0 1 0" input |
| 129 | + grid = [input().strip().split() for _ in range(n)] |
| 130 | + # if input is "010", use: |
| 131 | + # grid = [list(input().strip()) for _ in range(n)] |
| 132 | + |
| 133 | + comp_id = [[-1] * m for _ in range(n)] |
| 134 | + comp_size = [] |
| 135 | + cid = 0 |
| 136 | + |
| 137 | + # BFS to find components of '0' |
| 138 | + for i in range(n): |
| 139 | + for j in range(m): |
| 140 | + if grid[i][j] == "0" and comp_id[i][j] == -1: |
| 141 | + q = deque([(i, j)]) |
| 142 | + comp_id[i][j] = cid |
| 143 | + size = 1 |
| 144 | + |
| 145 | + while q: |
| 146 | + x, y = q.popleft() |
| 147 | + |
| 148 | + for dx, dy in direction: |
| 149 | + nx, ny = x + dx, y + dy |
| 150 | + |
| 151 | + if 0 <= nx < n and 0 <= ny < m: |
| 152 | + if grid[nx][ny] == "0" and comp_id[nx][ny] == -1: |
| 153 | + comp_id[nx][ny] = cid |
| 154 | + q.append((nx, ny)) |
| 155 | + size += 1 |
| 156 | + |
| 157 | + comp_size.append(size) |
| 158 | + cid += 1 |
| 159 | + |
| 160 | + # Build answer |
| 161 | + ans = [] |
| 162 | + for i in range(n): |
| 163 | + row = [] |
| 164 | + for j in range(m): |
| 165 | + if grid[i][j] == "1": |
| 166 | + row.append("1") |
| 167 | + else: |
| 168 | + size = comp_size[comp_id[i][j]] |
| 169 | + row.append("0" if size == 1 else str(size)) |
| 170 | + ans.append(" ".join(row)) |
| 171 | + |
| 172 | + print("\n".join(ans) + "\n") |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == "__main__": |
| 176 | + solve() |
| 177 | + |
| 178 | + |
| 179 | +""" |
| 180 | +Time Complexity per test case: O(N×M). Space Complexity per test case: O(N×M). |
| 181 | +""" |
0 commit comments