1
+ #include < bits/stdc++.h>
2
+
3
+ inline int64_t binary_search (
4
+ std::function<bool (int64_t )> const & predicate,
5
+ int64_t ng,
6
+ int64_t ok
7
+ ) {
8
+ assert (!predicate (ng) && predicate (ok));
9
+ while (abs (ok - ng) > 1 ) {
10
+ auto m = (ok + ng) / 2 ;
11
+ if (predicate (m)) ok = m;
12
+ else ng = m;
13
+ }
14
+ return ok;
15
+ }
16
+
17
+ using namespace std ;
18
+ using ll = int64_t ;
19
+ using ff = long double ;
20
+
21
+ int H, W, T;
22
+ vector<string> M;
23
+ int sX , sY , gX , gY ;
24
+
25
+ vector<int > dx = { 1 , -1 , 0 , 0 };
26
+ vector<int > dy = { 0 , 0 , 1 , -1 };
27
+ bool pass (ll XX) {
28
+ priority_queue<array<ll,3 >> Q;
29
+ vector<vector<bool >> done (H, vector<bool >(W, false ));
30
+ Q.push ({ 0 , sX , sY });
31
+ while (Q.size ()) {
32
+ auto entry = Q.top (); Q.pop ();
33
+ ll t = -entry[0 ];
34
+ int x = entry[1 ], y = entry[2 ];
35
+ if (done[y][x]) continue ;
36
+ done[y][x] = true ;
37
+ for (int i = 0 ; i < 4 ; ++i) {
38
+ int nx = x + dx[i], ny = y + dy[i];
39
+ if (nx < 0 || nx >= W) continue ;
40
+ if (ny < 0 || ny >= H) continue ;
41
+ if (done[ny][nx]) continue ;
42
+ if (nx == gX && ny == gY ) {
43
+ return t + 1 <= T;
44
+ }
45
+ ll nt = t + (M[ny][nx] == ' .' ? 1 : XX);
46
+ Q.push ({ -nt, nx, ny });
47
+ }
48
+ }
49
+ return false ;
50
+ }
51
+
52
+ ll solve () {
53
+ for (int y = 0 ; y < H; y++) {
54
+ for (int x = 0 ; x < W; x++) {
55
+ if (M[y][x] == ' S' ) {
56
+ sX = x, sY = y;
57
+ }
58
+ if (M[y][x] == ' G' ) {
59
+ gX = x, gY = y;
60
+ }
61
+ }
62
+ }
63
+ ll ng = T + 1 , ok = 1 ;
64
+ return binary_search (pass, ng, ok);
65
+ }
66
+
67
+ int main () {
68
+ ios_base::sync_with_stdio (false );
69
+ cin.tie (0 ); cout.tie (0 );
70
+
71
+ cin >> H >> W >> T;
72
+ M.assign (H, " " );
73
+ for (int y = 0 ; y < H; ++y) {
74
+ cin >> M[y];
75
+ }
76
+ cout << solve () << endl;
77
+
78
+ return 0 ;
79
+ }
0 commit comments