Skip to content

Commit

Permalink
Section7-10. 경로탐색 (인접행렬, DFS)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuseon25 committed Feb 1, 2025
1 parent 1d4a94d commit 620b21e
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Section07/경로탐색_인접행렬.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package 인프런.Section07;

import java.util.Scanner;

public class 경로탐색_인접행렬 {

static int[][] graph;
static int n, m, answer = 0;
static int[] check;

public void DFS(int v) {
if(v == n) answer++;
else {
for(int i = 1; i <= n; i++) {
if(graph[v][i] == 1 && check[i] == 0) {
check[i] = 1;
DFS(i);
check[i] = 0; //백트래킹 할때 check 한 거 다시 풀기
}
}
}

}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

경로탐색_인접행렬 t = new 경로탐색_인접행렬();

n = scanner.nextInt();
m = scanner.nextInt();

graph = new int[n+1][n+1];
check = new int[n+1];

for(int i = 0; i < m; i++) {
int a = scanner.nextInt();
int b = scanner.nextInt();
t.graph[a][b] = 1;
}
check[1] = 1;
t.DFS(1);
System.out.print(answer);
}
}

0 comments on commit 620b21e

Please sign in to comment.