728x90
https://school.programmers.co.kr/learn/courses/30/lessons/43162
소스코드
#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;
vector<vector<int>> board;
vector<vector<int>> board;
bool BFS(int start, vector<bool>& visited)
{
queue<int> q;
q.push(start);
visited[start] = true;
while (q.empty() == false)
{
int now = q.front();
q.pop();
for (int next = 0; next < board[0].size(); next++)
{
if (board[now][next] == 0)
continue;
if (visited[next])
continue;
q.push(next);
visited[next] = true;
}
}
}
int solution(int n, vector<vector<int>> computers)
{
int answer = 0;
board = computers;
vector<vector<int>> result;
vector<bool> visited(n, false);
for (int i = 0; i < n; i++)
{
if (visited[i] == false)
{
BFS(i, visited);
answer++;
}
}
return answer;
}
728x90
'코딩공부 > 프로그래머스' 카테고리의 다른 글
[프로그래머스/C++]Lv.3 아이템 줍기(BFS, 달팽이알고리즘) (0) | 2024.07.18 |
---|---|
[프로그래머스/C++]Lv.2 게임 맵 최단거리(BFS) (0) | 2024.07.18 |
[Softeer/C++]Level2 연탄의 크기 (0) | 2024.03.08 |
[Softeer/C++]Level1 나무심 (0) | 2024.03.08 |
[Softeer/C++]Level1 연탄 배달의 시작 (0) | 2024.03.07 |