Submission #41032


ソースコード

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
77
78
79
80
import java.io.*;
import java.util.*;
public class Main{
static Scanner s=new Scanner(new BufferedInputStream(System.in));
static int gInt() {
return Integer.parseInt(s.next());
}
static int h =gInt();
static int w =gInt();
static int q =gInt();
static char[][] f=new char[h][w];
static UF uf=new UF(h*w);
public static void main(String[] $){
for(int i=0;i<h;++i)
s.next().getChars(0,w,f[i],0);
System.setOut(new PrintStream(new BufferedOutputStream(System.out)));
for(int i=0;i<h;++i)
for(int j=0;j<w;++j)
if(f[i][j]=='#')
island(i,j);
for(;q>0;--q){
int m=gInt();
int i=gInt()-1;
int j=gInt()-1;
if(m==0)
island(i,j);
else
System.out.println(f[i][j]!='#'?0:uf.groupSize(i*w+j));
}
System.out.flush();
}
static int[] dx={0,1,0,-1},dy={1,0,-1,0};
static void island(int i,int j){
f[i][j]='#';
for(int d=0;d<4;++d){
int I=i+dy[d],J=j+dx[d];
if(0<=I&&I<h&&0<=J&&J<w
&&f[I][J]=='#')
uf.connect(i*w+j,I*w+J);
}
}
}
class UF{
public int[] uni;
public UF(int size){
Arrays.fill(uni=new int[size],-1);
}
public int root(int t){
return uni[t]<0?t:(uni[t]=root(uni[t]));
}
/** @return changed */
public boolean connect(int a,int b){
if((a=root(a))==(b=root(b)))
return false;
if(uni[a]>uni[b]){
int buf=b;
b=a;
a=buf;
}
uni[a]+=uni[b];
uni[b]=a;
return true;
}
public boolean connected(int a,int b){
return root(a)==root(b);
}
public int groupSize(int t){
return -uni[root(t)];
}
}

ステータス

項目 データ
問題 0971 - 島の創造
ユーザー名 fal_rnd
投稿日時 2018-08-09 23:18:07
言語 Java
状態 Accepted
得点 10
ソースコード長 1596 Byte
最大実行時間 1175 ms
最大メモリ使用量 37840 KB

セット

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

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
input-sample1 AC 129 ms 15588 KB
1
2
input-sample2 AC 122 ms 15640 KB
1
2
input01-easy AC 127 ms 15624 KB
1
2
input02-easy AC 138 ms 15596 KB
1
2
input03-easy AC 143 ms 15776 KB
1
2
input04 AC 957 ms 27776 KB
2
input05 AC 687 ms 25996 KB
2
input06 AC 920 ms 24884 KB
2
input07 AC 1028 ms 25784 KB
2
input08 AC 1074 ms 31172 KB
2
input09 AC 1118 ms 32168 KB
2
input10 AC 1103 ms 32984 KB
2
input11 AC 1175 ms 37016 KB
2
input12 AC 1054 ms 37840 KB
2
input13 AC 127 ms 26424 KB
2
input14 AC 123 ms 26648 KB
2