justgo_developer

[JAVA] 백준 1260 DFS와 BFS 본문

IT/알고리즘

[JAVA] 백준 1260 DFS와 BFS

다날92 2018. 1. 7. 23:42
728x90
반응형



import java.util.LinkedList;

import java.util.Queue;

import java.util.Scanner;


public class Main {

static int N;

static int M;

static int V;

static int[][]G;

static boolean[]visit;

public static void dfs(int n){

visit[n]=true;

System.out.print(n+" ");

for(int i=1; i<=N;i++){

if(G[n][i]==1 && visit[i]==false){

dfs(i);

}

}

}

public static void bfs(int n){

Queue<Integer> q = new LinkedList<Integer> ();

q.offer(n);

visit[n]=true;

int temp;

while(!q.isEmpty()){

temp = q.poll();

System.out.print(temp+" ");

for(int i=0;i<=N;i++){

if(G[temp][i]==1 && visit[i]==false){

q.offer(i);

visit[i]=true;

}

}

}

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

N = sc.nextInt();

M = sc.nextInt();

V = sc.nextInt();

G = new int[1001][1001];

visit = new boolean[10001];

int t1,t2;

for(int i=0;i<M;i++){

t1 = sc.nextInt();

t2 = sc.nextInt();

G[t1][t2] = G[t2][t1] =1;

}

dfs(V);

for(int i=1;i<=N;i++)

visit[i]=false;

System.out.println("");

bfs(V);

}

}



728x90
반응형