//Dost SEFEROĞLU
#include <bits/stdc++.h>
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
using namespace std;
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<
#define all(cont) cont.begin(),cont.end()
#define vi vector<int>
const int inf = 2e18,MOD = 1e9+7;
void solve() {
int n,m;
cin >> n >> m;
char grid[n][m];
for (int i=0;i<n;i++) {
string s;
cin >> s;
for (int j = 0;j<m;j++) grid[i][j] = s[j];
}
int dist[n][m];
for (int i=0;i<n;i++) for (int j = 0;j<m;j++) dist[i][j] = inf;
deque<pii> dq;
dist[n-1][m-1] = 1;
dq.push_front({n-1,m-1});
vi dx = {0,1,0,-1},dy = {1,0,-1,0};
while (!dq.empty()) {
pii f = dq.front();
dq.pop_front();
int x = f.ff,y = f.ss;
for (int i = 0;i<4;i++) {
int gx = x+dx[i],gy = y+dy[i];
if (gx < 0 || gx >= n || gy < 0 || gy >= m) continue;
if (grid[gx][gy] == '.') continue;
if (dist[gx][gy] <= dist[x][y]+(grid[gx][gy] != grid[x][y])) continue;
dist[gx][gy] = dist[x][y]+(grid[gx][gy] != grid[x][y]);
if (grid[gx][gy] == grid[x][y]) dq.push_front({gx,gy});
else dq.push_back({gx,gy});
}
}/*
for (int i = 0;i<n;i++) {
for (int j = 0;j<m;j++) {
if (dist[i][j] != inf) cout << dist[i][j] << " ";
else cout << "- ";
}
cout << '\n';
} */
int ans = 0;
for (int i=0;i<n;i++) for (int j = 0;j<m;j++) if (dist[i][j] != inf) ans = max(ans,dist[i][j]);
cout << ans << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Dodi
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);/*
#else
freopen("odometer.in","r",stdin);
freopen("odometer.out","w",stdout); */
#endif
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
2732 KB |
Output is correct |
2 |
Correct |
2 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
7 ms |
2272 KB |
Output is correct |
5 |
Correct |
3 ms |
1136 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
852 KB |
Output is correct |
11 |
Correct |
3 ms |
792 KB |
Output is correct |
12 |
Correct |
5 ms |
1240 KB |
Output is correct |
13 |
Correct |
3 ms |
1108 KB |
Output is correct |
14 |
Correct |
3 ms |
1108 KB |
Output is correct |
15 |
Correct |
11 ms |
2900 KB |
Output is correct |
16 |
Correct |
13 ms |
2852 KB |
Output is correct |
17 |
Correct |
10 ms |
2644 KB |
Output is correct |
18 |
Correct |
9 ms |
2424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
852 KB |
Output is correct |
2 |
Correct |
51 ms |
15572 KB |
Output is correct |
3 |
Correct |
314 ms |
157008 KB |
Output is correct |
4 |
Correct |
92 ms |
37172 KB |
Output is correct |
5 |
Correct |
156 ms |
88512 KB |
Output is correct |
6 |
Correct |
847 ms |
185024 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
3 ms |
852 KB |
Output is correct |
10 |
Correct |
2 ms |
852 KB |
Output is correct |
11 |
Correct |
2 ms |
852 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
45 ms |
15852 KB |
Output is correct |
14 |
Correct |
31 ms |
9300 KB |
Output is correct |
15 |
Correct |
21 ms |
10264 KB |
Output is correct |
16 |
Correct |
23 ms |
6592 KB |
Output is correct |
17 |
Correct |
135 ms |
40160 KB |
Output is correct |
18 |
Correct |
100 ms |
39416 KB |
Output is correct |
19 |
Correct |
103 ms |
37076 KB |
Output is correct |
20 |
Correct |
76 ms |
34300 KB |
Output is correct |
21 |
Correct |
183 ms |
91656 KB |
Output is correct |
22 |
Correct |
168 ms |
88396 KB |
Output is correct |
23 |
Correct |
263 ms |
76336 KB |
Output is correct |
24 |
Correct |
178 ms |
89388 KB |
Output is correct |
25 |
Correct |
317 ms |
157052 KB |
Output is correct |
26 |
Correct |
731 ms |
221248 KB |
Output is correct |
27 |
Correct |
784 ms |
207444 KB |
Output is correct |
28 |
Correct |
841 ms |
184996 KB |
Output is correct |
29 |
Correct |
821 ms |
182824 KB |
Output is correct |
30 |
Correct |
840 ms |
194532 KB |
Output is correct |
31 |
Correct |
517 ms |
101616 KB |
Output is correct |
32 |
Correct |
797 ms |
204964 KB |
Output is correct |