-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path61.squareroom2.py
More file actions
31 lines (28 loc) · 821 Bytes
/
61.squareroom2.py
File metadata and controls
31 lines (28 loc) · 821 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def f(si, sj):
q = [(si, sj)]
cnt = 0
while q:
i, j = q.pop()
cnt += 1
for di, dj in (-1, 0), (1, 0), (0, 1), (0, -1):
ni, nj = i + di, j + dj
if 0 <= ni < N and 0 <= nj < N:
if A[ni][nj] == A[i][j] + 1:
q.append((ni, nj))
return cnt
T = int(input())
for tc in range(1, T+1):
N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]
maxV = 0
room = N**2
for i in range(N):
for j in range(N):
result = f(i, j)
if maxV < result:
maxV = result
room = A[i][j]
elif result == maxV and A[i][j] < room:
maxV = result
room = A[i][j]
print('#{} {} {}' .format(tc, room, maxV))