/**
* author : pham van sam
* created : 16 June 2023 (Friday)
**/
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(it, s) for (__typeof(s.begin()) it = (s).begin(); it != (s).end(); ++it)
template <typename U, typename V> bool maximize(U &A, const V &B) { return (A < B) ? (A = B, true) : false; }
template <typename U, typename V> bool minimize(U &A, const V &B) { return (A > B) ? (A = B, true) : false; }
const int MAXN = 4e3 + 5;
const int dir[] = {1, -1, 0, 0, 0, 0, 1, -1};
int N, M, dp[MAXN][MAXN];
char a[MAXN][MAXN];
void process(void) {
cin >> N >> M;
FOR(i, 1, N) FOR(j, 1, M) cin >> a[i][j];
deque <pair <int, int>> q;
memset(dp, 0x3f, sizeof dp);
q.emplace_front(1, 1); dp[1][1] = 1;
while(!q.empty()) {
auto [u, v] = q.front(); q.pop_front();
REP(i, 4) {
int x = u + dir[i];
int y = v + dir[i + 4];
if(x < 1 || x > N || y < 1 || y > M) continue;
if(a[x][y] == '.') continue;
int cost = (a[x][y] != a[u][v] ? 1 : 0);
if(minimize(dp[x][y], dp[u][v] + cost)) {
if(!cost) q.emplace_front(x, y);
else q.emplace_back(x, y);
}
}
}
int ans = 0;
FOR(i, 1, N) FOR(j, 1, M) if(a[i][j] != '.') {
maximize(ans, dp[i][j]);
}
cout << ans;
}
signed main() {
#define TASK "tracks"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
auto start_time = steady_clock::now();
int test = 1;
// cin >> test;
for (int i = 1; i <= test; ++i) {
process();
// cout << '\n';
}
auto end_time = steady_clock::now();
cerr << "\nExecution time : " << duration_cast<milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:59:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
65312 KB |
Output is correct |
2 |
Correct |
21 ms |
63056 KB |
Output is correct |
3 |
Correct |
24 ms |
63276 KB |
Output is correct |
4 |
Correct |
31 ms |
65304 KB |
Output is correct |
5 |
Correct |
24 ms |
64308 KB |
Output is correct |
6 |
Correct |
21 ms |
63156 KB |
Output is correct |
7 |
Correct |
21 ms |
63316 KB |
Output is correct |
8 |
Correct |
23 ms |
63324 KB |
Output is correct |
9 |
Correct |
21 ms |
63384 KB |
Output is correct |
10 |
Correct |
22 ms |
64084 KB |
Output is correct |
11 |
Correct |
22 ms |
63936 KB |
Output is correct |
12 |
Correct |
26 ms |
64280 KB |
Output is correct |
13 |
Correct |
24 ms |
64200 KB |
Output is correct |
14 |
Correct |
24 ms |
64312 KB |
Output is correct |
15 |
Correct |
32 ms |
65236 KB |
Output is correct |
16 |
Correct |
37 ms |
65332 KB |
Output is correct |
17 |
Correct |
29 ms |
65220 KB |
Output is correct |
18 |
Correct |
28 ms |
65336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
78064 KB |
Output is correct |
2 |
Correct |
62 ms |
69536 KB |
Output is correct |
3 |
Correct |
263 ms |
94404 KB |
Output is correct |
4 |
Correct |
94 ms |
74148 KB |
Output is correct |
5 |
Correct |
156 ms |
83640 KB |
Output is correct |
6 |
Correct |
573 ms |
108048 KB |
Output is correct |
7 |
Correct |
26 ms |
78768 KB |
Output is correct |
8 |
Correct |
26 ms |
78000 KB |
Output is correct |
9 |
Correct |
22 ms |
63128 KB |
Output is correct |
10 |
Correct |
24 ms |
63108 KB |
Output is correct |
11 |
Correct |
32 ms |
78412 KB |
Output is correct |
12 |
Correct |
21 ms |
63696 KB |
Output is correct |
13 |
Correct |
60 ms |
69536 KB |
Output is correct |
14 |
Correct |
44 ms |
67564 KB |
Output is correct |
15 |
Correct |
46 ms |
68036 KB |
Output is correct |
16 |
Correct |
41 ms |
65228 KB |
Output is correct |
17 |
Correct |
120 ms |
74984 KB |
Output is correct |
18 |
Correct |
90 ms |
74752 KB |
Output is correct |
19 |
Correct |
90 ms |
74148 KB |
Output is correct |
20 |
Correct |
81 ms |
73328 KB |
Output is correct |
21 |
Correct |
176 ms |
84324 KB |
Output is correct |
22 |
Correct |
157 ms |
83644 KB |
Output is correct |
23 |
Correct |
205 ms |
80460 KB |
Output is correct |
24 |
Correct |
166 ms |
84200 KB |
Output is correct |
25 |
Correct |
390 ms |
94372 KB |
Output is correct |
26 |
Correct |
321 ms |
138908 KB |
Output is correct |
27 |
Correct |
407 ms |
120956 KB |
Output is correct |
28 |
Correct |
569 ms |
108008 KB |
Output is correct |
29 |
Correct |
551 ms |
106388 KB |
Output is correct |
30 |
Correct |
510 ms |
111528 KB |
Output is correct |
31 |
Correct |
495 ms |
86204 KB |
Output is correct |
32 |
Correct |
393 ms |
109708 KB |
Output is correct |