일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- silver
- Recursive
- 영화
- usaco
- 추천
- review
- 코딩 테스트
- coding
- Algorithm
- array
- 영어
- 알고리즘
- Movie
- Greedy
- 해설
- 리뷰
- health
- Netflix
- BFS
- 2020
- 넷플릭스
- 완전탐색
- parametric search
- 백준
- benefits
- 수능
- BOJ
- kakao
- 나는솔로
- 카카오
Archives
- Today
- Total
Young
BOJ - 공주님을 구해라! (17836) 본문
728x90
반응형
https://www.acmicpc.net/problem/17836
딱 두가지 경우만 생각해보면 된다.
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
struct ss {
int y, x;
};
int n, m;
bool range(int y, int x) {
return !(y < 0 || y >= n || x < 0 || x >= m);
}
int mp[111][111];
int t;
// 전형적인 bfs 코드이므로 설명은 생략
int bfs(int y, int x) { // 도착지 (y, x)까지 bfs를 통해 최단거리를 구하는 함수
bool chk[111][111] = {};
queue<ss> q;
q.push({ 0,0 });
chk[0][0] = true;
int step = 0;
bool ff = false;
while (!q.empty()) {
int l = q.size();
while (l--) {
ss f = q.front();
q.pop();
if (f.y == y && f.x == x) {
ff = true;
break;
}
for (int dir = 0; dir < 4; dir++) {
int ny = f.y + dy[dir], nx = f.x + dx[dir];
if (!range(ny, nx) || chk[ny][nx] || mp[ny][nx] == 1) continue;
chk[ny][nx] = true;
q.push({ ny, nx });
}
}
if (ff) break;
step += 1;
}
if(ff) return step;
else return 123456789;
}
int main() {
cin >> n >> m >> t;
ss sword;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
cin >> mp[i][j];
if (mp[i][j] == 2) {
sword = { i, j };
mp[i][j] = 0;
}
}
int a = bfs(n - 1 , m - 1); // 전설의 명검 없이 바로 공주가 있는 곳으로 가는 경우
int b = bfs(sword.y, sword.x) + n - 1 - sword.y + m - 1 - sword.x; // 전설의 명검이 있는 곳으로 간 후, 공주가 있는 곳으로 가는
if (a > t && b > t) {
cout << "Fail";
}
else {
cout << min(a, b);
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
728x90
반응형
'코딩 테스트 대비 추천 문제' 카테고리의 다른 글
BOJ - N으로 만들기 (17255) (0) | 2019.12.09 |
---|---|
BOJ - 주사위 윷놀이 (17825) [2019 삼성 SW 기출] (0) | 2019.12.07 |
BOJ - 카드 놓기 (5568) (0) | 2019.12.03 |
BOJ - 마라톤1 (10655) (0) | 2019.12.03 |
BOJ - 달팽이 리스트 (17827) (0) | 2019.12.03 |