-
Notifications
You must be signed in to change notification settings - Fork 0
/
2003_S3.java
117 lines (106 loc) · 3.02 KB
/
2003_S3.java
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//Graph theory (BFS)
import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Main {
static StringTokenizer st;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static String next () throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine().trim());
return st.nextToken();
}
static int readInt () throws IOException {
return Integer.parseInt(next());
}
public static void main(String[] args) throws IOException{
int numOfWood = readInt();
int rows = readInt();
int cols = readInt();
ArrayList<Integer>[][] adj = new ArrayList[rows][cols];
int roomInstalled = 0;
int totalRoomMeter = 0;
char[][] array = new char[rows][cols];
String[] inputStrings = new String[rows];
for (int i = 0; i < array.length; i++) {
String str = next();
inputStrings[i] = str;
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = inputStrings[i].charAt(j);
if (array[i][j] == '.') {
totalRoomMeter++;
}
adj[i][j] = new ArrayList();
}
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if (array[i][j] == '.') {
if (j != 0) {
if (array[i][j-1] == '.') {
adj[i][j].add(i); adj[i][j].add(j-1);
}
}
if (j != array[i].length - 1) {
if (array[i][j+1] == '.') {
adj[i][j].add(i); adj[i][j].add(j+1);
}
}
if (i != 0) {
if (array[i-1][j] == '.') {
adj[i][j].add(i-1); adj[i][j].add(j);
}
}
if (i != array.length - 1) {
if (array[i+1][j] == '.') {
adj[i][j].add(i+1); adj[i][j].add(j);
}
}
}
}
}
ArrayList<Integer> roomSize = new ArrayList<>();
int tempTotalRoomMeter = 0;
int size = 0;
for (int i = 0; i < adj.length && tempTotalRoomMeter < totalRoomMeter; i++) {
for (int j = 0; j < adj[i].length && tempTotalRoomMeter < totalRoomMeter; j++) {
if(array[i][j] == '.') {
if (adj[i][j].isEmpty()) {roomSize.add(1); continue;}
Queue<ArrayList<Integer>> q = new LinkedList();
q.add(adj[i][j]);
while (!q.isEmpty()) {
ArrayList<Integer> list = q.poll();
for (int k = 0; k < list.size() - 1; k+=2) {
int x = list.get(k), y = list.get(k+1);
if (array[x][y] == '.') {
size++;
array[x][y] = 'I';
q.add(adj[x][y]);
}
}
}
roomSize.add(size);
tempTotalRoomMeter += size;
size = 0;
}
}
}
Collections.sort(roomSize);
for (int i = roomSize.size() - 1; i >= 0; i--) {
int curSize = roomSize.get(i);
if (numOfWood - curSize >= 0) {
numOfWood -= curSize;
roomInstalled++;
}else {
break;
}
}
if (roomInstalled != 1) {
System.out.println(roomInstalled +" rooms, " + numOfWood + " square metre(s) left over");
}else {
System.out.println(roomInstalled +" room, " + numOfWood + " square metre(s) left over");
}
}
}