///* __ __ __ */
///* ====== _ /| /| __ _ / | | /| | * | | | | / /| |\ | / | | * | / */
///* \- || |_| |_ / |/ | | | |_ |- | |--| /-| | | \ \ |==| |- /=| | \ | | |--| | |- */
///* || | | |_ / | |__| _| |_ \__ | | / | |__ | __| | | | \ / | | \| \__ | | | | \ */
///*
// autor :: Rick Prime
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
#define Mp make_pair
typedef vector<int> vi;
typedef vector<double> vd;
typedef pair<int,int> pii;
typedef vector<pii> vii;
const int N = 1e6, inf = 2e18, mod = 1e9+7;
void solve(){
//freopen("snakes.in", "r", stdin);
//freopen("snakes.out", "w", stdout );
int h, w;
cin >> h >> w;
char a[h][w];
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
cin >> a[i][j];
}
}
priority_queue<pair<int, pii>, vector<pair<int,pii>>, greater<pair<int, pii>>> q;
q.push({-1,{0, 0}});
vi di = {-1, 1, 0, 0};
vi dj = {0, 0, -1, 1};
int ans = 1;
int ds[h][w]{};
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
ds[i][j] = -inf;
}
}
ds[0][0] = -1;
while(!q.empty()){
auto [dis, ps] = q.top();
auto [x, y] = ps;
q.pop();
for(int k = 0; k < 4; k++){
int nx = x + di[k];
int ny = y + dj[k];
if(nx<0 || ny<0 || nx>=h || ny>=w || a[nx][ny] == '.')continue;
if(ds[nx][ny] >= dis - (a[x][y] != a[nx][ny]))continue;
ds[nx][ny] = dis - (a[x][y] != a[nx][ny]);
q.push({ds[nx][ny], {nx, ny}});
}
}
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++)ans = max(ans, -(ds[i][j]==-inf?inf:ds[i][j]));
//{
//if(ds[i][j] == -inf){
//cout << "?" << ' ';
//continue;
//}
//cout << -ds[i][j] << ' ';
//}cout << '\n';
}
cout << ans << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
int test = 1;
//cin >> test;
while(test--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2036 ms |
5836 KB |
Time limit exceeded |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
5 ms |
492 KB |
Output is correct |
4 |
Execution timed out |
2095 ms |
5000 KB |
Time limit exceeded |
5 |
Correct |
1734 ms |
1384 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
7 ms |
488 KB |
Output is correct |
8 |
Correct |
103 ms |
348 KB |
Output is correct |
9 |
Correct |
6 ms |
348 KB |
Output is correct |
10 |
Correct |
1923 ms |
1244 KB |
Output is correct |
11 |
Execution timed out |
2069 ms |
1624 KB |
Time limit exceeded |
12 |
Execution timed out |
2068 ms |
2772 KB |
Time limit exceeded |
13 |
Correct |
1659 ms |
1412 KB |
Output is correct |
14 |
Correct |
1685 ms |
1372 KB |
Output is correct |
15 |
Execution timed out |
2041 ms |
4548 KB |
Time limit exceeded |
16 |
Execution timed out |
2045 ms |
5836 KB |
Time limit exceeded |
17 |
Execution timed out |
2064 ms |
3672 KB |
Time limit exceeded |
18 |
Execution timed out |
2041 ms |
5068 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
860 KB |
Output is correct |
2 |
Execution timed out |
2086 ms |
17216 KB |
Time limit exceeded |
3 |
Execution timed out |
2070 ms |
158412 KB |
Time limit exceeded |
4 |
Execution timed out |
2072 ms |
38608 KB |
Time limit exceeded |
5 |
Correct |
222 ms |
88404 KB |
Output is correct |
6 |
Execution timed out |
2081 ms |
354032 KB |
Time limit exceeded |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Execution timed out |
2097 ms |
1244 KB |
Time limit exceeded |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
496 KB |
Output is correct |
13 |
Execution timed out |
2058 ms |
17352 KB |
Time limit exceeded |
14 |
Execution timed out |
2033 ms |
10696 KB |
Time limit exceeded |
15 |
Correct |
21 ms |
10076 KB |
Output is correct |
16 |
Execution timed out |
2066 ms |
7496 KB |
Time limit exceeded |
17 |
Execution timed out |
2063 ms |
40564 KB |
Time limit exceeded |
18 |
Correct |
103 ms |
39544 KB |
Output is correct |
19 |
Execution timed out |
2055 ms |
38776 KB |
Time limit exceeded |
20 |
Execution timed out |
2078 ms |
35588 KB |
Time limit exceeded |
21 |
Execution timed out |
2011 ms |
93124 KB |
Time limit exceeded |
22 |
Correct |
237 ms |
88660 KB |
Output is correct |
23 |
Execution timed out |
2060 ms |
76620 KB |
Time limit exceeded |
24 |
Correct |
188 ms |
89172 KB |
Output is correct |
25 |
Correct |
627 ms |
156956 KB |
Output is correct |
26 |
Correct |
1070 ms |
120664 KB |
Output is correct |
27 |
Execution timed out |
2052 ms |
354064 KB |
Time limit exceeded |
28 |
Execution timed out |
2053 ms |
354020 KB |
Time limit exceeded |
29 |
Execution timed out |
2068 ms |
353884 KB |
Time limit exceeded |
30 |
Execution timed out |
2054 ms |
350636 KB |
Time limit exceeded |
31 |
Execution timed out |
2060 ms |
297844 KB |
Time limit exceeded |
32 |
Execution timed out |
2044 ms |
206212 KB |
Time limit exceeded |