-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1445.cpp
90 lines (83 loc) · 2.02 KB
/
1445.cpp
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
//
// Created by kdongha on 2020/02/06.
//
#include <bits/stdc++.h>
int N, M, sx, sy, fx, fy;
char map[51][51];
struct Node {
int trash;
int near;
};
Node dp[51][51];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool operator<(const Node &n1, const Node &n2) {
if (n1.trash == n2.trash) {
return n1.near < n2.near;
}
return n1.trash < n2.trash;
}
bool inside(int x, int y) {
return x >= 0 && x < N && y >= 0 && y < M;
}
bool near(int x, int y) {
for (int d = 0; d < 4; d++) {
int nx = x + dx[d];
int ny = y + dy[d];
if (inside(nx, ny) && map[nx][ny] == 'g') {
return true;
}
}
return false;
}
void solve() {
std::cin >> N >> M;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
dp[i][j] = {3000, 3000};
std::cin >> map[i][j];
if (map[i][j] == 'S') {
sx = i;
sy = j;
} else if (map[i][j] == 'F') {
fx = i;
fy = j;
}
}
}
dp[sx][sy] = {0, 0};
std::queue<std::pair<int, int>> q;
q.push({sx, sy});
while (!q.empty()) {
auto current = q.front();
q.pop();
for (int d = 0; d < 4; d++) {
int nx = current.first + dx[d];
int ny = current.second + dy[d];
if (inside(nx, ny)) {
Node node = dp[current.first][current.second];
if (map[nx][ny] == 'g') {
node.trash++;
}
else if (near(nx, ny)) {
node.near++;
}
if (node < dp[nx][ny]) {
dp[nx][ny] = node;
q.push({nx, ny});
}
}
}
}
if(near(fx,fy)){
dp[fx][fy].near--;
}
std::cout << dp[fx][fy].trash << " " << dp[fx][fy].near;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
solve();
return 0;
}