#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<long long, long long> pll;
using namespace std;
int n, m;
vector<string> mat;
bool vis[4001][4001];
int dist[4001][4001], dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
bool in_range(int i, int j){
if(i >= 0 && j >= 0 && i < n && j < m) return true;
return false;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
mat.resize(n);
for(int i = 0; i < n; i++)
cin>>mat[i];
priority_queue<pair<int, pii>> q;
q.push(mp(0, mp(0, 0)));
int ans = 1;
dist[0][0] = vis[0][0] = 1;
while(!q.empty()){
pii at = q.top().second;
q.pop();
ans = max(ans, dist[at.first][at.second]);
for(int dir = 0; dir < 4; dir++){
int nx = at.first + dx[dir], ny = at.second + dy[dir];
if(in_range(nx, ny) && mat[nx][ny] != '.' && !vis[nx][ny]){
vis[nx][ny] = true;
dist[nx][ny] = dist[at.first][at.second] + (mat[at.first][at.second] != mat[nx][ny]);
q.push(mp(-dist[nx][ny], mp(nx,ny)));
}
}
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
5536 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
25 ms |
5364 KB |
Output is correct |
5 |
Correct |
4 ms |
2900 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
772 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
5 ms |
2504 KB |
Output is correct |
11 |
Correct |
7 ms |
2260 KB |
Output is correct |
12 |
Correct |
14 ms |
3156 KB |
Output is correct |
13 |
Correct |
4 ms |
2900 KB |
Output is correct |
14 |
Correct |
4 ms |
2900 KB |
Output is correct |
15 |
Correct |
31 ms |
5280 KB |
Output is correct |
16 |
Correct |
40 ms |
5540 KB |
Output is correct |
17 |
Correct |
18 ms |
5272 KB |
Output is correct |
18 |
Correct |
25 ms |
5372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
30920 KB |
Output is correct |
2 |
Correct |
102 ms |
15276 KB |
Output is correct |
3 |
Correct |
288 ms |
75520 KB |
Output is correct |
4 |
Correct |
107 ms |
33048 KB |
Output is correct |
5 |
Correct |
120 ms |
54308 KB |
Output is correct |
6 |
Execution timed out |
2093 ms |
116400 KB |
Time limit exceeded |
7 |
Correct |
16 ms |
32212 KB |
Output is correct |
8 |
Correct |
14 ms |
30824 KB |
Output is correct |
9 |
Correct |
4 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
14 ms |
31672 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
98 ms |
15240 KB |
Output is correct |
14 |
Correct |
56 ms |
10444 KB |
Output is correct |
15 |
Correct |
16 ms |
13176 KB |
Output is correct |
16 |
Correct |
51 ms |
5780 KB |
Output is correct |
17 |
Correct |
251 ms |
29088 KB |
Output is correct |
18 |
Correct |
65 ms |
35984 KB |
Output is correct |
19 |
Correct |
103 ms |
32972 KB |
Output is correct |
20 |
Correct |
74 ms |
25820 KB |
Output is correct |
21 |
Correct |
173 ms |
53548 KB |
Output is correct |
22 |
Correct |
117 ms |
54364 KB |
Output is correct |
23 |
Correct |
521 ms |
44860 KB |
Output is correct |
24 |
Correct |
97 ms |
50716 KB |
Output is correct |
25 |
Correct |
412 ms |
96344 KB |
Output is correct |
26 |
Correct |
1359 ms |
82500 KB |
Output is correct |
27 |
Execution timed out |
2062 ms |
98984 KB |
Time limit exceeded |
28 |
Execution timed out |
2091 ms |
116468 KB |
Time limit exceeded |
29 |
Execution timed out |
2072 ms |
108200 KB |
Time limit exceeded |
30 |
Execution timed out |
2045 ms |
104944 KB |
Time limit exceeded |
31 |
Execution timed out |
2012 ms |
75064 KB |
Time limit exceeded |
32 |
Correct |
1923 ms |
97896 KB |
Output is correct |