关注
赶时间瞎写的,凑活着看吧,反正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
相关推荐
点赞 评论 收藏
转发
查看7道真题和解析 产品面经
点赞 评论 收藏
转发
牛客热帖
正在热议
# 和牛牛一起刷题打卡 #
4825次浏览 447人参与
# 机械制造薪资爆料 #
341474次浏览 4057人参与
# 牛客帮帮团来啦!有问必答 #
987832次浏览 15302人参与
# 通信硬件薪资爆料 #
242479次浏览 2283人参与
# 腾讯工作体验 #
146758次浏览 1408人参与
# 如何写一份好简历 #
301368次浏览 4331人参与
# 实习必须要去大厂吗? #
18300次浏览 265人参与
# 你的简历改到第几版了 #
323421次浏览 4860人参与
# 你觉得机械有必要实习吗 #
10264次浏览 140人参与
# 你收到了团子的OC了吗 #
515513次浏览 6138人参与
# 晒一晒我的offer #
3654338次浏览 56953人参与
# 2022毕业生求职现身说法 #
20616次浏览 310人参与
# 毕业租房也有小确幸 #
32329次浏览 1912人参与
# 产品人专业大盘点 #
15119次浏览 120人参与
# 为什么国企只招应届生 #
56215次浏览 411人参与
# 浅聊一下我实习的辛苦费 #
93560次浏览 917人参与
# 视觉/交互/设计岗位评价 #
3369次浏览 61人参与
# 产品人求职现状 #
54990次浏览 795人参与
# 为什么那么多公司毁约 #
55092次浏览 487人参与
# 找工作时遇到的神仙HR #
196680次浏览 1844人参与