문제링크: https://www.acmicpc.net/problem/1260
public class Main {
public static int[][] createGraph(int N, int M) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[][] graph = new int[N][N];
int vertex1;
int vertex2;
for (int i = 0; i < M; ++i) {
StringTokenizer st = new StringTokenizer(br.readLine());
vertex1 = Integer.parseInt(st.nextToken());
vertex2 = Integer.parseInt(st.nextToken());
graph[vertex1-1][vertex2-1]=1;
graph[vertex2-1][vertex1-1]=1;
}
return graph;
}
public static void DFS(int[][] graph, int start_vertex) {
HashSet<Integer> visited = new HashSet<>();
Stack<Integer> will_visit = new Stack<>();
visited.add(start_vertex);
will_visit.push(start_vertex);
while (will_visit.isEmpty() == false) {
Integer current_vertex = will_visit.pop();
System.out.print(current_vertex+" ");
for(int i=0; i<graph.length;i++)
{
if(graph[current_vertex-1][i]==1&&!visited.contains(i+1))
{
visited.add(i+1);
will_visit.add(i+1);
break;
}
}
}
}
public static void BFS(int[][] graph, int start_vertex) {
HashSet<Integer> visited = new HashSet<>();
Queue<Integer> will_visit = new LinkedList<>();
visited.add(start_vertex);
will_visit.add(start_vertex);
while (will_visit.isEmpty() == false) {
Integer current_vertex = will_visit.remove();
System.out.print(current_vertex+" ");
for(int i=0; i<graph.length;i++)
{
if(graph[current_vertex-1][i]==1&&!visited.contains(i+1))
{
visited.add(i+1);
will_visit.add(i+1);
}
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int V = Integer.parseInt(st.nextToken());
int[][] graph = createGraph(N, M);
DFS(graph, V);
System.out.println();
BFS(graph, V);
}
}
런타임 에러가 난다 이유가 뭘까....??
import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
// 함수에서 사용할 변수들
static int[][] graph; // 간선 연결상태
static int N; // 정점개수
static int M; // 간선개수
static int start; // 시작정점
public static void DFS(HashSet<Integer> visited, int current_vertex) {
visited.add(current_vertex);
System.out.print(current_vertex + " ");
for (int i = 0; i < graph.length; i++) {
if (graph[current_vertex - 1][i] == 1 && !visited.contains(i + 1)) {
visited.add(i + 1);
DFS(visited,i+1);
}
}
}
public static void BFS() {
HashSet<Integer> visited = new HashSet<>();
Queue<Integer> will_visit = new LinkedList<>();
visited.add(start);
will_visit.add(start);
while (will_visit.isEmpty() == false) {
Integer current_vertex = will_visit.remove();
System.out.print(current_vertex + " ");
for (int i = 0; i < graph.length; i++) {
if (graph[current_vertex - 1][i] == 1 && !visited.contains(i + 1)) {
will_visit.add(i + 1);
visited.add(i + 1);
}
}
}
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
start = sc.nextInt();
graph = new int[1001][1001];
int vertex1, vertex2;
for (int i = 0; i < M; ++i) {
vertex1 = sc.nextInt();
vertex2 = sc.nextInt();
graph[vertex1 - 1][vertex2 - 1] = graph[vertex2 - 1][vertex1 - 1] = 1;
}
DFS(new HashSet(), start);
System.out.println();
BFS();
}
}
런타임에러 고칠려다가
10 10 4
5 4
6 4
6 8
8 9
1 10
2 10
10 3
8 2
1 7
4 10
이 반례를 찾았다.
DFS 부분에서 인접행렬로 그래프를 만드니까 재귀로해야 DFS가 작동을 했다.
그래도 런타인 에러가 났다.
찾아보니 Scanner를 두번 사용해서 문제 였다.
Scanner는 close() 하지 않아도, 프로그램 종료시 자동으로 close()를 하긴하지만 close()를 명시해주는 것이 좋다.