알고리즘 모음(C++)

백준 16173 - 점프왕 쩰리(Small) (C++) 본문

백준

백준 16173 - 점프왕 쩰리(Small) (C++)

공대생의 잡다한 사전 2023. 1. 11. 14:25

문제 링크입니다. https://www.acmicpc.net/problem/16173

 

16173번: 점프왕 쩰리 (Small)

쩰리는 맨 왼쪽 위의 칸에서 출발해 (행, 열)로 나타낸 좌표계로,  (1, 1) -> (2, 1) -> (3, 1) -> (3, 3)으로 이동해 게임에서 승리할 수 있다.

www.acmicpc.net

간단한 구현 문제입니다.

쩰리가 아래와 오른쪽만 갈 수 있다고 했을 때, -1에 도달할 수 있는지를 구하는 문제입니다.

 

한칸씩만 움직이는 것이 아닌, 주어진 map의 값만큼 움직입니다.

따라서 오른쪽으로 혹은 아래쪽으로 map만큼 움직였을 때, map의 범위를 벗어나는지를 확인해주는 것은 필수입니다.

 

 

자세한 것은 코드를 참고해주세요

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <deque>
#include <stack>
#define P pair<int,int>
#define F first
#define S second
#define LL long long

using namespace std;

int N;
int map[4][4];
int check[4][4];
P Move[2] = {{1, 0}, {0, 1}};

bool bfs(int X, int Y){
    queue<P> q;
    check[X][Y] = 1;
    q.push({X, Y});
    while(!q.empty()){
        int x = q.front().F;
        int y = q.front().S;
        q.pop();
        if(map[x][y] == -1) return true;
        for(int i = 0; i < 2; i++){
            int xx = x + Move[i].F * map[x][y];
            int yy = y + Move[i].S * map[x][y];
            if(xx >= 1 && xx <= N && yy >= 1 && yy <= N){
                if(check[xx][yy] == 1) continue;
                check[xx][yy] = 1;
                q.push({xx, yy});
            }
        }
    }
    return false;
}

void solve(){
    if(bfs(1, 1)) cout << "HaruHaru";
    else cout << "Hing";
}

int main() {
	cin.tie(0);
	cout.tie(0);
	cin >> N;
	for(int i = 1; i <= N; i++){
	    for(int j = 1; j <= N; j++){
	        cin >> map[i][j];
	    }
	}
	solve();
	return 0;
}

 

 

질문 및 조언은 댓글을 남겨주세요

'백준' 카테고리의 다른 글

백준 10942 - 팰린드롬?(C++)  (2) 2023.01.11
백준 16174 - 점프왕 쩰리(Large) (C++)  (0) 2023.01.11
백준 9466 - 텀 프로젝트(C++)  (0) 2023.01.09
백준 12852 - 1로 만들기 2(C++)  (0) 2023.01.08
백준 9328 - 열쇠(C++, 복습)  (0) 2023.01.08