赶时间瞎写的,凑活着看吧,反正AC了 while 1: try: m, n = list(map(int, input().split(','))) mtx = [] for i in range(m): mtx.append(list(map(int, input().split(',')))) except: break def isValid(x, y): # print(x,y) return x >= 0 and x < m and y >= 0 and y < n team_count = 0 max_count = 0 seen = set() for i in range(m): for j in range(n): if mtx[i][j] == 0 or (i, j) in seen: continue else: count = 1 q = [(i, j)] seen.add((i, j)) while q: x, y = q.pop() if isValid(x - 1, y) and mtx[x - 1][y] == 1 and (x - 1, y) not in seen: q.append((x - 1, y)) seen.add((x - 1, y)) count += 1 if isValid(x + 1, y) and mtx[x + 1][y] == 1 and (x + 1, y) not in seen: q.append((x + 1, y)) seen.add((x + 1, y)) count += 1 if isValid(x, y - 1) and mtx[x][y - 1] == 1 and (x, y - 1) not in seen: q.append((x, y - 1)) seen.add((x, y - 1)) count += 1 if isValid(x, y + 1) and mtx[x][y + 1] == 1 and (x, y + 1) not in seen: q.append((x, y + 1)) seen.add((x, y + 1)) count += 1 if isValid(x - 1, y - 1) and mtx[x - 1][y - 1] == 1 and (x - 1, y - 1) not in seen: q.append((x - 1, y - 1)) seen.add((x - 1, y - 1)) count += 1 if isValid(x - 1, y + 1) and mtx[x - 1][y + 1] == 1 and (x - 1, y + 1) not in seen: q.append((x - 1, y + 1)) seen.add((x - 1, y + 1)) count += 1 if isValid(x + 1, y - 1) and mtx[x + 1][y - 1] == 1 and (x + 1, y - 1) not in seen: q.append((x + 1, y - 1)) seen.add((x + 1, y - 1)) count += 1 if isValid(x + 1, y + 1) and mtx[x + 1][y + 1] == 1 and (x + 1, y + 1) not in seen: q.append((x + 1, y + 1)) seen.add((x + 1, y + 1)) count += 1 if count > max_count: max_count = count team_count += 1 print(str(team_count) + ',' + str(max_count))
点赞 1

相关推荐

头像
01-29 18:11
海南大学 Java
奔跑的suechil...:单从项目看这个简历不怕被问穿吗 带微服务的项目需要相当多的项目理解和经验诶
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务