#include <bits/stdc++.h>
#define vi vector<int>
#define ve vector
#define ll long long
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define onbit __builtin_popcount
#define ii pair<int,int>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 1e18
#define eps 1e-7
#define eps1 1e-2
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
using namespace std;
const int MOD = 1e9+7;
const int nax = 1e6+2;
const int MAX_VAL = 1e6;
double PI=3.14159265359;
int arx[8]={1,1,0,-1,-1,-1, 0, 1};
int ary[8]={0,1,1, 1, 0,-1,-1,-1};
typedef complex<int> Point;
#define X real()
#define Y imag()
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
int n,m;
char tab[4005][4005];
bool inside(int x,int y)
{
return (x>-1&&x<n&&y<m&&y>-1&&tab[x][y]!='.');
}
int main(){
optimise;
/*#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif*/
//setIO("threesum");
cin>>n>>m;
int ans=1;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
cin>>tab[i][j];
}
}
deque <pair<int,int>> dp;
dp.push_front({0,0});
int depth[n][m];
memset(depth,0,sizeof depth);
depth[0][0]=1;
while (!dp.empty())
{
int x=dp.front().fi;
int y=dp.front().se;
dp.pop_front();
for (int i = 0; i < 7; i+=2)
{
int a=x+arx[i],b=y+ary[i];
//cout <<" "<<x<<" "<<y<<endl;
if (inside(a,b)&&depth[a][b]==0)
{
if (tab[x][y]==tab[a][b])
{
depth[a][b]=depth[x][y];
dp.push_front({a,b});
}else{
depth[a][b]=depth[x][y]+1;
ans=max(depth[a][b],ans);
dp.push_back({a,b});
//cout << depth[a][b]<<" "<<depth[x][y]<<endl;
}
}
}//cout << dp.size()<<endl;
}
cout << ans<<endl;
}
Compilation message
tracks.cpp: In function 'void setIO(std::string)':
tracks.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
3456 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
7 ms |
3152 KB |
Output is correct |
5 |
Correct |
3 ms |
1748 KB |
Output is correct |
6 |
Correct |
0 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
3 ms |
1620 KB |
Output is correct |
11 |
Correct |
3 ms |
1364 KB |
Output is correct |
12 |
Correct |
5 ms |
1876 KB |
Output is correct |
13 |
Correct |
3 ms |
1748 KB |
Output is correct |
14 |
Correct |
3 ms |
1804 KB |
Output is correct |
15 |
Correct |
10 ms |
3412 KB |
Output is correct |
16 |
Correct |
12 ms |
3412 KB |
Output is correct |
17 |
Correct |
9 ms |
3316 KB |
Output is correct |
18 |
Correct |
7 ms |
3156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15444 KB |
Output is correct |
2 |
Correct |
48 ms |
12748 KB |
Output is correct |
3 |
Correct |
327 ms |
94244 KB |
Output is correct |
4 |
Correct |
89 ms |
26032 KB |
Output is correct |
5 |
Correct |
197 ms |
55968 KB |
Output is correct |
6 |
Correct |
691 ms |
108352 KB |
Output is correct |
7 |
Correct |
8 ms |
16212 KB |
Output is correct |
8 |
Correct |
9 ms |
15444 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
7 ms |
15924 KB |
Output is correct |
12 |
Correct |
2 ms |
980 KB |
Output is correct |
13 |
Correct |
46 ms |
12860 KB |
Output is correct |
14 |
Correct |
27 ms |
8300 KB |
Output is correct |
15 |
Correct |
28 ms |
9048 KB |
Output is correct |
16 |
Correct |
21 ms |
4924 KB |
Output is correct |
17 |
Correct |
120 ms |
28024 KB |
Output is correct |
18 |
Correct |
96 ms |
27648 KB |
Output is correct |
19 |
Correct |
88 ms |
25984 KB |
Output is correct |
20 |
Correct |
73 ms |
23944 KB |
Output is correct |
21 |
Correct |
190 ms |
58012 KB |
Output is correct |
22 |
Correct |
198 ms |
56140 KB |
Output is correct |
23 |
Correct |
228 ms |
48016 KB |
Output is correct |
24 |
Correct |
185 ms |
56908 KB |
Output is correct |
25 |
Correct |
447 ms |
94232 KB |
Output is correct |
26 |
Correct |
447 ms |
124136 KB |
Output is correct |
27 |
Correct |
591 ms |
128772 KB |
Output is correct |
28 |
Correct |
682 ms |
108204 KB |
Output is correct |
29 |
Correct |
689 ms |
105836 KB |
Output is correct |
30 |
Correct |
662 ms |
111588 KB |
Output is correct |
31 |
Correct |
472 ms |
63416 KB |
Output is correct |
32 |
Correct |
558 ms |
118624 KB |
Output is correct |