#include<bits/stdc++.h>
using namespace std;
int n,m,dx[]={1,-1,0,0},dy[]={0,0,1,-1},d[4005][4005],u,v,sx,sy,x,y;
char s[4005][4005];
queue<pair<int,int> > q;
vector<pair<int,int> > g;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%s",s[i]+1);
}
g.push_back({1,1});
for(int i=1;;i++)
{
//printf("%d\n",i);
for(int j=0;j<g.size();j++)
{
sx=g[j].first;
sy=g[j].second;
//printf("%d %d\n",sx,sy);
q.push({sx,sy});
d[sx][sy]=1;
}
g.clear();
while(!q.empty())
{
u=q.front().first;
v=q.front().second;
q.pop();
for(int j=0;j<4;j++)
{
x=u+dx[j];
y=v+dy[j];
if(x>=1&&x<=n&&y>=1&&y<=m)
{
if(s[x][y]!='.')
{
if(s[x][y]!=s[u][v])
{
if(d[x][y]==0)
{
sx=x;
sy=y;
g.push_back({sx,sy});
}
}else
{
//printf("%d %d %d %d\n",u,v,x,y);
if(d[x][y]==0)
{
//printf("%d %d %d\n",x,y,d[x][y]);
d[x][y]=1;
q.push({x,y});
}
}
}
}
}
}
if(g.size()==0)
{
printf("%d\n",i);
break;
}
}
}
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:18:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for(int j=0;j<g.size();j++)
| ~^~~~~~~~~
tracks.cpp:9:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
tracks.cpp:12:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | scanf("%s",s[i]+1);
| ~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
5580 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
10 ms |
5320 KB |
Output is correct |
5 |
Correct |
3 ms |
2892 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
3 ms |
2380 KB |
Output is correct |
11 |
Correct |
3 ms |
2252 KB |
Output is correct |
12 |
Correct |
8 ms |
3148 KB |
Output is correct |
13 |
Correct |
3 ms |
2872 KB |
Output is correct |
14 |
Correct |
3 ms |
2892 KB |
Output is correct |
15 |
Correct |
17 ms |
5280 KB |
Output is correct |
16 |
Correct |
22 ms |
5588 KB |
Output is correct |
17 |
Correct |
11 ms |
5196 KB |
Output is correct |
18 |
Correct |
9 ms |
5320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
30772 KB |
Output is correct |
2 |
Correct |
65 ms |
14968 KB |
Output is correct |
3 |
Correct |
264 ms |
61256 KB |
Output is correct |
4 |
Correct |
75 ms |
32696 KB |
Output is correct |
5 |
Correct |
199 ms |
48068 KB |
Output is correct |
6 |
Correct |
1041 ms |
111040 KB |
Output is correct |
7 |
Correct |
16 ms |
32128 KB |
Output is correct |
8 |
Correct |
15 ms |
30824 KB |
Output is correct |
9 |
Correct |
3 ms |
756 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
15 ms |
31564 KB |
Output is correct |
12 |
Correct |
1 ms |
1612 KB |
Output is correct |
13 |
Correct |
60 ms |
15016 KB |
Output is correct |
14 |
Correct |
34 ms |
10356 KB |
Output is correct |
15 |
Correct |
22 ms |
13108 KB |
Output is correct |
16 |
Correct |
28 ms |
5704 KB |
Output is correct |
17 |
Correct |
155 ms |
28588 KB |
Output is correct |
18 |
Correct |
87 ms |
35396 KB |
Output is correct |
19 |
Correct |
73 ms |
32640 KB |
Output is correct |
20 |
Correct |
67 ms |
25588 KB |
Output is correct |
21 |
Correct |
161 ms |
46904 KB |
Output is correct |
22 |
Correct |
209 ms |
48196 KB |
Output is correct |
23 |
Correct |
291 ms |
39832 KB |
Output is correct |
24 |
Correct |
144 ms |
44420 KB |
Output is correct |
25 |
Correct |
428 ms |
82208 KB |
Output is correct |
26 |
Correct |
425 ms |
72988 KB |
Output is correct |
27 |
Correct |
597 ms |
86332 KB |
Output is correct |
28 |
Correct |
1056 ms |
111500 KB |
Output is correct |
29 |
Correct |
981 ms |
108184 KB |
Output is correct |
30 |
Correct |
828 ms |
105960 KB |
Output is correct |
31 |
Correct |
1008 ms |
68880 KB |
Output is correct |
32 |
Correct |
517 ms |
86624 KB |
Output is correct |