[프로그래머스] 코딩테스트 > 연습깊이/너비 우선 탐색(DFS/BFS) > 네트워크
문제 설명
네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있을 때 컴퓨터 A와 컴퓨터 C도 간접적으로 연결되어 정보를 교환할 수 있습니다. 따라서 컴퓨터 A, B, C는 모두 같은 네트워크 상에 있다고 할 수 있습니다.
컴퓨터의 개수 n, 연결에 대한 정보가 담긴 2차원 배열 computers가 매개변수로 주어질 때, 네트워크의 개수를 return 하도록 solution 함수를 작성하시오.
제한사항- 컴퓨터의 개수 n은 1 이상 200 이하인 자연수입니다.
- 각 컴퓨터는 0부터 n-1인 정수로 표현합니다.
- i번 컴퓨터와 j번 컴퓨터가 연결되어 있으면 computers[i][j]를 1로 표현합니다.
- computer[i][i]는 항상 1입니다.
3 | [[1, 1, 0], [1, 1, 0], [0, 0, 1]] | 2 |
3 | [[1, 1, 0], [1, 1, 1], [0, 1, 1]] | 1 |
예제 #1
아래와 같이 2개의 네트워크가 있습니다.

예제 #2
아래와 같이 1개의 네트워크가 있습니다.

이 문제를 딱 보자마자 떠오른 생각으로 짠 코드이다.
class Solution {
public int solution(int n, int[][] computers) {
// 연결된 컴퓨터를 확인하는 String array
String[] com = new String[n];
for(int i=0;i<n;i++)
com[i]=i+"번 컴퓨터";
// 네트워크 수
int answer = findNotNull(n, computers, com);
return answer;
}
public int findNotNull(int n, int[][] computers, String[] com) {
int answer = 0;
for(int i=0;i<n;i++) {
if(com[i] != null) {
makeNull(computers, i, com);
answer+=1;
}
}
return answer;
}
public void makeNull(int[][] computers, int i, String[] com) {
// 연결된 com을 null로 만들어줌
for(int temp=0; temp<com.length; temp++) {
if(computers[i][temp]==1) {com[temp]=null;}
}
//
if(i+1==com.length) {
return;
}
//
findNull(computers, i+1, com);
}
public void findNull(int[][] computers, int i, String[] com) {
if(com[i]==null) {
makeNull(computers, i, com);
}else {
if(i+1==com.length) {return;}
findNull(computers, i+1, com);
}
}
}
물론 답은 틀렸다,,,,, 하,,,,
아이디어는 컴퓨터 이름을 담고 있는 com 이라는 array를 만들어서
네트워크가 연결되어 있는 컴퓨터는 값을 null로 만들어보자는 것이었는데,,,,
이게 나도 짜면서 생각이 든게
null이 중복되겠구나 생각이 들었다,,,,ㅠㅠㅠㅠㅠ
짜면서도 이건 아닌거 같은데 라는 생각이 들었다,,,
그래서 다른 접근!
거의 같은 방식이긴 한데, 네트워크에 연결이 된 컴퓨터인지 아닌지 판단하는 boolean[] connected 를 만든는 것
class Solution {
public int solution(int n, int[][] computers) {
// 네트워크 연결 유무
boolean[] connected = new boolean[n];
for(boolean temp:connected)
temp=false;
// 네트워크 수
int answer = 0;
for(int i=0;i<n;i++) {
if(connected[i]==false) {
makeCon(computers, i, connected);
answer+=1;
}
}
return answer;
}
public void makeCon(int[][] computers, int i, boolean[] connected) {
// 연결된 connected을 true로 만들어줌
for(int temp=0; temp<connected.length; temp++) {
if(computers[i][temp]==1) {connected[temp]=true;}
}
//
if(i+1==connected.length) {
return;
}
//
findCon(computers, i+1, connected);
}
public void findCon(int[][] computers, int i, boolean[] connected) {
if(connected[i]==true) {
makeCon(computers, i, connected);
}else {
if(i+1==connected.length) {return;}
findCon(computers, i+1, connected);
}
}
}
하지만 이것도 결국 실패,,,,,
근데 아무리 생각해봐도 이 방법이 틀린게 아닌거 같다
분명 이게 맞는데 왜 틀린지 생각해봤는데 findCon() 같은 method는 없어도 된다는 결론을 내렸다!!
class Solution {
public int solution(int n, int[][] computers) {
// 네트워크 연결 유무
boolean[] connected = new boolean[n];
for(boolean temp:connected)
temp=false;
// 네트워크 수
int answer = 0;
for(int i=0;i<n;i++) {
if(connected[i]==false) {
makeCon(computers, i, connected);
answer+=1;
}
}
return answer;
}
public void makeCon(int[][] computers, int i, boolean[] connected) {
// 연결된 connected을 true로 만들어줌
for(int temp=0; temp<connected.length; temp++) {
if(connected[temp]==false && computers[i][temp]==1) {
connected[temp]=true;
makeCon(computers, temp, connected);
}
}
}
}
그래 역시 이게 맞다,,,,,ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ
오래결렸다,,,,,,,,,,,,,,,,,,,,,,
재귀를 잘 쓰는 것도 능력인 것 같다