//
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef long long ll;
typedef long double ld;
const ll infLL = 2e18 + 7;
const int inf = 2e9 + 7;
const int maxN = 4010;
const ll MOD = 998244353;
const double eps = 1e-12;
int d[maxN][maxN];
int a[maxN][maxN];
bool visited[maxN][maxN];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int res = 0;
bool check(int x, int y, int n, int m)
{
return (0 <= x && x < n && 0 <= y && y < m);
}
void bfs(int n, int m)
{
deque<ii> q; q.push_front(ii(0, 0));
while (!q.empty())
{
int ux = q.front().first;
int uy = q.front().second;
q.pop_front();
if (visited[ux][uy]) continue;
visited[ux][uy] = true;
res = max(res, d[ux][uy]);
for (int k = 0; k < 4; ++k)
{
int vx = ux + dx[k];
int vy = uy + dy[k];
if (!check(vx, vy, n, m) || visited[vx][vy] || a[vx][vy] == 0) continue;
if (a[vx][vy] == a[ux][uy])
{
d[vx][vy] = d[ux][uy];
q.push_front(ii(vx, vy));
}
else
{
d[vx][vy] = d[ux][uy] + 1;
q.push_back(ii(vx, vy));
}
}
}
}
void solve()
{
memset(visited, false, sizeof visited);
memset(d, 0, sizeof d);
memset(a, 0, sizeof a);
int n, m; cin >> n >> m;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
char c; cin >> c;
if (c == 'F') a[i][j] = 1;
else if (c == 'R') a[i][j] = 2;
else a[i][j] = 0;
}
}
bfs(n, m);
cout << res + 1 << '\n';
}
void setIO(string name)
{
ios_base::sync_with_stdio(0);
cin.tie(NULL); cout.tie(NULL);
if (!name.size()) return;
#ifndef ONLINE_JUDGE
freopen((name + ".inp").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
#endif
}
int main()
{
setIO("");
int test = 1; //cin >> test;
while (test--) solve();
}
// _ _ _
// | | | | | |
// __| |_ _| |_ __| |
// / _` \ \ / / __/ _` |
// | (_| |\ V /| || (_| |
// \__,_| \_/ \__\__,_|
//
//
Compilation message
tracks.cpp: In function 'void setIO(std::string)':
tracks.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen((name + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
142140 KB |
Output is correct |
2 |
Correct |
46 ms |
141872 KB |
Output is correct |
3 |
Correct |
46 ms |
141912 KB |
Output is correct |
4 |
Correct |
53 ms |
142372 KB |
Output is correct |
5 |
Correct |
49 ms |
141912 KB |
Output is correct |
6 |
Correct |
45 ms |
141812 KB |
Output is correct |
7 |
Correct |
45 ms |
141904 KB |
Output is correct |
8 |
Correct |
46 ms |
141860 KB |
Output is correct |
9 |
Correct |
46 ms |
141924 KB |
Output is correct |
10 |
Correct |
47 ms |
141896 KB |
Output is correct |
11 |
Correct |
48 ms |
142020 KB |
Output is correct |
12 |
Correct |
52 ms |
141988 KB |
Output is correct |
13 |
Correct |
47 ms |
141916 KB |
Output is correct |
14 |
Correct |
48 ms |
141900 KB |
Output is correct |
15 |
Correct |
58 ms |
142128 KB |
Output is correct |
16 |
Correct |
61 ms |
142180 KB |
Output is correct |
17 |
Correct |
56 ms |
142140 KB |
Output is correct |
18 |
Correct |
53 ms |
142412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
141924 KB |
Output is correct |
2 |
Correct |
97 ms |
143436 KB |
Output is correct |
3 |
Correct |
364 ms |
157552 KB |
Output is correct |
4 |
Correct |
150 ms |
145488 KB |
Output is correct |
5 |
Correct |
181 ms |
150676 KB |
Output is correct |
6 |
Correct |
870 ms |
182464 KB |
Output is correct |
7 |
Correct |
53 ms |
141924 KB |
Output is correct |
8 |
Correct |
56 ms |
141856 KB |
Output is correct |
9 |
Correct |
49 ms |
141900 KB |
Output is correct |
10 |
Correct |
48 ms |
141896 KB |
Output is correct |
11 |
Correct |
47 ms |
141944 KB |
Output is correct |
12 |
Correct |
46 ms |
141804 KB |
Output is correct |
13 |
Correct |
98 ms |
143396 KB |
Output is correct |
14 |
Correct |
74 ms |
142880 KB |
Output is correct |
15 |
Correct |
70 ms |
142796 KB |
Output is correct |
16 |
Correct |
72 ms |
142552 KB |
Output is correct |
17 |
Correct |
179 ms |
145916 KB |
Output is correct |
18 |
Correct |
142 ms |
145812 KB |
Output is correct |
19 |
Correct |
128 ms |
145468 KB |
Output is correct |
20 |
Correct |
114 ms |
145308 KB |
Output is correct |
21 |
Correct |
245 ms |
150984 KB |
Output is correct |
22 |
Correct |
178 ms |
150652 KB |
Output is correct |
23 |
Correct |
297 ms |
149536 KB |
Output is correct |
24 |
Correct |
212 ms |
150788 KB |
Output is correct |
25 |
Correct |
591 ms |
157504 KB |
Output is correct |
26 |
Correct |
570 ms |
254172 KB |
Output is correct |
27 |
Correct |
703 ms |
183688 KB |
Output is correct |
28 |
Correct |
854 ms |
182464 KB |
Output is correct |
29 |
Correct |
845 ms |
178644 KB |
Output is correct |
30 |
Correct |
801 ms |
186720 KB |
Output is correct |
31 |
Correct |
709 ms |
152856 KB |
Output is correct |
32 |
Correct |
609 ms |
187928 KB |
Output is correct |