diff --git a/bishoe01/BFS_DFS/Count Servers that Communicate.swift b/bishoe01/BFS_DFS/Count Servers that Communicate.swift new file mode 100644 index 0000000..dcae2cc --- /dev/null +++ b/bishoe01/BFS_DFS/Count Servers that Communicate.swift @@ -0,0 +1,67 @@ +class Solution { + func countServers(_ grid: [[Int]]) -> Int { + var visitedGraph = Array(repeating: Array(repeating: false, count: grid[0].count), count: grid.count) + func isRange(x: Int, y: Int) -> Bool { + return x >= 0 && y >= 0 && x < grid.count && y < grid[0].count && grid[x][y] == 1 && visitedGraph[x][y] == false + } + var answer = 0 + for i in 0 ..< grid.count { + for j in 0 ..< grid[0].count { + var queue = [(Int, Int)]() + var pointer = 0 + var connectCnt = 0 + + if isRange(x: i, y: j) { + queue.append((i, j)) + visitedGraph[i][j] = true // 추가 + connectCnt += 1 + } + + while pointer < queue.count { + let tmp = queue[pointer] + let currentX = tmp.0, currentY = tmp.1 + +// 굴러갈 수 있는지 확인 / 2차 풀이 : 사실상 이부분만 고쳐주면 될 것같은데, 행 - 열이라 + +// 행 계산 + for row in 0 ..< grid.count { +// print("next:", nextX, nextY) + if isRange(x: row, y: currentY) { +// print("Hi") + queue.append((row, currentY)) + visitedGraph[row][currentY] = true + connectCnt += 1 +// print("ROW", row, currentY) + } + } +// 열 계싼 + for col in 0 ..< grid[0].count { +// print("next:", nextX, nextY) + if isRange(x: currentX, y: col) { +// print("Hi") + queue.append((currentX, col)) + visitedGraph[currentX][col] = true + connectCnt += 1 +// print("COL", currentX, col) + } + } + + pointer += 1 + } + if connectCnt > 1 { + answer += connectCnt + connectCnt = 1 + } + } + } + +// print(answer) + return answer + } +} + +// +// Solution().countServers([[1, 0], [1, 1]]) +// Solution().countServers([[1, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) +// Solution().countServers([[1, 0], [1, 1]]) +Solution().countServers([[1, 0, 0, 1, 0], [0, 0, 0, 0, 0], [0, 0, 0, 1, 0]]) diff --git a/bishoe01/README.md b/bishoe01/README.md index 6f4f1ca..2ac2f86 100644 --- a/bishoe01/README.md +++ b/bishoe01/README.md @@ -3,5 +3,6 @@ | 차시 | 날짜 | 문제유형 | 링크 | 풀이 | |:----:|:---------:|:----:|:-----:|:----:| | 1차시 | 2025.03.27 | 구현 | [[3차] 파일명 정렬](https://school.programmers.co.kr/learn/courses/30/lessons/17686)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/1| -| 1차시 | 2025.04.09 | DP | [1277. Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/description/?envType=problem-list-v2&envId=dynamic-programming)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/11| +| 2차시 | 2025.04.09 | DP | [1277. Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/description/?envType=problem-list-v2&envId=dynamic-programming)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/11| +| 3차시 | 2025.04.30 | DP | [1267. Count Servers that Communicate](https://leetcode.com/problems/count-servers-that-communicate/description/?envType=daily-question&envId=2025-04-29)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/16| ---