Notice
Recent Posts
Recent Comments
Link
알고리즘 모음(C++)
백준 16174 - 점프왕 쩰리(Large) (C++) 본문
문제 링크입니다. https://www.acmicpc.net/problem/16174
16174번: 점프왕 쩰리 (Large)
쩰리는 맨 왼쪽 위의 칸에서 출발해 (행, 열)로 나타낸 좌표계로, (1, 1) -> (2, 1) -> (3, 1) -> (3, 3)으로 이동해 게임에서 승리할 수 있다.
www.acmicpc.net
bfs를 이용한 간단한 문제입니다.
쩰리가 -1에 도착할 수 있는지 구하는 문제입니다.
쩰리는 아래와 오른쪽으로만 이동할 수 있습니다.
이동할 때 한칸만 이동하는 것이 아닌, 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[65][65];
int check[65][65];
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;
}
질문 및 조언은 댓글을 남겨주세요
'백준' 카테고리의 다른 글
백준 27110 - 특식 배부(C++) (0) | 2023.01.15 |
---|---|
백준 10942 - 팰린드롬?(C++) (2) | 2023.01.11 |
백준 16173 - 점프왕 쩰리(Small) (C++) (0) | 2023.01.11 |
백준 9466 - 텀 프로젝트(C++) (0) | 2023.01.09 |
백준 12852 - 1로 만들기 2(C++) (0) | 2023.01.08 |