Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

[SW Expert Academy] 1219. [S/W 문제해결 기본] 4일차 - 길찾기 본문

SW_Expert

[SW Expert Academy] 1219. [S/W 문제해결 기본] 4일차 - 길찾기

코드와이 2021. 2. 26. 10:28

 

문제링크

swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=4&contestProbId=AV14geLqABQCFAYD&categoryId=AV14geLqABQCFAYD&categoryType=CODE&problemTitle=&orderBy=PASS_RATE&selectCodeLang=JAVA&select-1=4&pageSize=10&pageIndex=1

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

package D4;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class 길찾기 {
	
	static int[][] arr;
	static int n, ans;
	static boolean[] visited;
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		StringBuilder sb = new StringBuilder();
		
		for(int tc = 1; tc <= 1 ; tc++) {
			sb.append("#").append(tc).append(" ");
			ans = 0;
			visited = new boolean[100];
			arr = new int[100][100];
			st = new StringTokenizer(br.readLine());
			
			st.nextToken();
			n = Integer.parseInt(st.nextToken());
			
			st = new StringTokenizer(br.readLine());
			for(int i = 0 ; i < n ; i++) {
				arr[Integer.parseInt(st.nextToken())][Integer.parseInt(st.nextToken())] = 1;
			}
			
			func(0);
			sb.append(ans == 1?ans : 0).append("\n");
		}
		sb.setLength(sb.length() - 1);
		System.out.println(sb);
	}
	
	public static void func(int x) {

		if( x == 99) {
			ans = 1;
			return;
		}
		
		for(int i = 0 ; i < 100 ; i++) {
			if(arr[x][i] == 1 && !visited[i]) {
				visited[i] = true;
				func(i);
				visited[i] = false;
			}
		}
		return;
	}
}