[백준](Java) 1260 - DFS와 BFS
문제 링크
문제 풀이
코드
package dfs;
import java.util.*;
import java.util.LinkedList;
import java.util.Scanner;
public class BJ1260 {
    static int [][] map;
    static boolean [] chkbfs;
    static boolean [] chkdfs;
    static int [] list;
    static int n;
    static int m;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        int v = sc.nextInt();
        map = new int[n+1][n+1];
        chkbfs = new boolean[n+1];
        chkdfs = new boolean[n+1];
        for(int i=0; i<m; i++){
            int y = sc.nextInt();
            int x = sc.nextInt();
            map[y][x]=1;
            map[x][y]=1;
        }
        list = new int[n];
        dfs(v);
        chkbfs[v] = true;
        System.out.println("");
        bfs(v);
    }
    public static void dfs(int v){
        System.out.print(v+" ");
        chkdfs[v]=true;
        for(int i=1; i<=n; i++){
            if(map[v][i]==1 && !chkdfs[i]){
                dfs(i);
            }
        }
    }
    public static void bfs(int v){
        Queue<Integer> queue = new LinkedList<>();
        queue.add(v);
        while(!queue.isEmpty()){
            int temp = queue.poll();
            System.out.print(temp+" ");
            for(int i=1; i<=n; i++){
                if(map[temp][i]==1 && !chkbfs[i]){
                    queue.add(i);
                    chkbfs[i] = true;
                }
            }
        }
    }
}
Author And Source
이 문제에 관하여([백준](Java) 1260 - DFS와 BFS), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@courage331/백준Java-1260-DFS와-BFS저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)