#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mk make_pair
#define pb push_back
#define alls(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define rep(i, n) for (int i = 1; i <= int(n); i++)
#define inc(i, l, r, d) for (int i = l; i <= r; i += d)
#define dec(i, r, l, d) for (int i = r; i >= l; i -= d)
#define dbg(x) cerr << #x << " = " << x << endl;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
const ld eps = 1e-12;
const ll inf = 1e16;
const ll mod = 998244353;
const ll mod1 = 1e9 + 87;
const ll mod2 = 127397154761;
ll qpow(ll a, ll b) {if (b == 0) return 1; ll ans = qpow(a, b >> 1); ans = ans * ans % mod; if (b & 1) ans = ans * a % mod; return ans;}
using namespace std;
void IOS(string name = "") {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
if ((int)name.size()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
const int maxn = 4005;
map<pii, vector<pair<pii, int>>> g;
int m, n;
char s[maxn][maxn];
int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
map<pii, int > dis;
map<pii, bool> vis;
bool check(int x1, int y1) {
return x1 > 0 && y1 > 0 && x1 <= m && y1 <= n;
}
int main() {
IOS();
cin >> m >> n;
rep(i, m) {
rep(j, n) {
cin >> s[i][j];
}
}
if (s[1][1] == '.') {
cout << 0 << endl;
return 0;
}
deque<pii> dq;
rep(i, m) {
rep(j, n) {
dis[mk(i, j)] = INT_MAX - 10;
}
}
dis[mk(1, 1)] = 1;
dq.pb(mk(1, 1));
vis[mk(1, 1)] = true;
while (!dq.empty()) {
pii u = dq.front(); dq.pop_front();
forn(k, 4) {
int x1 = u.first + d[k][0], y1 = u.second + d[k][1];
if (!check(x1, y1)) continue;
if (s[x1][y1] == '.') continue;
pii v = mk(x1, y1);
int w = (s[x1][y1] != s[u.first][u.second]);
if (vis[v]) continue;
if (dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
vis[v] = true;
if (w == 1) {
dq.pb(v);
}
else {
dq.push_front(v);
}
}
}
}
int ans = 0;
rep(i, m) {
rep(j, n) {
if (s[i][j] != '.') {
ans = max(ans, dis[mk(i, j)]);
}
}
}
cout << ans << endl;
return 0;
}
Compilation message
tracks.cpp: In function 'void IOS(std::string)':
tracks.cpp:29:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:30:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
319 ms |
31972 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
193 ms |
21580 KB |
Output is correct |
5 |
Correct |
39 ms |
7904 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
888 KB |
Output is correct |
8 |
Correct |
5 ms |
1116 KB |
Output is correct |
9 |
Correct |
3 ms |
1372 KB |
Output is correct |
10 |
Correct |
38 ms |
7124 KB |
Output is correct |
11 |
Correct |
42 ms |
6236 KB |
Output is correct |
12 |
Correct |
105 ms |
12116 KB |
Output is correct |
13 |
Correct |
35 ms |
8016 KB |
Output is correct |
14 |
Correct |
34 ms |
7992 KB |
Output is correct |
15 |
Correct |
237 ms |
29268 KB |
Output is correct |
16 |
Correct |
290 ms |
32180 KB |
Output is correct |
17 |
Correct |
174 ms |
23888 KB |
Output is correct |
18 |
Correct |
171 ms |
21584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
19288 KB |
Output is correct |
2 |
Correct |
1176 ms |
143928 KB |
Output is correct |
3 |
Execution timed out |
2072 ms |
559956 KB |
Time limit exceeded |
4 |
Execution timed out |
2066 ms |
278608 KB |
Time limit exceeded |
5 |
Execution timed out |
2072 ms |
577108 KB |
Time limit exceeded |
6 |
Execution timed out |
2079 ms |
531132 KB |
Time limit exceeded |
7 |
Correct |
27 ms |
19548 KB |
Output is correct |
8 |
Correct |
24 ms |
19360 KB |
Output is correct |
9 |
Correct |
34 ms |
5724 KB |
Output is correct |
10 |
Correct |
10 ms |
2908 KB |
Output is correct |
11 |
Correct |
17 ms |
19268 KB |
Output is correct |
12 |
Correct |
11 ms |
3160 KB |
Output is correct |
13 |
Correct |
1217 ms |
144012 KB |
Output is correct |
14 |
Correct |
686 ms |
84228 KB |
Output is correct |
15 |
Correct |
652 ms |
84560 KB |
Output is correct |
16 |
Correct |
593 ms |
64076 KB |
Output is correct |
17 |
Execution timed out |
2080 ms |
322644 KB |
Time limit exceeded |
18 |
Execution timed out |
2081 ms |
312936 KB |
Time limit exceeded |
19 |
Correct |
1990 ms |
278780 KB |
Output is correct |
20 |
Correct |
1707 ms |
278612 KB |
Output is correct |
21 |
Execution timed out |
2077 ms |
591696 KB |
Time limit exceeded |
22 |
Execution timed out |
2087 ms |
582484 KB |
Time limit exceeded |
23 |
Execution timed out |
2063 ms |
515152 KB |
Time limit exceeded |
24 |
Execution timed out |
2061 ms |
574448 KB |
Time limit exceeded |
25 |
Execution timed out |
2057 ms |
552524 KB |
Time limit exceeded |
26 |
Execution timed out |
2117 ms |
580128 KB |
Time limit exceeded |
27 |
Execution timed out |
2075 ms |
574804 KB |
Time limit exceeded |
28 |
Execution timed out |
2096 ms |
569920 KB |
Time limit exceeded |
29 |
Execution timed out |
2066 ms |
543932 KB |
Time limit exceeded |
30 |
Execution timed out |
2058 ms |
577284 KB |
Time limit exceeded |
31 |
Execution timed out |
2061 ms |
587340 KB |
Time limit exceeded |
32 |
Execution timed out |
2080 ms |
590664 KB |
Time limit exceeded |