#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;
}
void bfs() {
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();
for (auto it: g[u]) {
pii v = it.first;
int w = it.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 main() {
IOS();
cin >> m >> n;
rep(i, m) {
rep(j, n) {
cin >> s[i][j];
}
}
rep(i, m) {
rep(j, n) {
forn(k, 4) {
int x1 = i + d[k][0], y1 = j + d[k][1];
if (check(x1, y1)) {
if (s[x1][y1] == s[i][j]) {
g[mk(i, j)].pb(mk(mk(x1, y1), 0));
}
else {
g[mk(i, j)].pb(mk(mk(x1, y1), 1));
}
}
}
}
}
bfs();
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
533 ms |
66732 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
3 |
Incorrect |
3 ms |
1252 KB |
Output isn't correct |
4 |
Correct |
330 ms |
43388 KB |
Output is correct |
5 |
Incorrect |
153 ms |
23876 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
7 |
Incorrect |
3 ms |
1116 KB |
Output isn't correct |
8 |
Correct |
6 ms |
1968 KB |
Output is correct |
9 |
Incorrect |
13 ms |
3464 KB |
Output isn't correct |
10 |
Incorrect |
109 ms |
18772 KB |
Output isn't correct |
11 |
Correct |
73 ms |
11728 KB |
Output is correct |
12 |
Incorrect |
168 ms |
24656 KB |
Output isn't correct |
13 |
Incorrect |
160 ms |
23888 KB |
Output isn't correct |
14 |
Incorrect |
153 ms |
23832 KB |
Output isn't correct |
15 |
Incorrect |
514 ms |
69200 KB |
Output isn't correct |
16 |
Incorrect |
519 ms |
66604 KB |
Output isn't correct |
17 |
Incorrect |
509 ms |
65872 KB |
Output isn't correct |
18 |
Correct |
319 ms |
43348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
83 ms |
28496 KB |
Output isn't correct |
2 |
Execution timed out |
2078 ms |
377172 KB |
Time limit exceeded |
3 |
Execution timed out |
2039 ms |
430788 KB |
Time limit exceeded |
4 |
Execution timed out |
2093 ms |
405072 KB |
Time limit exceeded |
5 |
Execution timed out |
2065 ms |
432468 KB |
Time limit exceeded |
6 |
Execution timed out |
2048 ms |
426008 KB |
Time limit exceeded |
7 |
Incorrect |
65 ms |
27732 KB |
Output isn't correct |
8 |
Incorrect |
77 ms |
28492 KB |
Output isn't correct |
9 |
Incorrect |
89 ms |
16080 KB |
Output isn't correct |
10 |
Incorrect |
41 ms |
9044 KB |
Output isn't correct |
11 |
Incorrect |
66 ms |
28244 KB |
Output isn't correct |
12 |
Incorrect |
33 ms |
6992 KB |
Output isn't correct |
13 |
Execution timed out |
2060 ms |
377168 KB |
Time limit exceeded |
14 |
Incorrect |
1833 ms |
244676 KB |
Output isn't correct |
15 |
Execution timed out |
2050 ms |
265108 KB |
Time limit exceeded |
16 |
Incorrect |
1324 ms |
172880 KB |
Output isn't correct |
17 |
Execution timed out |
2080 ms |
436564 KB |
Time limit exceeded |
18 |
Execution timed out |
2061 ms |
443476 KB |
Time limit exceeded |
19 |
Execution timed out |
2084 ms |
444284 KB |
Time limit exceeded |
20 |
Execution timed out |
2061 ms |
433384 KB |
Time limit exceeded |
21 |
Execution timed out |
2118 ms |
463812 KB |
Time limit exceeded |
22 |
Execution timed out |
2025 ms |
443976 KB |
Time limit exceeded |
23 |
Execution timed out |
2063 ms |
436724 KB |
Time limit exceeded |
24 |
Execution timed out |
2075 ms |
442524 KB |
Time limit exceeded |
25 |
Execution timed out |
2082 ms |
436508 KB |
Time limit exceeded |
26 |
Execution timed out |
2040 ms |
437328 KB |
Time limit exceeded |
27 |
Execution timed out |
2033 ms |
436416 KB |
Time limit exceeded |
28 |
Execution timed out |
2043 ms |
436816 KB |
Time limit exceeded |
29 |
Execution timed out |
2041 ms |
430900 KB |
Time limit exceeded |
30 |
Execution timed out |
2061 ms |
410204 KB |
Time limit exceeded |
31 |
Execution timed out |
2089 ms |
421208 KB |
Time limit exceeded |
32 |
Execution timed out |
2079 ms |
426780 KB |
Time limit exceeded |