*칼을 가지고 지나가는 경우와 아닌 경우를 나눠서 판단
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
84
85
86
87
88
89
90
|
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int n, m, t, ans;
int map[101][101];
bool visited[101][101][2];
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
typedef pair<int, int> pp;
typedef pair<pp, pp> pppp;
void bfs(int x, int y)
{
queue<pppp> q;
q.push(pppp(pp(0, 0), pp(x, y)));
visited[x][y][0] = true;
while (!q.empty())
{
int size = q.size();
while (size--)
{
q.pop();
if (x == n - 1 && y == m - 1)
{
ans = min(ans, cnt);
continue;
}
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx >= n || ny < 0 || ny >= m || visited[nx][ny][knife])
continue;
if (map[nx][ny] == 1)
{
if (knife)
{
//칼을 가지고 있다면
visited[nx][ny][knife] = true;
q.push(pppp(pp(cnt+1, knife), pp(nx, ny)));
}
else {
continue;
}
}
else if (map[nx][ny] == 2) {
//칼 발견
visited[nx][ny][1] = true;
q.push(pppp(pp(cnt+1, 1), pp(nx, ny)));
}
else {
visited[nx][ny][knife] = true;
q.push(pppp(pp(cnt+1, knife), pp(nx, ny)));
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> t;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
cin >> map[i][j];
}
}
ans = t+1;
bfs(0, 0);
if (ans == t+1) cout << "Fail" << '\n';
else cout << ans << '\n';
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'BOJ > C++' 카테고리의 다른 글
통나무 옮기기 (0) | 2020.02.29 |
---|---|
로봇청소기 + 테스트케이스 (0) | 2020.02.29 |
알고스팟 (0) | 2020.02.28 |
달이 차오른다, 가자 (0) | 2020.02.28 |
Two Dots (0) | 2020.02.28 |