#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma,popcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
/* DEFINES */
#define S second
#define F first
#define ll long long
#define ull unsigned long long
#define ld long double
#define npos ULLONG_MAX
#define INF LLONG_MAX
#define vv(a) vector<a>
#define pp(a, b) pair<a, b>
#define pq(a) priority_queue<a>
#define qq(a) queue<a>
#define ss(a) set<a>
#define mm(a, b) map<a, b>
#define ump(a, b) unordered_map<a, b>
#define ANDROID \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define elif else if
#define endl "\n"
#define allc(a) begin(a), end(a)
#define all(a) a, a + sizeof(a) / sizeof(a[0])
#define pb push_back
#define logi(a) __lg(a)
#define sqrt(a) sqrtl(a)
#define mpr make_pair
#define ins insert
using namespace std;
using namespace __gnu_pbds;
using namespace __cxx11;
typedef char chr;
typedef basic_string<chr> str;
template <typename T, typename key = less<T>>
using ordered_set = tree<T, null_type, key, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 4005;
ll dist[N][N];
str s[N];
ll n, m;
ll dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
bool can_go(ll x, ll y)
{
return x > 0 and x <= n and y > 0 and y <= m and s[x][y] != '.';
}
void bfs()
{
deque<pp(ll, ll)> q;
for (ll i = 1; i <= n; i++)
for (ll j = 1; j <= m; j++)
dist[i][j] = 1e9;
dist[1][1] = 0;
q.pb(mpr(1, 1));
while (!q.empty())
{
auto [x, y] = q.front();
q.pop_front();
for (ll k = 0; k < 4; k++)
if (can_go(x + dx[k], y + dy[k]) and dist[x + dx[k]][y + dy[k]] > dist[x][y] + (s[x][y] != s[x + dx[k]][y + dy[k]]))
{
dist[x + dx[k]][y + dy[k]] = dist[x][y] + (s[x][y] != s[x + dx[k]][y + dy[k]]);
if (s[x][y] != s[x + dx[k]][y + dy[k]])
q.pb(mpr(x + dx[k], y + dy[k]));
else
q.push_front(mpr(x + dx[k], y + dy[k]));
}
}
}
void solve()
{
cin >> n >> m;
for (ll i = 1; i <= n; i++)
cin >> s[i], s[i] = ' ' + s[i];
bfs();
ll ans = 0;
for (ll i = 1; i <= n; i++)
for (ll j = 1; j <= m; j++)
if (s[i][j] != '.')
ans = max(ans, dist[i][j]);
cout << ans + 1 << endl;
}
/*
*/
int main()
{
ANDROID
// precomp();
ll t = 1;
// cin >> t;
for (ll i = 1; i <= t; i++)
// cout << "Case #" << i << ": ",
solve();
cerr << "\nTime elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
17500 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2640 KB |
Output is correct |
4 |
Correct |
6 ms |
17628 KB |
Output is correct |
5 |
Correct |
3 ms |
10844 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
3 ms |
8800 KB |
Output is correct |
11 |
Correct |
3 ms |
8796 KB |
Output is correct |
12 |
Correct |
5 ms |
11100 KB |
Output is correct |
13 |
Correct |
3 ms |
10844 KB |
Output is correct |
14 |
Correct |
3 ms |
10940 KB |
Output is correct |
15 |
Correct |
10 ms |
17500 KB |
Output is correct |
16 |
Correct |
11 ms |
17644 KB |
Output is correct |
17 |
Correct |
8 ms |
17508 KB |
Output is correct |
18 |
Correct |
7 ms |
17748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
121692 KB |
Output is correct |
2 |
Correct |
35 ms |
44788 KB |
Output is correct |
3 |
Correct |
151 ms |
157316 KB |
Output is correct |
4 |
Correct |
54 ms |
67704 KB |
Output is correct |
5 |
Correct |
102 ms |
112456 KB |
Output is correct |
6 |
Correct |
536 ms |
184504 KB |
Output is correct |
7 |
Correct |
16 ms |
125788 KB |
Output is correct |
8 |
Correct |
13 ms |
121716 KB |
Output is correct |
9 |
Correct |
2 ms |
2744 KB |
Output is correct |
10 |
Correct |
1 ms |
2660 KB |
Output is correct |
11 |
Correct |
14 ms |
123592 KB |
Output is correct |
12 |
Correct |
2 ms |
6756 KB |
Output is correct |
13 |
Correct |
32 ms |
44772 KB |
Output is correct |
14 |
Correct |
19 ms |
31164 KB |
Output is correct |
15 |
Correct |
15 ms |
35440 KB |
Output is correct |
16 |
Correct |
15 ms |
16300 KB |
Output is correct |
17 |
Correct |
79 ms |
72268 KB |
Output is correct |
18 |
Correct |
55 ms |
72100 KB |
Output is correct |
19 |
Correct |
58 ms |
67668 KB |
Output is correct |
20 |
Correct |
39 ms |
62812 KB |
Output is correct |
21 |
Correct |
98 ms |
117396 KB |
Output is correct |
22 |
Correct |
100 ms |
112700 KB |
Output is correct |
23 |
Correct |
145 ms |
95932 KB |
Output is correct |
24 |
Correct |
91 ms |
116932 KB |
Output is correct |
25 |
Correct |
378 ms |
157364 KB |
Output is correct |
26 |
Correct |
301 ms |
236392 KB |
Output is correct |
27 |
Correct |
401 ms |
210568 KB |
Output is correct |
28 |
Correct |
558 ms |
184528 KB |
Output is correct |
29 |
Correct |
531 ms |
181196 KB |
Output is correct |
30 |
Correct |
470 ms |
190188 KB |
Output is correct |
31 |
Correct |
362 ms |
122112 KB |
Output is correct |
32 |
Correct |
383 ms |
187904 KB |
Output is correct |