#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define fi first
#define se second
pii dir[4]={{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
vector<pii > vec, tmp;
char a[4003][4003];
int n, m, ans;
bool ok(pii u)
{
return (u.fi && u.fi<=n && u.se && u.se<=m && a[u.fi][u.se]!='.');
}
void dfs(pii u)
{
char x=a[u.fi][u.se];
a[u.fi][u.se]='.';
for (pii i:dir)
{
pii v=u;
v.fi+=i.fi;
v.se+=i.se;
if (ok(v))
{
if (a[v.fi][v.se]==x)
dfs(v);
else
tmp.push_back(v);
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++)
cin >> a[i][j];
tmp.push_back({1, 1});
while (!tmp.empty())
{
vec=tmp;
tmp.clear();
for (pii u:vec)
dfs(u);
ans++;
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
3148 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
10 ms |
3928 KB |
Output is correct |
5 |
Correct |
4 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
4 ms |
1368 KB |
Output is correct |
11 |
Correct |
4 ms |
1748 KB |
Output is correct |
12 |
Correct |
7 ms |
1876 KB |
Output is correct |
13 |
Correct |
5 ms |
1492 KB |
Output is correct |
14 |
Correct |
4 ms |
1488 KB |
Output is correct |
15 |
Correct |
20 ms |
2772 KB |
Output is correct |
16 |
Correct |
18 ms |
3156 KB |
Output is correct |
17 |
Correct |
11 ms |
2608 KB |
Output is correct |
18 |
Correct |
10 ms |
3876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15608 KB |
Output is correct |
2 |
Correct |
56 ms |
7032 KB |
Output is correct |
3 |
Correct |
433 ms |
32360 KB |
Output is correct |
4 |
Correct |
76 ms |
11764 KB |
Output is correct |
5 |
Correct |
273 ms |
21408 KB |
Output is correct |
6 |
Correct |
866 ms |
112700 KB |
Output is correct |
7 |
Correct |
9 ms |
16340 KB |
Output is correct |
8 |
Correct |
9 ms |
15700 KB |
Output is correct |
9 |
Correct |
3 ms |
852 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
8 ms |
16084 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
51 ms |
6976 KB |
Output is correct |
14 |
Correct |
32 ms |
4976 KB |
Output is correct |
15 |
Correct |
24 ms |
5336 KB |
Output is correct |
16 |
Correct |
28 ms |
2760 KB |
Output is correct |
17 |
Correct |
151 ms |
12652 KB |
Output is correct |
18 |
Correct |
115 ms |
12368 KB |
Output is correct |
19 |
Correct |
72 ms |
11664 KB |
Output is correct |
20 |
Correct |
79 ms |
10824 KB |
Output is correct |
21 |
Correct |
207 ms |
22140 KB |
Output is correct |
22 |
Correct |
245 ms |
21416 KB |
Output is correct |
23 |
Correct |
246 ms |
18248 KB |
Output is correct |
24 |
Correct |
197 ms |
21956 KB |
Output is correct |
25 |
Correct |
448 ms |
32356 KB |
Output is correct |
26 |
Runtime error |
894 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Correct |
906 ms |
299240 KB |
Output is correct |
28 |
Correct |
824 ms |
112832 KB |
Output is correct |
29 |
Correct |
757 ms |
99576 KB |
Output is correct |
30 |
Correct |
769 ms |
187400 KB |
Output is correct |
31 |
Correct |
729 ms |
26764 KB |
Output is correct |
32 |
Correct |
720 ms |
333348 KB |
Output is correct |