#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define f(i, l, r) for (int i = l; i <= r; i++)
#define debug(x) cout << #x << '=' << (x) << endl
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define ff first
#define ss second
using PLL = pair<ll, ll>;
using VL = vector<ll>;
using VLL = vector<pair<ll, ll>>;
// -------------------------------------------------------------------------------
constexpr int mod = 1e9 + 7;
void setIO(const string &name = "") {
ios_base::sync_with_stdio(0);
cin.tie(0);
if (sz(name)) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
// -------------------------------------------------------------------------------
// ------------------------Template End ------------------------------------------
// -------------------------------------------------------------------------------
string grid[4004];
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
int n, m;
bool valid(int x, int y) {
return x >= 0 and x < n and y >= 0 and y < m and grid[x][y] != '.';
}
int dist[4004][4004];
int main() {
setIO();
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> grid[i];
}
deque<pair<int, int>> q;
q.push_back({0, 0});
int ans = 1;
dist[0][0] = 1;
while (q.size()) {
auto curr = q.front();
q.pop_front();
ans = max(ans, dist[curr.ff][curr.ss]);
for (int i = 0; i < 4; i++) {
int x = curr.ff + dx[i], y = curr.ss + dy[i];
if (valid(x, y) and dist[x][y] == 0) {
if (grid[curr.ff][curr.ss] == grid[x][y]) {
dist[x][y] = dist[curr.ff][curr.ss];
q.push_front({x, y});
} else {
dist[x][y] = dist[curr.ff][curr.ss] + 1;
q.push_back({x, y});
}
}
}
}
cout << ans;
return 0;
}
Compilation message
tracks.cpp: In function 'void setIO(const string&)':
tracks.cpp:25:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:26:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
3788 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
7 ms |
3532 KB |
Output is correct |
5 |
Correct |
3 ms |
1996 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
572 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
844 KB |
Output is correct |
10 |
Correct |
3 ms |
1612 KB |
Output is correct |
11 |
Correct |
2 ms |
1484 KB |
Output is correct |
12 |
Correct |
5 ms |
2124 KB |
Output is correct |
13 |
Correct |
3 ms |
1996 KB |
Output is correct |
14 |
Correct |
3 ms |
1996 KB |
Output is correct |
15 |
Correct |
10 ms |
3660 KB |
Output is correct |
16 |
Correct |
12 ms |
3880 KB |
Output is correct |
17 |
Correct |
8 ms |
3660 KB |
Output is correct |
18 |
Correct |
7 ms |
3532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
15968 KB |
Output is correct |
2 |
Correct |
37 ms |
11852 KB |
Output is correct |
3 |
Correct |
201 ms |
75456 KB |
Output is correct |
4 |
Correct |
66 ms |
29344 KB |
Output is correct |
5 |
Correct |
167 ms |
51436 KB |
Output is correct |
6 |
Correct |
640 ms |
109948 KB |
Output is correct |
7 |
Correct |
12 ms |
16588 KB |
Output is correct |
8 |
Correct |
11 ms |
15980 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
11 ms |
16332 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
39 ms |
11808 KB |
Output is correct |
14 |
Correct |
22 ms |
7920 KB |
Output is correct |
15 |
Correct |
20 ms |
10268 KB |
Output is correct |
16 |
Correct |
21 ms |
4912 KB |
Output is correct |
17 |
Correct |
102 ms |
25060 KB |
Output is correct |
18 |
Correct |
85 ms |
32088 KB |
Output is correct |
19 |
Correct |
71 ms |
29320 KB |
Output is correct |
20 |
Correct |
51 ms |
22468 KB |
Output is correct |
21 |
Correct |
125 ms |
50472 KB |
Output is correct |
22 |
Correct |
168 ms |
51452 KB |
Output is correct |
23 |
Correct |
194 ms |
42308 KB |
Output is correct |
24 |
Correct |
128 ms |
47444 KB |
Output is correct |
25 |
Correct |
493 ms |
96460 KB |
Output is correct |
26 |
Correct |
367 ms |
130868 KB |
Output is correct |
27 |
Correct |
500 ms |
123136 KB |
Output is correct |
28 |
Correct |
658 ms |
109936 KB |
Output is correct |
29 |
Correct |
640 ms |
108460 KB |
Output is correct |
30 |
Correct |
582 ms |
112328 KB |
Output is correct |
31 |
Correct |
524 ms |
72076 KB |
Output is correct |
32 |
Correct |
436 ms |
108320 KB |
Output is correct |