[qwq求助]最大连通区域面积

17 views
Skip to first unread message

张正林-1700018419

unread,
Dec 29, 2018, 5:33:34 AM12/29/18
to cs101pku
def matrix_protect(Matrix, a):
Matrix.insert(0, [a]*len(Matrix[0]))
Matrix.append([a]*len(Matrix[0]))
for i in Matrix:
i.insert(0,a)
i.append(a)

def check(i,j):
global cnt
p[i][j] = '.'
cnt += 1
if p[i-1][j-1] == 'W':
check(i-1,j-1)
if p[i][j-1] == 'W':
check(i,j-1)
if p[i+1][j-1] == 'W':
check(i+1,j-1)
if p[i-1][j] == 'W':
check(i-1,j)
if p[i+1][j] == 'W':
check(i+1,j)
if p[i-1][j+1] == 'W':
check(i-1,j+1)
if p[i][j+1] == 'W':
check(i,j+1)
if p[i+1][j+1] == 'W':
check(i+1,j+1)

s = []
t = int(input())
while (t>0):
n,m = map(int,input().split())
p = [list(input())for j in range(n)]
# print(p)
matrix_protect(p,'.')
# print(p)
for i in range(1,n+1):
for j in range(1,m+1):
if p[i][j] == 'W':
cnt = 0
check(i,j)
s.append(cnt)
print(max(s)if s != [] else 0)
t -=1

为什么总是WA啊,绝望

刘然

unread,
Dec 31, 2018, 6:06:12 AM12/31/18
to cs101pku
你的列表s定义的太早了,这样每个循环里面的连通区域都会加在s里面,因此如果有多组测试数据,除了第一组,后面的组中s均不为空。
你把测试数据的大列表放在前面,小列表放在后面,出来的数据就是错的。
把列表s换在循环里定义应该就可以ac了。

在 2018年12月29日星期六 UTC+8下午6:33:34,张正林-1700018419写道:
Reply all
Reply to author
Forward
0 new messages