关注
iOS Swift
import Foundation
/**
m行, n列
10,10
0,0,0,0,0,0,0,0,0,0
0,0,0,1,1,0,1,0,0,0
0,1,0,0,0,0,0,1,0,1
1,0,0,0,0,0,0,0,1,1
0,0,0,1,1,1,0,0,0,1
0,0,0,0,0,0,1,0,1,1
0,1,1,0,0,0,0,0,0,0
0,0,0,1,0,1,0,0,0,0
0,0,1,0,0,1,0,0,0,0
0,1,0,0,0,0,0,0,0,0
6,8
*/
class Node {
let value: Int
var check: Bool = false
init(_ value: Int) {
self.value = value
}
}
let first = readLine()!.split(separator: ",")
let (m, n) = (Int(first[0])!, Int(first[1])!)
var teamCount = 0
var count = 0
var maxCount = 0
var dict: [String: Node] = [:];
for row in 0..<m {
let line = readLine()!.split(separator: ",").map{ Int($0) }.compactMap { $0 }
for column in 0..<n {
let location = "\(row)\(column)"
dict[location] = Node(line[column])
}
}
func traverse(node: Node, row: Int, column: Int) {
count += 1
node.check = true
if column != 0, row != 0, let topleft = dict["\(row - 1)\(column - 1)"], topleft.value == 1, !topleft.check {
traverse(node: topleft, row: row - 1, column: column - 1)
}
if row != 0, let top = dict["\(row - 1)\(column)"], top.value == 1, !top.check {
traverse(node: top, row: row - 1, column: column)
}
if row != 0, column != n - 1, let topright = dict["\(row - 1)\(column + 1)"], topright.value == 1, !topright.check {
traverse(node: topright, row: row - 1, column: column + 1)
}
if column != 0, let left = dict["\(row)\(column - 1)"], left.value == 1, !left.check {
traverse(node: left, row: row, column: column - 1)
}
if column != n - 1, let right = dict["\(row)\(column + 1)"], right.value == 1, !right.check {
traverse(node: right, row: row, column: column + 1)
}
if row != m - 1, column != 0, let buttomleft = dict["\(row + 1)\(column - 1)"], buttomleft.value == 1, !buttomleft.check {
traverse(node: buttomleft, row: row + 1, column: column - 1)
}
if row != m - 1, let buttom = dict["\(row + 1)\(column)"], buttom.value == 1, !buttom.check {
traverse(node: buttom, row: row + 1, column: column)
}
if row != m - 1, column != 0, let buttomright = dict["\(row + 1)\(column + 1)"], buttomright.value == 1, !buttomright.check {
traverse(node: buttomright, row: row + 1, column: column + 1)
}
}
for row in 0..<m {
for column in 0..<n {
let location = "\(row)\(column)"
guard let node = dict[location], node.value == 1, !node.check else { continue }
teamCount += 1
maxCount = count > maxCount ? count : maxCount
count = 0
traverse(node: node, row: row, column: column)
}
}
maxCount = count > maxCount ? count : maxCount
print(teamCount, maxCount)
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
牛客热帖
正在热议
# 25届秋招总结 #
300551次浏览 2660人参与
# 如果不工作真的会快乐吗 #
59185次浏览 516人参与
# 百度开奖 #
162255次浏览 974人参与
# 地方国企笔面经互助 #
3791次浏览 10人参与
# 美团求职进展汇总 #
1327257次浏览 12449人参与
# 选完offer后,你后悔学本专业吗 #
19919次浏览 144人参与
# 阿里云管培生offer #
17280次浏览 294人参与
# 正在实习的你,几点下班 #
51839次浏览 389人参与
# 国央企薪资爆料 #
8295次浏览 67人参与
# 如何一边实习一边秋招 #
992085次浏览 12638人参与
# 提前批简历挂麻了怎么办 #
146417次浏览 1948人参与
# 学历or实习经历,哪个更重要 #
50995次浏览 402人参与
# 海康威视求职进展汇总 #
398779次浏览 3406人参与
# 米哈游求职进展汇总 #
175894次浏览 1458人参与
# 求职遇到的搞笑事件 #
70778次浏览 577人参与
# 投递实习岗位前的准备 #
1179167次浏览 18394人参与
# 面试体验感最好的是哪家? #
85075次浏览 845人参与
# 实习生应该准时下班吗 #
167404次浏览 1159人参与
# 得物求职进展汇总 #
66224次浏览 682人参与
# 网申一定要掌握的小技巧 #
5326次浏览 53人参与
# 招聘要求与实际实习内容不符怎么办 #
10267次浏览 273人参与
# 0offer是寒冬太冷还是我太菜 #
898224次浏览 8012人参与