Programmers
Programmers LV.3 네트워크
Developer Garam.Choi
2020. 12. 31. 14:57
이번 문제는 조금 난해했던거 같다.
import java.util.*;
class Solution {
static int answer = 0;
public int solution(int n, int[][] computers) {
boolean[][] checkComputer = new boolean[computers.length][n];
for(int i = 0; i<computers.length; i++){
if(!checkComputer[i][i]){
networkDfs(computers , i , checkComputer);
answer++;
}
}
return answer;
}
public void networkDfs(int[][] computers , int firstIndex ,boolean[][] checkComputer ){
for(int i=0; i< computers.length; i++){
if(checkComputer[firstIndex][i] == false && computers[firstIndex][i] == 1){
checkComputer[firstIndex][i] = true;
networkDfs(computers , i ,checkComputer);
}
}
}
}
우선 개발은 위와 같이 진행하였다,
DFS / BFS를 생각이 잘안나서 처음부터 다시 보고있었는데,
해당 문제를 보면 기타 블로그들을 보니 좀 이해가 되지 않는 경우들이 많았는데,
boolean[][] checkComputer = new boolean[computers.length][n];
와 같이 DFS에서 Check를 해야하는데,
boolean[] checkComputer = new boolean[computers.length]; 와 같이 체크하는 형식들이 많앗던거같다.
이려면
int[][] computers ={{1, 1, 0}, {1, 1, 1}, {0, 1, 1}}; 형식에서 1번째에 들어왔을때 이미 들어온거라고 바로 빠져나올텐데 완전탐색이 안되지 않나..? 이 생각이 들었다.
내가 잘못생각하고있는건가, 컴퓨터 수가 늘어나면 바로 에러가 날텐데..
잘 이해가 되지 않았다.