뚱땅뚱땅

[문제] SWEA 1219번 길찾기 본문

알고리즘/swea

[문제] SWEA 1219번 길찾기

양순이 2021. 3. 5. 17:30
728x90

swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14geLqABQCFAYD&categoryId=AV14geLqABQCFAYD&categoryType=CODE&problemTitle=1219&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1

 

SW Expert Academy

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

swexpertacademy.com

내 풀이

 

dfs

public class SWEA_1219 {
	static int N, ans;
	static ArrayList<ArrayList<Integer>> list;
	static int[] visited;	//  방문한건 1
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		int T = 1;
	
		for(int tc=1;tc<=T;tc++) {
			StringTokenizer st = new StringTokenizer(in.readLine()," ");
			st.nextToken();
			N = Integer.parseInt(st.nextToken());
			list = new ArrayList<>();
			for(int i=0;i<100;i++) {
				list.add(new ArrayList<>());
			}
		
			st = new StringTokenizer(in.readLine()," ");
			for(int i=0;i<N;i++) {
				int a = Integer.parseInt(st.nextToken());
				int b = Integer.parseInt(st.nextToken());
				list.get(a).add(b);
			}
			ans = 0;
			visited = new int[100];
			dfs(0);
			sb.append("#").append(tc).append(" ").append(ans).append('\n');
		}
		System.out.println(sb);
	}
	static void dfs(int x) {
		if(x == 99) {
			ans=1;
			return;
		}
		int size = list.get(x).size();
		for(int i=0;i<size;i++) {
			int nx = list.get(x).get(i);
			if(visited[nx]==0) {
				visited[nx] = 1;
				dfs(nx);
			}
		}
	}
}
728x90
Comments