Skip to content

Commit

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

import java.util.ArrayList;
import java.util.Scanner;

public class 경로탐색_인접리스트 {

static int n, m, answer = 0;
static ArrayList<ArrayList<Integer>> graph;
static int[] check;

public void DFS(int v) {
if(v==n) answer++;
else {
for(int nv : graph.get(v)) {
if(check[nv] == 0) {
check[nv] = 1;
DFS(nv);
check[nv] = 0;
}
}
}
}

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

경로탐색_인접리스트 t = new 경로탐색_인접리스트();

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

graph = new ArrayList<>();

for(int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}

check = new int[n+1];

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

0 comments on commit e65a7ae

Please sign in to comment.