

public class Main {
public void DFS(int n) {
if(n == 0) {
return;
} else {
DFS(n - 1);
System.out.print(n + " ");
}
}
public static void main(String[] args) {
Main T = new Main();
T.DFS(3);
}
}

public class Main {
public void DFS(int n) {
if(n == 0) {
return;
} else {
DFS(n / 2);
System.out.print(n % 2 + " ");
}
}
public static void main(String[] args) {
Main T = new Main();
T.DFS(11);
}
}

public class Main {
public int DFS(int n) {
if(n == 1) {
return 1;
} else {
return n * DFS(n - 1);
}
}
public static void main(String[] args) {
Main T = new Main();
System.out.println(T.DFS(5));
}
}


재귀함수는 스택 프레임 때문에 for문 보다 오래 걸린다.
public class Main {
static int[] fibo;
public int DFS(int n) {
if(fibo[n] > 0) {
return fibo[n];
}
if(n == 1) {
return fibo[n] = 1;
} else if(n == 2) {
return fibo[n] = 1;
} else {
return fibo[n] = DFS(n - 2) + DFS(n - 1);
}
}
public static void main(String[] args) {
Main T = new Main();
int n = 5;
fibo = new int[n + 1];
T.DFS(n);
for(int i =1; i <= n; i++) {
System.out.print(fibo[i] + " ");
}
}
}

class Node {
int data;
Node lt, rt;
public Node(int val) {
data = val;
lt = rt = null;
}
}
public class Main {
Node root;
public void DFS(Node root) {
if(root == null) {
return;
} else {
// System.out.print(root.data + " "); // 전위 순위
DFS(root.lt);
// System.out.print(root.data + " "); // 중위 순위
DFS(root.rt);
// System.out.print(root.data + " "); // 후위 순위
}
}
public static void main(String[] args) {
Main tree = new Main();
tree.root = new Node(1);
tree.root.lt = new Node(2);
tree.root.rt = new Node(3);
tree.root.lt.lt = new Node(4);
tree.root.lt.rt = new Node(5);
tree.root.rt.lt = new Node(6);
tree.root.rt.rt = new Node(7);
tree.DFS(tree.root);
}
}


public class Main {
static int n;
static int[] ch;
public void DFS(int L) {
if(L == n + 1) {
String tmp = "";
for(int i = 1; i <= n; i++) {
if(ch[i] == 1) {
tmp += (i + " ");
}
}
if(tmp.length() > 0) {
System.out.println(tmp);
}
} else {
ch[L] = 1;
DFS(L + 1);
ch[L] = 0;
DFS(L + 1);
}
}
public static void main(String[] args) {
Main T = new Main();
n = 3;
ch = new int[n + 1];
T.DFS(1);
}
}

import java.util.LinkedList;
import java.util.Queue;
class Node {
int data;
Node lt, rt;
public Node(int val) {
data = val;
lt = rt = null;
}
}
public class Main {
Node root;
public void DFS(Node root) {
Queue<Node> Q = new LinkedList<>();
Q.offer(root);
int L = 0;
while(!Q.isEmpty()) {
int len = Q.size();
System.out.print(L + " : ");
for(int i = 0; i < len; i++) {
Node cur = Q.poll();
System.out.print(cur.data + " ");
if(cur.lt != null) {
Q.offer(cur.lt);
}
if(cur.rt != null) {
Q.offer(cur.rt);
}
}
L++;
System.out.println();
}
}
public static void main(String[] args) {
Main tree = new Main();
tree.root = new Node(1);
tree.root.lt = new Node(2);
tree.root.rt = new Node(3);
tree.root.lt.lt = new Node(4);
tree.root.lt.rt = new Node(5);
tree.root.rt.lt = new Node(6);
tree.root.rt.rt = new Node(7);
tree.DFS(tree.root);
}
}

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
int answer = 0;
int[] dis = {1, -1, 5};
int[] ch;
Queue<Integer> Q = new LinkedList<>();
public int BFS(int s, int e) {
ch = new int[10001];
ch[s] = 1;
Q.offer(s);
int L = 0;
while(!Q.isEmpty()) {
int len = Q.size();
for(int i = 0 ; i < len; i++) {
int x = Q.poll();
for(int j = 0; j < 3; j++) {
int nx = x + dis[j];
if(nx == e) {
return L + 1;
}
if(nx >= 1 && nx <= 10000 && ch[nx] == 0) {
ch[nx] = 1;
Q.offer(nx);
}
}
}
L++;
}
return 0;
}
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
int s = kb.nextInt();
int e = kb.nextInt();
System.out.println(T.BFS(s, e));
}
}

class Node {
int data;
Node lt, rt;
public Node(int val) {
data = val;
lt = rt = null;
}
}
public class Main {
Node root;
public int DFS(int L, Node root) {
if(root.lt == null && root.rt == null) {
return L;
} else {
return Math.min(DFS(L + 1, root.lt), DFS(L + 1, root.rt));
}
}
public static void main(String[] args) {
Main tree = new Main();
tree.root = new Node(1);
tree.root.lt = new Node(2);
tree.root.rt = new Node(3);
tree.root.lt.lt = new Node(4);
tree.root.lt.rt = new Node(5);
System.out.println(tree.DFS(0, tree.root));
}
}

import java.util.LinkedList;
import java.util.Queue;
class Node {
int data;
Node lt, rt;
public Node(int val) {
data = val;
lt = rt = null;
}
}
public class Main {
Node root;
public int BFS(Node root) {
Queue<Node> Q = new LinkedList<>();
Q.offer(root);
int L = 0;
while(!Q.isEmpty()) {
int len = Q.size();
for(int i = 0; i < len; i++) {
Node cur = Q.poll();
if(cur.lt == null && cur.rt == null) {
return L;
}
if(cur.lt != null) {
Q.offer(cur.lt);
}
if(cur.rt != null) {
Q.offer(cur.rt);
}
}
L++;
}
return 0;
}
public static void main(String[] args) {
Main tree = new Main();
tree.root = new Node(1);
tree.root.lt = new Node(2);
tree.root.rt = new Node(3);
tree.root.lt.lt = new Node(4);
tree.root.lt.rt = new Node(5);
System.out.println(tree.BFS(tree.root));
}
}


import java.util.Scanner;
public class Main {
static int n, m, answer = 0;
static int[][] graph;
static int[] ch;
public void DFS(int v) {
if(v == n) {
answer++;
} else {
for(int i = 1; i <= n; i++) {
if(graph[v][i] == 1 && ch[i] == 0) {
ch[i] = 1;
DFS(i);
ch[i] = 0;
}
}
}
}
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
n = kb.nextInt();
m = kb.nextInt();
graph = new int[n + 1][n + 1];
ch = new int[n + 1];
for(int i = 0; i < m; i++) {
int a = kb.nextInt();
int b = kb.nextInt();
graph[a][b] = 1;
}
ch[1] = 1;
T.DFS(1);
System.out.println(answer);
}
}


import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static int n, m, answer = 0;
static ArrayList<ArrayList<Integer>> graph;
static int[] ch;
public void DFS(int v) {
if(v == n) {
answer++;
} else {
for(int nv : graph.get(v)) {
if(ch[nv] == 0) {
ch[nv] = 1;
DFS(nv);
ch[nv] = 0;
}
}
}
}
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
n = kb.nextInt();
m = kb.nextInt();
graph = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i <= n; i++) {
graph.add(new ArrayList<Integer>());
}
ch = new int[n + 1];
for(int i = 0; i < m; i++) {
int a = kb.nextInt();
int b = kb.nextInt();
graph.get(a).add(b);
}
ch[1] = 1;
T.DFS(1);
System.out.println(answer);
}
}

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static int n, m;
static ArrayList<ArrayList<Integer>> graph;
static int[] ch, dis;
public void BFS(int v) {
Queue<Integer> queue = new LinkedList<>();
ch[v] = 1;
dis[v] = 0;
queue.offer(v);
while(!queue.isEmpty()) {
int cv = queue.poll();
for(int nv : graph.get(cv)) {
if(ch[nv] == 0) {
ch[nv] = 1;
queue.offer(nv);
dis[nv] = dis[cv] + 1;
}
}
}
}
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
n = kb.nextInt();
m = kb.nextInt();
graph = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i <= n; i++) {
graph.add(new ArrayList<Integer>());
}
ch = new int[n + 1];
dis = new int[n + 1];
for(int i = 0; i < m; i++) {
int a = kb.nextInt();
int b = kb.nextInt();
graph.get(a).add(b);
}
T.BFS(1);
for(int i = 2; i <= n; i++) {
System.out.println(i + " : " + dis[i]);
}
}
}