알고리즘

DFS 기본 알고리즘

changha. 2022. 9. 28. 19:37

순서대로 재귀, stack 

import java.util.*;

public class Main
{
    static final int MAX_N = 10;
    static int N, E;
    static int[][] Graph = new int[MAX_N][MAX_N];
    static boolean[] visited = new boolean[MAX_N];
    
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		N = sc.nextInt();
		E = sc.nextInt();
		for (int i = 0; i < E; i++){
		    int u = sc.nextInt();
		    int v = sc.nextInt();
		    Graph[u][v] = Graph[v][u] = 1;
		    
		}
		
		dfs(0);
	}
	
	static void dfs(int node){
	    visited[node] = true;
	    
	    System.out.print(node + " ");
	    
	    for(int next = 0; next < N; next++){
	        if(!visited[next] && Graph[node][next] != 0){
	            dfs(next);
	        }
	    }
	}
}
import java.util.*;

public class Main
{
    static final int MAX_N = 10;
    static int N, E;
    static int[][] Graph = new int[MAX_N][MAX_N];
    
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		N = sc.nextInt();
		E = sc.nextInt();
		for (int i = 0; i < E; i++){
		    int u = sc.nextInt();
		    int v = sc.nextInt();
		    Graph[u][v] = Graph[v][u] = 1;
		    
		}
		
		dfs(0);
	}
	
	static void dfs(int node){
	    boolean[] visited = new boolean[MAX_N];
	    Stack<Integer> mystack = new Stack<>();
	    
	    mystack.push(node);
	    
	    while(!mystack.empty()){
	        int curr = mystack.pop();
	        
	        if(visited[curr]) continue;
	        
	        visited[curr] = true;
	        System.out.print(curr + " ");
	        
	        for(int next = 0; next < N; next++){
	            if(!visited[next] && Graph[curr][next] != 0){
	                mystack.push(next);
	            }
	        }
	        
	    }
	}
}

 

예제 문제: 

INPUT

5

0 0 0 0 0 

0 0 1 1 1

1 1 1 1 1

0 0 0 0 0

1 1 1 1 1

1 1 3

 

OUPUT

3 3 3 3 3

3 3 1 1 1

1 1 1 1 1

0 0 0 0 0

1 1 1 1 1

 

 

 

import java.util.*;

public class Main
{   
    
    static final int MAX_N = 10;
    static int N;
    static int[][] board = new int[MAX_N][MAX_N];
    static int[][] D = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    
    static class Point {
        Point (int r, int c) {
            row = r; col = c;
        }
        int row, col;
    }
    
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		N = sc.nextInt();
		
		for(int i = 0; i < N; i++){
		    for(int j = 0; j < N; j++){
		        board[i][j] = sc.nextInt();
		    }
		}
		
		int sRow = sc.nextInt(); int sCol = sc.nextInt();
		int color = sc.nextInt();
		
		dfs(sRow, sCol, color);
		
		for(int i = 0; i < N; i++){
		    for(int j = 0; j < N; j++){
		        System.out.print(board[i][j] + " ");
		    }
		    System.out.println();
		}
	}
	
	static void dfs(int sRow, int sCol, int color){
	    boolean[][] visited = new boolean[MAX_N][MAX_N];
	    Stack<Point> mystack = new Stack<>();
	    mystack.push(new Point(sRow, sCol));
	    
	    while(!mystack.empty()){
	        Point curr = mystack.pop();
	        
	        if(visited[curr.row][curr.col]) continue;
	        
	        visited[curr.row][curr.col] = true;
	        board[curr.row][curr.col] = color;
	        
	        for(int i = 0; i < 4; i++){
	            int nr = curr.row + D[i][0]; int nc = curr.col + D[i][1];
	            if(nr < 0 || nr > N - 1 || nc < 0 || nc > N - 1) continue;
	            if(visited[nr][nc]) continue;
	            if(board[nr][nc] == 1) continue;
	            
	            mystack.push(new Point(nr, nc));
	        }
	        
	    }
	    
	}
}

'알고리즘' 카테고리의 다른 글

<백준> 2606번 자바 알고리즘[BFS]  (0) 2022.10.06
[백준] 2178번 (java 자바)  (0) 2022.10.05
BFS 기본 알고리즘  (0) 2022.09.25
[백준] 16236번 (java 자바)  (0) 2022.09.20
[백준] 14500번 (java 자바)  (0) 2022.08.24