/*
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\\
\\ //
// 271828___182845__904523__53602__ \\
\\ 87___47____13______52____66__24_ //
// 97___75____72______47____09___36 \\
\\ 999595_____74______96____69___67 //
// 62___77____24______07____66__30_ \\
\\ 35___35____47______59____45713__ //
// \\
\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
*/
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <climits>
#include <algorithm>
#include <random>
#include <queue>
#include <deque>
#include <iomanip>
#include <string>
#include <tuple>
#include <bitset>
#include <chrono>
#include <ctime>
#include <fstream>
#include <stack>
#include <cstdio>
#include <functional>
using namespace std;
using LL = long long;
const int N = 1e5 + 5;
const LL mod = 1e9 + 7, inf = 1e18;
vector<int> dx = { 1, 0, -1, 0, 1, 1, -1, -1 };
vector<int> dy = { 0, -1, 0, 1, 1, -1, 1, -1 };
void solve() {
int n, m; cin >> n >> m;
vector<string> v(n);
for(int i = 0; i < n; ++i){
cin >> v[i];
}
vector<vector<int>> vis(n, vector<int> (m));
int ans = 0;
queue<pair<int, int>> q;
q.push({0, 0});
vis[0][0] = 1;
while(q.size()){
queue<pair<int, int>> new_q;
++ans;
int st_i = q.front().first, st_j = q.front().second;
while(q.size()){
int i = q.front().first, j = q.front().second;
q.pop();
for(int k = 0; k < 4; ++k){
int to_i = i + dx[k], to_j = j + dy[k];
if(to_i < 0 || to_j < 0 || to_i == n || to_j == m) {
continue;
}
if(vis[to_i][to_j]){
continue;
}
if(v[to_i][to_j] == v[st_i][st_j]){
q.push({to_i, to_j});
vis[to_i][to_j] = 1;
}
else if(v[to_i][to_j] != '.'){
new_q.push({to_i, to_j});
vis[to_i][to_j] = 1;
}
}
}
q = new_q;
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// int t; cin >> t; while (t--)
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
1740 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
7 ms |
1488 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
716 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
5 ms |
928 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
3 ms |
724 KB |
Output is correct |
15 |
Correct |
11 ms |
1728 KB |
Output is correct |
16 |
Correct |
12 ms |
1620 KB |
Output is correct |
17 |
Correct |
8 ms |
1492 KB |
Output is correct |
18 |
Correct |
10 ms |
1388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
48 ms |
8164 KB |
Output is correct |
3 |
Correct |
234 ms |
80892 KB |
Output is correct |
4 |
Correct |
61 ms |
19028 KB |
Output is correct |
5 |
Correct |
403 ms |
45392 KB |
Output is correct |
6 |
Correct |
1253 ms |
101060 KB |
Output is correct |
7 |
Correct |
2 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
724 KB |
Output is correct |
12 |
Correct |
2 ms |
344 KB |
Output is correct |
13 |
Correct |
48 ms |
8160 KB |
Output is correct |
14 |
Correct |
26 ms |
4884 KB |
Output is correct |
15 |
Correct |
25 ms |
5332 KB |
Output is correct |
16 |
Correct |
22 ms |
3536 KB |
Output is correct |
17 |
Correct |
140 ms |
20636 KB |
Output is correct |
18 |
Correct |
113 ms |
20332 KB |
Output is correct |
19 |
Correct |
58 ms |
19076 KB |
Output is correct |
20 |
Correct |
58 ms |
17532 KB |
Output is correct |
21 |
Correct |
145 ms |
47048 KB |
Output is correct |
22 |
Correct |
378 ms |
45472 KB |
Output is correct |
23 |
Correct |
270 ms |
39212 KB |
Output is correct |
24 |
Correct |
197 ms |
45752 KB |
Output is correct |
25 |
Correct |
483 ms |
80888 KB |
Output is correct |
26 |
Correct |
454 ms |
61904 KB |
Output is correct |
27 |
Correct |
595 ms |
84152 KB |
Output is correct |
28 |
Correct |
1010 ms |
101128 KB |
Output is correct |
29 |
Correct |
934 ms |
97816 KB |
Output is correct |
30 |
Correct |
808 ms |
94120 KB |
Output is correct |
31 |
Correct |
769 ms |
52492 KB |
Output is correct |
32 |
Correct |
590 ms |
82320 KB |
Output is correct |