// IN THE NAME OF GOD
#include<bits/stdc++.h>
#define endl '\n'
#define file_reading freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define flush cout.flush();
using namespace std;
typedef long long ll;
typedef long double dll;
typedef unsigned long long ull;
const int N = 4e3+5, inf = 1e9;
int dat[N][N], dist[N][N], ans, must;
deque<pair<int,int>> q;
void dij(int n, int m){
for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) dist[i][j] = inf;
dist[n][m] = 1;
q.push_front({n,m});
while(q.size()){
int x = q.front().first, y = q.front().second; q.pop_front();
ans = max(ans,dist[x][y]);
if (x-1 >= 1 && dat[x][y] == dat[x-1][y] && dist[x-1][y] >= inf) dist[x-1][y] = dist[x][y], q.push_front({x-1,y});
if (x+1 <= n && dat[x][y] == dat[x+1][y] && dist[x+1][y] >= inf) dist[x+1][y] = dist[x][y], q.push_front({x+1,y});
if (y-1 >= 1 && dat[x][y] == dat[x][y-1] && dist[x][y-1] >= inf) dist[x][y-1] = dist[x][y], q.push_front({x,y-1});
if (y+1 <= m && dat[x][y] == dat[x][y+1] && dist[x][y+1] >= inf) dist[x][y+1] = dist[x][y], q.push_front({x,y+1});
if (x-1 >= 1 && dat[x][y] != dat[x-1][y] && dat[x-1][y] && dist[x-1][y] >= inf) dist[x-1][y] = dist[x][y]+1, q.push_back({x-1,y});
if (x+1 <= n && dat[x][y] != dat[x+1][y] && dat[x+1][y] && dist[x+1][y] >= inf) dist[x+1][y] = dist[x][y]+1, q.push_back({x+1,y});
if (y-1 >= 1 && dat[x][y] != dat[x][y-1] && dat[x][y-1] && dist[x][y-1] >= inf) dist[x][y-1] = dist[x][y]+1, q.push_back({x,y-1});
if (y+1 <= m && dat[x][y] != dat[x][y+1] && dat[x][y+1] && dist[x][y+1] >= inf) dist[x][y+1] = dist[x][y]+1, q.push_back({x,y+1});
}
}
void solve(){
int n,m; cin >> n >> m;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
char g; cin >> g;
if (g == '.') dat[i][j] = 0;
else if (g == 'R') dat[i][j] = 1, must++;
else dat[i][j] = 2, must++;
}
}
if (must == 0){cout << 0 << endl; return;}
dij(n,m);
cout << ans << endl;
}
int32_t main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t; t=1; //cin >> t;
while(t--){solve();}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
6228 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
10 ms |
5716 KB |
Output is correct |
5 |
Correct |
4 ms |
3284 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
4 ms |
2772 KB |
Output is correct |
11 |
Correct |
4 ms |
2260 KB |
Output is correct |
12 |
Correct |
8 ms |
3284 KB |
Output is correct |
13 |
Correct |
4 ms |
3284 KB |
Output is correct |
14 |
Correct |
4 ms |
3284 KB |
Output is correct |
15 |
Correct |
16 ms |
6328 KB |
Output is correct |
16 |
Correct |
20 ms |
6244 KB |
Output is correct |
17 |
Correct |
13 ms |
5972 KB |
Output is correct |
18 |
Correct |
12 ms |
5716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
31476 KB |
Output is correct |
2 |
Correct |
65 ms |
22608 KB |
Output is correct |
3 |
Correct |
389 ms |
125692 KB |
Output is correct |
4 |
Correct |
111 ms |
44748 KB |
Output is correct |
5 |
Correct |
237 ms |
94396 KB |
Output is correct |
6 |
Correct |
801 ms |
138348 KB |
Output is correct |
7 |
Correct |
14 ms |
32820 KB |
Output is correct |
8 |
Correct |
15 ms |
31452 KB |
Output is correct |
9 |
Correct |
3 ms |
836 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
13 ms |
32212 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
66 ms |
22608 KB |
Output is correct |
14 |
Correct |
38 ms |
14656 KB |
Output is correct |
15 |
Correct |
38 ms |
16204 KB |
Output is correct |
16 |
Correct |
31 ms |
8444 KB |
Output is correct |
17 |
Correct |
158 ms |
48232 KB |
Output is correct |
18 |
Correct |
152 ms |
47584 KB |
Output is correct |
19 |
Correct |
125 ms |
44680 KB |
Output is correct |
20 |
Correct |
95 ms |
41256 KB |
Output is correct |
21 |
Correct |
246 ms |
97528 KB |
Output is correct |
22 |
Correct |
256 ms |
94364 KB |
Output is correct |
23 |
Correct |
302 ms |
78760 KB |
Output is correct |
24 |
Correct |
232 ms |
96344 KB |
Output is correct |
25 |
Correct |
650 ms |
125716 KB |
Output is correct |
26 |
Correct |
467 ms |
160216 KB |
Output is correct |
27 |
Correct |
583 ms |
145776 KB |
Output is correct |
28 |
Correct |
803 ms |
138320 KB |
Output is correct |
29 |
Correct |
801 ms |
136952 KB |
Output is correct |
30 |
Correct |
688 ms |
140464 KB |
Output is correct |
31 |
Correct |
787 ms |
101156 KB |
Output is correct |
32 |
Correct |
549 ms |
146204 KB |
Output is correct |