알고리즘/백준

15558 점프 게임

아메숑 2019. 3. 18. 00:13

1. 문제



2. 코드

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
60
61
62
63
64
65
66
67
68
69
#include "pch.h"
#include <queue>
#include <iostream>
#include <vector>
#define _CRT_SECURE_NO_WARNINGS
 
using namespace std;
 
char map[2][100001];
bool visit[2][100001];
 
struct node
{
    int x, y, cnt;
};
 
int main()
{
    int N, K;
    cin >> N >> K;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < N; j++) {
            cin >> map[i][j];
        }
    }
 
    bool flag = false;
    queue<node> q;
    //처음 시작위치, 카운트 
    q.push({ 0,0,0 });
    visit[0][0= 1;
 
    while (!q.empty()) {
        node cur = q.front();
        q.pop();
        int x = cur.x, y = cur.y, cnt = cur.cnt;
 
        //전진
        if (y + 1 == N)
            flag = true;
        else if (map[x][y + 1!= '0' && visit[x][y + 1== 0)
        {
            visit[x][y + 1= 1;
            q.push({ x,y + 1,cnt + 1 });
        }
 
        //후진 
        if (map[x][y - 1!= '0' && visit[x][y - 1== 0 && y - 1 > cnt) {
            visit[x][y - 1= 1;
            q.push({ x,y - 1,cnt + 1 });
        }
        
        //점프
        if (y + K >= N)
            flag = true;
        else if (map[1 - x][y + K] != '0' && visit[1 - x][y + K] == 0) {
            visit[1 - x][y + K] = 1;
            q.push({ 1 - x,y + K,cnt + 1 });
        }
 
    }
 

    if (flag == false)
        cout << 0;
    else
        cout << 1;
 
}
cs


3. 후기

한 위치에서 갈 수있는 방법은 3가지이다. 그래서 세 가지 경로를 검사하고 이동할 수 있으면 큐에 넣고, 이동한 위치가 목적지라면 flag를 true로 바꿔서 목적지에 도달할 수 있음을 표시했다.