Submission #41031


ソースコード

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(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:16:41
言語 Java
状態 Accepted
得点 10
ソースコード長 1571 Byte
最大実行時間 1177 ms
最大メモリ使用量 37288 KB

セット

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

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
input-sample1 AC 125 ms 15332 KB
1
2
input-sample2 AC 126 ms 15364 KB
1
2
input01-easy AC 149 ms 15636 KB
1
2
input02-easy AC 142 ms 15584 KB
1
2
input03-easy AC 137 ms 15644 KB
1
2
input04 AC 972 ms 25576 KB
2
input05 AC 676 ms 26368 KB
2
input06 AC 930 ms 25116 KB
2
input07 AC 1059 ms 26180 KB
2
input08 AC 1176 ms 31020 KB
2
input09 AC 1115 ms 34232 KB
2
input10 AC 1087 ms 33284 KB
2
input11 AC 1177 ms 37288 KB
2
input12 AC 1078 ms 37228 KB
2
input13 AC 124 ms 26660 KB
2
input14 AC 126 ms 26548 KB
2