//made by Nguyễn Hồng Nam
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
const ll mod = 1e9 + 7,inf = 1e18,mxn = 4005;
#define pll pair<ll,ll>
#define fi first
#define se second
deque<pll> q;
ll d[mxn][mxn],h,w,row[4] = {-1,0,0,1},col[4] = {0,-1,1,0},ans = 1;
string s[mxn];
void maxz(ll &a,ll b)
{
if(a < b) a= b;
}
bool check(ll i,ll j)
{
if(i >= 0 && i < h && j >= 0 && j < w && s[i][j] != '.') return true;
return false;
}
void bfs01()
{
d[0][0] = 1;
q.push_back({0,0});
while(!q.empty())
{
pll mx = q.front();
maxz(ans,d[mx.fi][mx.se]);
q.pop_front();
for(ll i = 0; i < 4; i++)
{
ll a = mx.fi + row[i],b = mx.se + col[i];
if(check(a,b) && d[a][b] == 0)
{
if(s[a][b] == s[mx.fi][mx.se])
{
d[a][b] = d[mx.fi][mx.se];
q.push_front({a,b});
}
else
{
d[a][b] = d[mx.fi][mx.se] + 1;
q.push_back({a,b});
}
}
}
}
}
int main()
{
#define taskname "track"
if (fopen(taskname".inp", "r"))
{
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
cin.tie(0)->sync_with_stdio(0);
cin >> h >> w;
for(ll i = 0; i < h; i++)
{
cin >> s[i];
}
bfs01();
cout << ans << '\n';
return 0;
}
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:56:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:57:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
5 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
2392 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
3 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
1884 KB |
Output is correct |
12 |
Correct |
5 ms |
2692 KB |
Output is correct |
13 |
Correct |
2 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
2396 KB |
Output is correct |
15 |
Correct |
8 ms |
4532 KB |
Output is correct |
16 |
Correct |
10 ms |
4956 KB |
Output is correct |
17 |
Correct |
7 ms |
4700 KB |
Output is correct |
18 |
Correct |
6 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16220 KB |
Output is correct |
2 |
Correct |
30 ms |
15156 KB |
Output is correct |
3 |
Correct |
158 ms |
93524 KB |
Output is correct |
4 |
Correct |
55 ms |
43348 KB |
Output is correct |
5 |
Correct |
103 ms |
69256 KB |
Output is correct |
6 |
Correct |
540 ms |
189368 KB |
Output is correct |
7 |
Correct |
8 ms |
16988 KB |
Output is correct |
8 |
Correct |
8 ms |
16220 KB |
Output is correct |
9 |
Correct |
3 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
8 ms |
16476 KB |
Output is correct |
12 |
Correct |
1 ms |
1368 KB |
Output is correct |
13 |
Correct |
33 ms |
15372 KB |
Output is correct |
14 |
Correct |
24 ms |
9812 KB |
Output is correct |
15 |
Correct |
19 ms |
14420 KB |
Output is correct |
16 |
Correct |
14 ms |
6492 KB |
Output is correct |
17 |
Correct |
88 ms |
33140 KB |
Output is correct |
18 |
Correct |
59 ms |
47932 KB |
Output is correct |
19 |
Correct |
54 ms |
43092 KB |
Output is correct |
20 |
Correct |
47 ms |
27388 KB |
Output is correct |
21 |
Correct |
99 ms |
60912 KB |
Output is correct |
22 |
Correct |
111 ms |
69276 KB |
Output is correct |
23 |
Correct |
160 ms |
56264 KB |
Output is correct |
24 |
Correct |
90 ms |
59352 KB |
Output is correct |
25 |
Correct |
377 ms |
159056 KB |
Output is correct |
26 |
Correct |
305 ms |
236024 KB |
Output is correct |
27 |
Correct |
444 ms |
241232 KB |
Output is correct |
28 |
Correct |
548 ms |
189140 KB |
Output is correct |
29 |
Correct |
530 ms |
183360 KB |
Output is correct |
30 |
Correct |
504 ms |
190908 KB |
Output is correct |
31 |
Correct |
434 ms |
115528 KB |
Output is correct |
32 |
Correct |
379 ms |
203260 KB |
Output is correct |