//chockolateman
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int H,W,di[4] = {1,-1,0,0},dj[4] = {0,0,1,-1},dist[4005][4005];
char grid[4005][4005];
bool is_ok(int i,int j)
{
if(i >= 1 && i <= H && j >= 1 && j <= W && grid[i][j] != '.')
return true;
return false;
}
void BFS(int starti,int startj)
{
for(int i = 1 ; i <= H ; i++)
for(int j = 1 ; j <= W ; j++)
if(grid[i][j]!='.')
dist[i][j] = INF;
deque<pair<int,int>> q;
dist[starti][startj] = 1;
q.push_back({starti,startj});
while(!q.empty())
{
pair<int,int> v = q.front();
int i = v.first;
int j = v.second;
q.pop_front();
for(int k = 0 ; k <= 3 ; k++)
{
int new_i = i + di[k];
int new_j = j + dj[k];
if(is_ok(new_i,new_j))
{
if(grid[i][j]==grid[new_i][new_j])
{
if(dist[i][j] < dist[new_i][new_j])
{
dist[new_i][new_j] = dist[i][j];
q.push_front({new_i,new_j});
}
}
else
{
if(dist[i][j] + 1 < dist[new_i][new_j])
{
dist[new_i][new_j] = dist[i][j] + 1;
q.push_back({new_i,new_j});
}
}
}
}
}
}
int main()
{
scanf("%d%d",&H,&W);
for(int i = 1 ; i <= H ; i++)
for(int j = 1 ; j <= W ; j++)
scanf(" %c",&grid[i][j]);
BFS(1,1);
int ans = 0;
for(int i = 1 ; i <= H ; i++)
for(int j = 1 ; j <= W ; j++)
ans = max(ans,dist[i][j]);
printf("%d\n",ans);
return 0;
}
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:63:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%d%d",&H,&W);
| ~~~~~^~~~~~~~~~~~~~
tracks.cpp:66:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf(" %c",&grid[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
12892 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
9 ms |
12892 KB |
Output is correct |
5 |
Correct |
4 ms |
8792 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
6748 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
4 ms |
8796 KB |
Output is correct |
11 |
Correct |
3 ms |
8792 KB |
Output is correct |
12 |
Correct |
7 ms |
8792 KB |
Output is correct |
13 |
Correct |
6 ms |
8644 KB |
Output is correct |
14 |
Correct |
4 ms |
8792 KB |
Output is correct |
15 |
Correct |
15 ms |
12904 KB |
Output is correct |
16 |
Correct |
16 ms |
12996 KB |
Output is correct |
17 |
Correct |
16 ms |
12892 KB |
Output is correct |
18 |
Correct |
10 ms |
12888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
32092 KB |
Output is correct |
2 |
Correct |
71 ms |
21444 KB |
Output is correct |
3 |
Correct |
541 ms |
57680 KB |
Output is correct |
4 |
Correct |
144 ms |
32600 KB |
Output is correct |
5 |
Correct |
336 ms |
47448 KB |
Output is correct |
6 |
Correct |
800 ms |
108000 KB |
Output is correct |
7 |
Correct |
10 ms |
32092 KB |
Output is correct |
8 |
Correct |
8 ms |
32312 KB |
Output is correct |
9 |
Correct |
3 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
7 ms |
31564 KB |
Output is correct |
12 |
Correct |
2 ms |
6744 KB |
Output is correct |
13 |
Correct |
65 ms |
20064 KB |
Output is correct |
14 |
Correct |
51 ms |
18260 KB |
Output is correct |
15 |
Correct |
33 ms |
18264 KB |
Output is correct |
16 |
Correct |
27 ms |
10856 KB |
Output is correct |
17 |
Correct |
163 ms |
27988 KB |
Output is correct |
18 |
Correct |
144 ms |
34128 KB |
Output is correct |
19 |
Correct |
142 ms |
32104 KB |
Output is correct |
20 |
Correct |
116 ms |
26196 KB |
Output is correct |
21 |
Correct |
405 ms |
43616 KB |
Output is correct |
22 |
Correct |
345 ms |
44628 KB |
Output is correct |
23 |
Correct |
371 ms |
38308 KB |
Output is correct |
24 |
Correct |
332 ms |
40804 KB |
Output is correct |
25 |
Correct |
591 ms |
78692 KB |
Output is correct |
26 |
Correct |
471 ms |
120672 KB |
Output is correct |
27 |
Correct |
738 ms |
120964 KB |
Output is correct |
28 |
Correct |
786 ms |
107996 KB |
Output is correct |
29 |
Correct |
806 ms |
106184 KB |
Output is correct |
30 |
Correct |
757 ms |
110292 KB |
Output is correct |
31 |
Correct |
613 ms |
63572 KB |
Output is correct |
32 |
Correct |
626 ms |
109576 KB |
Output is correct |