Submission #52727


ソースコード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <bits/stdc++.h>
using namespace std;
struct UnionFind{
vector<int> tree;
UnionFind(int n): tree(n+1, -1) {};
int root(int i){ return tree[i] < 0 ? i : tree[i] = root(tree[i]); }
bool unite(int i, int j){
i = root(i), j = root(j);
if(i == j) return false;
tree[i] += tree[j], tree[j] = i;
return true;
}
bool same(int i, int j){ return root(i) == root(j); }
int size(int i){ return -tree[root(i)]; }
};
char mas[2010][2010];
int masidx[2010][2010];
int h, w, q;
bool isOK(int i, int j){
return 0 <= i && i < h && 0 <= j && j < w;
}
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> h >> w >> q;
for(int i = 0;i < h;i++){
cin >> mas[i];
for(int j = 0;j < w;j++) masidx[i][j] = i*w+j;
}
UnionFind uf(h*w);
for(int i = 0;i < h;i++){
for(int j = 0;j < w;j++){
if(mas[i][j] == '#'){
for(int k = 0;k < 4;k++){
int xx = j+dx[k], yy = i+dy[k];
if(isOK(yy, xx) && mas[yy][xx] == '#') uf.unite(masidx[i][j], masidx[yy][xx]);
}
}
}
}
int qu, a, b;
for(int p = 0;p < q;p++){
cin >> qu >> a >> b;
a--, b--;
if(qu == 0){
mas[a][b] = '#';
for(int k = 0;k < 4;k++){
int xx = b+dx[k], yy = a+dy[k];
if(isOK(yy, xx) && mas[yy][xx] == '#') uf.unite(masidx[a][b], masidx[yy][xx]);
}
}else{
cout << (mas[a][b] == '#' ? uf.size(masidx[a][b]) : 0) << endl;
}
}
return 0;
}

ステータス

項目 データ
問題 0971 - 島の創造
ユーザー名 ei1719
投稿日時 2019-08-11 17:33:28
言語 C++14
状態 Accepted
得点 10
ソースコード長 1772 Byte
最大実行時間 1112 ms
最大メモリ使用量 27844 KB

セット

セット 得点 Cases
1 Easy 1 / 1 *easy, *sample*
2 ALL 9 / 9 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
input-sample1 AC 25 ms 2524 KB
1
2
input-sample2 AC 24 ms 2600 KB
1
2
input01-easy AC 20 ms 3036 KB
1
2
input02-easy AC 22 ms 3072 KB
1
2
input03-easy AC 24 ms 3316 KB
1
2
input04 AC 540 ms 17188 KB
2
input05 AC 359 ms 17448 KB
2
input06 AC 533 ms 17996 KB
2
input07 AC 588 ms 18996 KB
2
input08 AC 630 ms 21364 KB
2
input09 AC 679 ms 22796 KB
2
input10 AC 654 ms 23456 KB
2
input11 AC 1112 ms 27440 KB
2
input12 AC 579 ms 27844 KB
2
input13 AC 21 ms 13656 KB
2
input14 AC 24 ms 13872 KB
2