Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 풀이
- qorwns
- 조세퍼스 순열
- 해시구현
- 백준 2447
- c#
- ㅣ풀이
- 해시 구현
- AVL 시간 복잡도
- 1764
- 버킷 정렬
- 백준
- 백준 17779
- 백준 1158
- 구현
- 백준 5397
- 자료구조
- 별 찍기 10
- 5397
- heap
- 게리멘더링2
- 스택의 특징
- 백준 17822
- dfs
- 원판 돌리기
- 백준 17471
- 시간 복잡도
- Stack 이란
- 백준 1406
- C/C++ 구현
Archives
- Today
- Total
홍시홍의 프로그래밍
[프로그래머스] 네트워크 본문
분류 : dfs/bfs
요구사항
연결된 네트워크 사이클 구하기
풀이
dfs로 풀이하면 직관적으로 풀 수 있으나, bfs로 풀어보고 싶어 bfs로 풀어보았다
1. i->j로 연결되있고, 방문한 적이 없으면 bfs를 시작한다.
2. i에 연결되어 있는 노드를 큐에 넣고, visit[node]=1로 설정한다.
3. 큐가 빌때까지 1~2반복한다
#include <string>
#include <vector>
#include <queue>
#include <string.h>
#include <iostream>
using namespace std;
struct go {
int x;
int y;
};
int solution(int n, vector<vector<int>> computers) {
int answer = 0;
int visit[220] = { 0, };
int qvisit[220][220] = { 0, };
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (computers[i][j] == 1) {
if (visit[j] == 0) {
visit[i] = 1;
queue<int> q;
answer++;
q.push(i);
while (!q.empty()) {
int now = q.front();
q.pop();
for (int k = 0; k < computers[now].size(); k++) {
int next = computers[now][k];
if (computers[now][k] == 1) {
if (visit[k] == 0) {
cout << k << endl;
visit[k] = 1;
q.push(k);
}
}
}
}
}
}
}
}
return answer;
}
'알고리즘 문제풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] k번째 수 (0) | 2020.04.18 |
---|---|
[프로그래머스] 단어 변화 (0) | 2020.04.12 |
[프로그래머스] 타겟 넘버 (0) | 2020.04.10 |
[프로그래머스] 베스트앨범 (0) | 2020.04.10 |
[프로그래머스] 멀쩡한 사각형 (0) | 2020.04.05 |
Comments