본문 바로가기

ALGORITHM

백준 13023 ABCDE (Java)

1. 문제 링크

 

13023번: ABCDE

문제의 조건에 맞는 A, B, C, D, E가 존재하면 1을 없으면 0을 출력한다.

www.acmicpc.net

 

2. 문제 및 입출력예제

 

3. 문제 풀이

dfs를 이용해서 풀었다.

클래스를 구현해서 그 클래스로 배열을 만드는 방법 (코드 1)과

ArrayList<> 배열을 만드는 방법 2가지를 해봤다.

1번 방법이 확실히 코드도 간단하고 메모리나 시간 측면에서 더 나은 성능을 보여줬다.

 

4. 코드

방법 1

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	static class Node{
		public int vertex;
		public Node link;
		
		public Node(int vertex, Node link) {
			super();
			this.vertex = vertex;
			this.link = link;
		}
	}
	static Node[]  adjList;
	static int N, M, cnt, size[];
	static boolean visit[];
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String[] split = br.readLine().split(" ");
		N = Integer.parseInt(split[0]);
		M = Integer.parseInt(split[1]); //연결된 수가 이만큼
		
		adjList = new Node[N];
		
		
		for(int i=0;i<M;i++) {
			split = br.readLine().split(" ");
			int num = Integer.parseInt(split[0]);
			int ber = Integer.parseInt(split[1]);
			
			adjList[num] = new Node(ber, adjList[num]);
			adjList[ber] = new Node(num, adjList[ber]);
		}
		
		visit = new boolean[N];
		cnt = 0;
		for(int i=0;i<N;i++) {
			if(cnt == 0) {
				dfs(i, 1);
			}
		}
		
		// 4. 정답 출력	
		System.out.println(cnt);
	}
	
	private static void dfs(int current, int conn) {
		visit[current] = true;
		if(conn == 5) {
			cnt = 1;
			return;
		}
		
		visit[current] = true;
		
		for(Node temp = adjList[current]; temp != null; temp = temp.link) {
			if(!visit[temp.vertex]) {
				dfs(temp.vertex, conn+1);
			}
		}
		visit[current] = false;
	}
}

 

방법 2

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main {
	
	static ArrayList<Integer>[] adjList;
	static int N, M, cnt, size[];
	static boolean visit[];
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] split = br.readLine().split(" ");
		N = Integer.parseInt(split[0]);
		M = Integer.parseInt(split[1]);
		
		adjList = new ArrayList[N];
		for(int i=0;i<N;i++) {
			adjList[i] = new ArrayList<>();
		}
		
		for(int i=0;i<M;i++) {
			split = br.readLine().split(" ");
			int num = Integer.parseInt(split[0]);
			int ber = Integer.parseInt(split[1]);
			
			adjList[num].add(ber);
			adjList[ber].add(num);
		}
		
		visit = new boolean[N];
		cnt = 0;
		for(int i=0;i<N;i++) {
			if(cnt == 0) {
				dfs(i, 1);
			}
		}
		System.out.println(cnt);
	}
	
	private static void dfs(int current, int conn) {
		if(conn == 5) {
			cnt = 1;
			return;
		}
		
		visit[current] = true;
		
		for(int i: adjList[current]) {
			if(!visit[i]) {
				dfs(i, conn+1);
			}
		}
		visit[current] = false;
	}
}

 

'ALGORITHM' 카테고리의 다른 글

백준 4153 직각 삼각형(Java)  (0) 2023.03.20
백준 17281 ⚾ 야구 (Java)  (0) 2023.03.19
백준 11050 이항 계수 1 (Java)  (0) 2023.03.18
백준 1541 잃어버린 괄호 (Java)  (0) 2023.03.18
백준 1987 알파벳 (Java)  (0) 2023.03.17