프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
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;
}
'프로그래머스 > 3레벨' 카테고리의 다른 글
[프로그래머스/C++] 최고의 집합 (0) | 2024.03.22 |
---|---|
[프로그래머스/C++] 등굣길 (0) | 2024.03.22 |
[프로그래머스/C++] 야근 지수 (1) | 2024.03.22 |
[프로그래머스/C++] 이중우선순위큐 (0) | 2024.03.22 |
[프로그래머스/C++] 정수 삼각형 (1) | 2024.03.22 |