코딩테스트
[백준] 16173번 점프왕 쩰리 (Small)
스키(ski)
2022. 8. 31. 23:55
문제 내용
https://www.acmicpc.net/problem/16173
16173번: 점프왕 쩰리 (Small)
쩰리는 맨 왼쪽 위의 칸에서 출발해 (행, 열)로 나타낸 좌표계로, (1, 1) -> (2, 1) -> (3, 1) -> (3, 3)으로 이동해 게임에서 승리할 수 있다.
www.acmicpc.net
해결 코드
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
|
#include <iostream>
#include <queue>
#include <tuple> //tie 사용을 위한 header
#define MAX_N 3
using namespace std;
int n;
int arr[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N];
bool check(int x, int y) {
if (x < 0 || x >= n || y < 0 || y >= n || visited[x][y])
return false;
return true;
}
bool BFS() {
queue<pair<int, int>> q;
q.push(make_pair(0, 0));
visited[0][0] = true;
while (!q.empty()) {
int x, y;
tie(x, y) = q.front();
q.pop();
int num = arr[x][y];
if (num == -1)
return true;
for (int i = 0; i <= 1; i++) { //오른쪽 or 아래중 선택
int tx = x + i * num;
int ty = y + (1 - i) * num;
if (check(tx, ty)) {
q.push(make_pair(tx, ty));
visited[tx][ty] = true;
}
}
}
return false;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> arr[i][j];
}
}
if (BFS())
cout << "HaruHaru";
else
cout << "Hing";
}
|
cs |
주요 개념
- 그래프 탐색: 너비 우선 탐색(Breadth-First Search,BFS)
- 브루트포스 알고리즘(Brute force)
주의 사항
- 오른쪽과 아래를 섞어서 가는것이 아닌 오른쪽 or 아래중 하나를 선택하는것에 유의 ex)현재칸:2 -> 오른쪽 2칸 or 아래 2칸 중 선택