#include <bits/stdc++.h>
#define pb push_back
#define er erase
#define in insert
#define s(a, x, y) sort(a.begin()+x, a.begin()+y);
#define r(a, x, y) reverse(a.begin()+x, a.begin()+y);
#define mp make_pair
#define F first
#define S second
#define ll long long
#define ld long double
#define forn(i, a, b) for(int i=a; i<=b; i++)
#define form(i, a, b) for(int i=a; i>=b; i--)
#define PI 3.14159265358979
#define inf 2147483647
#define sp setprecision
#define pii pair <int, int>
#define gcd __gcd
#define endl '\n'
#define sz(a) (int)a.size()
using namespace std;
const ll N = 4000 + 13;
ll t1, n, m, d[N][N], ans;
vector <pii> g[N][N];
string t;
char s[N][N];
deque <pii> q;
bool st1[N][N], st2[N][N];
pii v;
void solve(){
cin >> n >> m;
forn (i, 1, max (n, m)){
s[i][0] = s[0][i] = '.';
}
forn (i, 1, n){
cin >> t;
forn (j, 1, m){
s[i][j] = t[j - 1];
if (s[i][j] != '.' && s[i - 1][j] != '.') {
g[i][j].pb ({i - 1, j});
g[i - 1][j].pb ({i, j});
if (s[i][j] == s[i - 1][j]) st2[i][j] = 0;
else st2[i][j] = 1;
}
if (s[i][j] != '.' && s[i][j - 1] != '.') {
g[i][j].pb ({i, j - 1});
g[i][j - 1].pb ({i, j});
if (s[i][j] == s[i][j - 1]) st1[i][j] = 0;
else st1[i][j] = 1;
}
}
}
q.push_back ({1, 1});
d[1][1] = 1;
while (sz(q)){
v = q[0];
q.pop_front();
for (pii to: g[v.F][v.S]){
if (!d[to.F][to.S]){
d[to.F][to.S] = d[v.F][v.S];
if (v.F == to.F && !st1[v.F][max (v.S, to.S)] || v.S == to.S && !st2[max (v.F, to.F)][v.S]) q.push_front (to);
else {
q.push_back (to);
d[to.F][to.S] ++;
ans = max (ans, d[to.F][to.S]);
}
}
}
}
// forn (i, 1, n){
// forn (j, 1, m){
// cout << d[i][j];
// }
// cout << endl;
// }
cout << ans << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// cin >> t1;
t1 = 1;
while (t1 --){
solve();
}
return 0;
}
/*
5
0.50 0.29 0.49 0.95 0.66
2 3
0 3
3 4
2 1
5
0.50 0.29 0.49 0.95 0.83
2 3
0 3
3 4
2 1
1
4 0.66
*/
Compilation message
tracks.cpp: In function 'void solve()':
tracks.cpp:61:21: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
61 | if (v.F == to.F && !st1[v.F][max (v.S, to.S)] || v.S == to.S && !st2[max (v.F, to.F)][v.S]) q.push_front (to);
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
231 ms |
399320 KB |
Output is correct |
2 |
Correct |
179 ms |
378716 KB |
Output is correct |
3 |
Correct |
180 ms |
379360 KB |
Output is correct |
4 |
Correct |
208 ms |
395000 KB |
Output is correct |
5 |
Correct |
208 ms |
384428 KB |
Output is correct |
6 |
Correct |
178 ms |
378724 KB |
Output is correct |
7 |
Correct |
188 ms |
379320 KB |
Output is correct |
8 |
Correct |
196 ms |
379712 KB |
Output is correct |
9 |
Correct |
176 ms |
380164 KB |
Output is correct |
10 |
Correct |
186 ms |
384000 KB |
Output is correct |
11 |
Correct |
191 ms |
383892 KB |
Output is correct |
12 |
Correct |
194 ms |
387656 KB |
Output is correct |
13 |
Correct |
188 ms |
384352 KB |
Output is correct |
14 |
Correct |
194 ms |
384312 KB |
Output is correct |
15 |
Correct |
217 ms |
396324 KB |
Output is correct |
16 |
Correct |
244 ms |
399436 KB |
Output is correct |
17 |
Correct |
205 ms |
392520 KB |
Output is correct |
18 |
Correct |
215 ms |
395184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
230 ms |
429088 KB |
Output is correct |
2 |
Correct |
306 ms |
433136 KB |
Output is correct |
3 |
Correct |
769 ms |
638484 KB |
Output is correct |
4 |
Correct |
373 ms |
456948 KB |
Output is correct |
5 |
Correct |
611 ms |
603852 KB |
Output is correct |
6 |
Runtime error |
1536 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Correct |
201 ms |
431236 KB |
Output is correct |
8 |
Correct |
206 ms |
429020 KB |
Output is correct |
9 |
Correct |
198 ms |
395616 KB |
Output is correct |
10 |
Correct |
189 ms |
394624 KB |
Output is correct |
11 |
Correct |
200 ms |
426888 KB |
Output is correct |
12 |
Correct |
171 ms |
380852 KB |
Output is correct |
13 |
Correct |
305 ms |
433048 KB |
Output is correct |
14 |
Correct |
252 ms |
414044 KB |
Output is correct |
15 |
Correct |
250 ms |
407248 KB |
Output is correct |
16 |
Correct |
239 ms |
408432 KB |
Output is correct |
17 |
Correct |
504 ms |
497712 KB |
Output is correct |
18 |
Correct |
462 ms |
468288 KB |
Output is correct |
19 |
Correct |
376 ms |
456696 KB |
Output is correct |
20 |
Correct |
319 ms |
455892 KB |
Output is correct |
21 |
Correct |
532 ms |
547856 KB |
Output is correct |
22 |
Correct |
611 ms |
603872 KB |
Output is correct |
23 |
Correct |
804 ms |
591328 KB |
Output is correct |
24 |
Correct |
543 ms |
544424 KB |
Output is correct |
25 |
Correct |
1639 ms |
678624 KB |
Output is correct |
26 |
Runtime error |
1399 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Runtime error |
1396 ms |
1048576 KB |
Execution killed with signal 9 |
28 |
Runtime error |
1465 ms |
1048576 KB |
Execution killed with signal 9 |
29 |
Runtime error |
1451 ms |
1048576 KB |
Execution killed with signal 9 |
30 |
Runtime error |
1440 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Execution timed out |
2114 ms |
959260 KB |
Time limit exceeded |
32 |
Runtime error |
1679 ms |
1048576 KB |
Execution killed with signal 9 |