#include <bits/stdc++.h>
#define forw(i,a,b) for (int i = (a); i <= (b); i++)
#define ford(i,a,b) for (int i = (a); i >= (b); i--)
#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define ff first
#define ss second
#define ALL(v) (v).begin(), (v).end()
#define nl '\n'
using namespace std;
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
/* AHIHI */
#define MAX 4010
typedef long long ll;
typedef pair<int, int> pii;
const int dx[] = {-1,1,0,0}, dy[] = {0,0,-1,1};
int n,m,res,d[MAX][MAX];
char a[MAX][MAX];
bool ins(int u,int v)
{
return min(u,v) >= 1 && u <= n && v <= m;
}
void bfs()
{
deque<pii> q;
q.push_back({1,1});
d[1][1] = 1;
while (q.size()) {
pii c = q.front(); q.pop_front();
maximize(res, d[c.ff][c.ss]);
rep(i,0,4) {
int u = c.ff + dx[i], v = c.ss + dy[i];
if (ins(u,v) && d[u][v] == 0 && a[u][v]!='.') {
if (a[u][v] == a[c.ff][c.ss]) {
d[u][v] = d[c.ff][c.ss];
q.push_front({u,v});
}
else {
d[u][v] = d[c.ff][c.ss] + 1;
q.push_back({u,v});
}
}
}
}
}
void solve(int tc)
{
cin >> n >> m;
forw(i,1,n) forw(j,1,m) cin >> a[i][j];
bfs();
cout << res << nl;
}
signed main()
{
ios::sync_with_stdio(false); cin.tie(0);
// string name = "HT";
// freopen((name + ".inp").c_str(),"r",stdin);
// freopen((name + ".out").c_str(),"w",stdout);
int T = 1;
// cin >> T;
forw(i,1,T) solve(i);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
5452 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
10 ms |
5196 KB |
Output is correct |
5 |
Correct |
4 ms |
2892 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
836 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
4 ms |
2380 KB |
Output is correct |
11 |
Correct |
4 ms |
2124 KB |
Output is correct |
12 |
Correct |
7 ms |
3020 KB |
Output is correct |
13 |
Correct |
4 ms |
2892 KB |
Output is correct |
14 |
Correct |
4 ms |
2892 KB |
Output is correct |
15 |
Correct |
14 ms |
5240 KB |
Output is correct |
16 |
Correct |
15 ms |
5460 KB |
Output is correct |
17 |
Correct |
14 ms |
5196 KB |
Output is correct |
18 |
Correct |
10 ms |
5164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
30796 KB |
Output is correct |
2 |
Correct |
64 ms |
15056 KB |
Output is correct |
3 |
Correct |
424 ms |
73260 KB |
Output is correct |
4 |
Correct |
117 ms |
32604 KB |
Output is correct |
5 |
Correct |
291 ms |
53416 KB |
Output is correct |
6 |
Correct |
803 ms |
107476 KB |
Output is correct |
7 |
Correct |
17 ms |
32216 KB |
Output is correct |
8 |
Correct |
16 ms |
30792 KB |
Output is correct |
9 |
Correct |
3 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
456 KB |
Output is correct |
11 |
Correct |
16 ms |
31616 KB |
Output is correct |
12 |
Correct |
2 ms |
1612 KB |
Output is correct |
13 |
Correct |
66 ms |
15012 KB |
Output is correct |
14 |
Correct |
39 ms |
10376 KB |
Output is correct |
15 |
Correct |
44 ms |
13108 KB |
Output is correct |
16 |
Correct |
28 ms |
5728 KB |
Output is correct |
17 |
Correct |
164 ms |
28672 KB |
Output is correct |
18 |
Correct |
143 ms |
35600 KB |
Output is correct |
19 |
Correct |
117 ms |
32636 KB |
Output is correct |
20 |
Correct |
96 ms |
25520 KB |
Output is correct |
21 |
Correct |
246 ms |
52528 KB |
Output is correct |
22 |
Correct |
284 ms |
53400 KB |
Output is correct |
23 |
Correct |
309 ms |
43644 KB |
Output is correct |
24 |
Correct |
248 ms |
49792 KB |
Output is correct |
25 |
Correct |
651 ms |
94400 KB |
Output is correct |
26 |
Correct |
529 ms |
131048 KB |
Output is correct |
27 |
Correct |
675 ms |
111956 KB |
Output is correct |
28 |
Correct |
792 ms |
107792 KB |
Output is correct |
29 |
Correct |
791 ms |
107876 KB |
Output is correct |
30 |
Correct |
745 ms |
107084 KB |
Output is correct |
31 |
Correct |
629 ms |
73636 KB |
Output is correct |
32 |
Correct |
614 ms |
109092 KB |
Output is correct |