🌷기본 코드 구현 시 필요한 것

🌷 로직

  1. while (스택에 값이 있는 동안)

  2. 방문 여부 검사

  3. state 변수(현재 방문한 변수)에 대한 처리(문제마다 다름)

  4. 다음에 방문해야할 자료에 대해 범위, 방문 여부 등 문제에 따라 조건 검사

  5. 조건에 위배되지 않으면 스택에 push or 재귀 함수 호출

boolean isVisited[] = new boolean[n];
Stack<Integer> stack = new Stack<>();

while (!stack.isEmpty()) {
    int state = stack.pop();
    if (isVisited[]) {
    	// continue 또는 리턴 
    }
    isVisted[state] = true;
    for (int next : getNext(state)) {
    	if (!isVisted[next]) {
        	stack.push(next);
        }
    }
}

// 재귀로 구현한다면 
static void DFS(int node) {
	if (visited[node]) {
		return ;
    } // 이미 방문한 노드인 경우엔 수행 X
    visited[node] = true; // 한 번 지난 노드는 다시 안 지나가게끔
    for (int i : graph[node]) {
    	if (!visited[i]) {
        	DFS(i);
        }
    }
}