티스토리 뷰

알고리즘/BOJ

[백준] 15558 점프 게임

세진짱 2020. 1. 28. 18:43

 

숨바꼭질? 그 문제랑 비슷한 문제다

내가 갈 수 있는 경우를 다 큐에 넣고 bfs를 돌리면 된다

 

이 때 시간마다 칸이 사라지므로 그 경우를 큐의 사이즈를 통해 해결하자!

 

평소처럼 큐가 비어있으면 돌리는게 아닌 현재 시간에 갈 수 있는 만큼만 큐를 돌리는거다

현재 시간에 움직일 수 있는 칸  == 현재 큐 사이즈 이용 ㄱㄱ

 

문제를 잘 읽어보면 N번 칸 보다 더 큰 칸으로 간다면 1이므로 조건을 잘 확인하자!

 

소스코드

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
58
59
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int n, k,ans;
int map[100010][2];
bool check[100010][2];
int dx[2= { 1,-1 };
int dy[2= { 0,0 };
 
bool inner(int x, int y) {
    return (0 <= x && x < n && 0 <= y && y < 2);
}
int main() {
    scanf(" %d %d"&n, &k);
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < n; j++) {
            scanf(" %1d"&map[j][i]);
        }
    
    }
 
    queue<pair<intint>> q;
    q.push({ 0,0 });
    check[0][0= true;
    int _time = 0;
    while (int s = q.size()) {
        while (s--) {
            int x = q.front().first;
            int y = q.front().second;
            q.pop();
 
            if (x < _time) continue;
 
            for (int i = 0; i < 2; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (inner(nx, ny) && map[nx][ny] == 1 && !check[nx][ny]) {
                    check[nx][ny] = true;
                    q.push({ nx,ny });
                }
            }
 
            int nx = x + k;
            int ny = (y + 1) % 2;
            if (inner(nx, ny) && map[nx][ny] == 1 && !check[nx][ny]) {
                check[nx][ny] = true;
                q.push({ nx,ny });
            }
            else if (nx >= n) ans = 1;
        }
        _time++;
    }
 
    ans = ans || check[n - 1][0|| check[n - 1][1];
    printf("%d\n", ans);
 
    
}
 
 

'알고리즘 > BOJ' 카테고리의 다른 글

[백준] 15661 링크와 스타트  (0) 2020.01.29
[백준] 15662 톱니바퀴 (2)  (0) 2020.01.28
[백준] 15989 1,2,3 더하기 4  (2) 2020.01.28
[백준] 6087 레이저 통신  (0) 2020.01.28
[백준] 12100 2048 (Easy)  (0) 2020.01.28
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
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
글 보관함