프로그래머스/3레벨
[프로그래머스/C++] 네트워크
Koalitsiya
2024. 3. 22. 14:59
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
- 3레벨에서 의외로 기초적인 dfs/bfs 문제
- 그래도 변수명을 제대로 썼는지 확인하자, 다음 노드로 갱신해야하는데 현재 노드로 갱신하고 있는걸 코드 실행 해보고 알았다...
#include <string>
#include <vector>
#include <queue>
using namespace std;
bool isVisited[200] = {false};
int answer = 0;
queue<int> q;
void bfs(int startNode, int n, vector<vector<int>> computers) {
q.push(startNode);
isVisited[startNode] = true;
while(!q.empty()) {
int curNode = q.front();
q.pop();
isVisited[nextNode] = true;
for(int nextNode = 0; nextNode < n; nextNode++) {
if(computers[curNode][nextNode] == 1 && !isVisited[nextNode]) {
q.push(nextNode);
}
}
}
answer++;
}
int solution(int n, vector<vector<int>> computers) {
for(int i = 0; i < n; i++) {
if(!isVisited[i]) {
bfs(i, n, computers);
}
}
return answer;
}