[백준] 15558 점프게임

` bfs

3가지 경우를 확인하고 큐에 넣기


 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class B15558 {
	static int N,K;
	static char arr[][];	
	static boolean visit[][];
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        K = Integer.parseInt(st.nextToken());
        
        arr = new char[2][N];
        visit = new boolean[2][N];
        arr[0] = br.readLine().toCharArray();
        arr[1] = br.readLine().toCharArray();
        	
		// 앞 뒤 점프
        int dc[] = {-1,1,K};
        boolean flag = false; // 갈 수 있는지 여부
		Queue<int[]> q = new LinkedList<int[]>();
		q.add(new int[] {0,0,0}); // 좌표와 시간 저장
		visit[0][0]=true;
		e:while(!q.isEmpty()) {
			int cur[]= q.poll();
			for (int i = 0; i < 3; i++) {
				int nc = cur[1]+dc[i];
				int nr = cur[0];
				int time = cur[2];
				// 점프의 경우
				if(i==2) {
					if(cur[0]==1)
							nr = 0;
						else
							nr = 1;
				}
				if(nc>=N) { // 사이즈를 넘어가 게임 클리어
					flag = true;
					break e;
				}
				// 방문 여부, 갈 수 있는지 체크
				if(nc <= time) continue;
				if(visit[nr][nc]) continue;
				if(arr[nr][nc]=='0') continue;
				visit[nr][nc]=true;
				q.add(new int[] {nr,nc,time+1});
			}
		}
		System.out.println(flag?1:0);
	}
}

updatedupdated2021-04-112021-04-11
Load Comments?