#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int h,w;
cin>>h>>w;
string s[h+5];
int used[h+5][w+5];
memset(used,0,sizeof(used));
int cnt=0;
for(int i=0;i<h;i++){
cin>>s[i];
}
set<pair<int,pair<int,int>>>st;
st.insert({1,{0,0}});
used[0][0]=1;
int ans=0;
pair<int,pair<int,int>>p;
while(!st.empty()){
p=*st.begin();
int num=p.ff,x=p.ss.ff,y=p.ss.ss;
ans=max(ans,num);
st.erase(p);
if(x>0&&used[x-1][y]==0){
used[x-1][y]=1;
if(s[x-1][y]==s[x][y]){
st.insert({num,{x-1,y}});
}
else if(s[x-1][y]!='.'){
st.insert({num+1,{x-1,y}});
}
}
if(x<h-1&&used[x+1][y]==0){
used[x+1][y]=1;
if(s[x+1][y]==s[x][y]){
st.insert({num,{x+1,y}});
}
else if(s[x+1][y]!='.'){
st.insert({num+1,{x+1,y}});
}
}
if(y>0&&used[x][y-1]==0){
used[x][y-1]=1;
if(s[x][y-1]==s[x][y]){
st.insert({num,{x,y-1}});
}
else if(s[x][y-1]!='.'){
st.insert({num+1,{x,y-1}});
}
}
if(y<w-1&&used[x][y+1]==0){
used[x][y+1]=1;
if(s[x][y+1]==s[x][y]){
st.insert({num,{x,y+1}});
}
else if(s[x][y+1]!='.'){
st.insert({num+1,{x,y+1}});
}
}
}
cout<<ans;
}
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:18:9: warning: unused variable 'cnt' [-Wunused-variable]
18 | int cnt=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
2224 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
34 ms |
2240 KB |
Output is correct |
5 |
Correct |
5 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
6 ms |
724 KB |
Output is correct |
11 |
Correct |
9 ms |
844 KB |
Output is correct |
12 |
Correct |
19 ms |
1108 KB |
Output is correct |
13 |
Correct |
6 ms |
724 KB |
Output is correct |
14 |
Correct |
4 ms |
852 KB |
Output is correct |
15 |
Correct |
39 ms |
2012 KB |
Output is correct |
16 |
Correct |
51 ms |
2132 KB |
Output is correct |
17 |
Correct |
23 ms |
1852 KB |
Output is correct |
18 |
Correct |
33 ms |
2156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
130 ms |
9784 KB |
Output is correct |
3 |
Correct |
487 ms |
96556 KB |
Output is correct |
4 |
Correct |
133 ms |
22776 KB |
Output is correct |
5 |
Correct |
222 ms |
54172 KB |
Output is correct |
6 |
Execution timed out |
2096 ms |
153392 KB |
Time limit exceeded |
7 |
Correct |
2 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
756 KB |
Output is correct |
9 |
Correct |
6 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
130 ms |
9804 KB |
Output is correct |
14 |
Correct |
85 ms |
5836 KB |
Output is correct |
15 |
Correct |
20 ms |
6216 KB |
Output is correct |
16 |
Correct |
67 ms |
4308 KB |
Output is correct |
17 |
Correct |
339 ms |
24736 KB |
Output is correct |
18 |
Correct |
75 ms |
24140 KB |
Output is correct |
19 |
Correct |
129 ms |
22872 KB |
Output is correct |
20 |
Correct |
115 ms |
20904 KB |
Output is correct |
21 |
Correct |
301 ms |
56112 KB |
Output is correct |
22 |
Correct |
224 ms |
54324 KB |
Output is correct |
23 |
Correct |
729 ms |
47056 KB |
Output is correct |
24 |
Correct |
185 ms |
54748 KB |
Output is correct |
25 |
Correct |
399 ms |
96500 KB |
Output is correct |
26 |
Execution timed out |
2009 ms |
73932 KB |
Time limit exceeded |
27 |
Execution timed out |
2033 ms |
98464 KB |
Time limit exceeded |
28 |
Execution timed out |
2040 ms |
142000 KB |
Time limit exceeded |
29 |
Execution timed out |
2045 ms |
149700 KB |
Time limit exceeded |
30 |
Execution timed out |
2077 ms |
136512 KB |
Time limit exceeded |
31 |
Execution timed out |
2067 ms |
54988 KB |
Time limit exceeded |
32 |
Execution timed out |
2085 ms |
86732 KB |
Time limit exceeded |