-
15558 점프 게임알고리즘/백준 2019. 3. 18. 00:13
1. 문제
2. 코드
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869#include "pch.h"#include <queue>#include <iostream>#include <vector>#define _CRT_SECURE_NO_WARNINGSusing 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;elsecout << 1;}cs 3. 후기
한 위치에서 갈 수있는 방법은 3가지이다. 그래서 세 가지 경로를 검사하고 이동할 수 있으면 큐에 넣고, 이동한 위치가 목적지라면 flag를 true로 바꿔서 목적지에 도달할 수 있음을 표시했다.
'알고리즘 > 백준' 카테고리의 다른 글
10942 팰린드롬? (0) 2019.03.19 1890 점프 (0) 2019.03.19 8111 0과 1 (0) 2019.03.17 4991 로봇 청소기 (0) 2019.03.15 9328 열쇠 (0) 2019.03.15