Submission #52728


ソースコード

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
#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(){
scanf("%d %d %d", &h, &w, &q);
for(int i = 0;i < h;i++){
scanf("%s", 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++){
scanf("%d %d %d", &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{
printf("%d\n", (mas[a][b] == '#' ? uf.size(masidx[a][b]) : 0));
}
}
return 0;
}

ステータス

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

セット

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

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
input-sample1 AC 21 ms 2652 KB
1
2
input-sample2 AC 19 ms 2488 KB
1
2
input01-easy AC 22 ms 2944 KB
1
2
input02-easy AC 22 ms 3128 KB
1
2
input03-easy AC 20 ms 3504 KB
1
2
input04 AC 182 ms 17112 KB
2
input05 AC 122 ms 17656 KB
2
input06 AC 179 ms 18108 KB
2
input07 AC 177 ms 19020 KB
2
input08 AC 150 ms 21408 KB
2
input09 AC 237 ms 22864 KB
2
input10 AC 202 ms 23676 KB
2
input11 AC 180 ms 27428 KB
2
input12 AC 153 ms 27856 KB
2
input13 AC 23 ms 13820 KB
2
input14 AC 15 ms 13920 KB
2