forked from znannan/itec597-mid-term
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonastery.java
More file actions
151 lines (138 loc) · 5.47 KB
/
Copy pathMonastery.java
File metadata and controls
151 lines (138 loc) · 5.47 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
import java.util.Arrays;
import java.util.HashMap;
public class Monastery {
/**
* 返回修道院内部结构关键信息以及最佳修缮策略
*
* @param rooms 房间布局图,值为上下左右四个方向是否有墙的映射值之和
* @return 修道院内部结构关键信息,累计包含6个元素,具体参见题目说明
*/
public int[] resolve(int[][] rooms) {
int[] result = new int[6];
int rows = rooms.length;
int cols = rooms[0].length;
int numberOfRooms = rows * cols;
WeightedQuickUnionUF unionUF = constructUnionUF(rooms);
result[0] = unionUF.count();
result[1] = numberOfRoomsInMaxRegion(unionUF, numberOfRooms);
//拆除一堵墙之后
int maxCount = 0;
int row = 0;
int col = 0;
int direction = 0;
//从左往右遍历
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int currentRoom = xyTo1D(i, j, cols);
if (j + 1 < cols && Direction.EAST.hasWall(currentRoom)) {
int nextRoom = xyTo1D(i, j + 1, cols);
unionUF.union(currentRoom, nextRoom);
int tmp = numberOfRoomsInMaxRegion(unionUF, numberOfRooms);
if (tmp > maxCount) {
maxCount = tmp;
row = i + 1;
col = j + 1;
direction = 4;
}
unionUF = constructUnionUF(rooms); //restore
}
}
}
//从下往上遍历
for (int j = 0; j < cols; j++) {
for (int i = rows - 1; i >= 0; i--) {
int currentRoom = xyTo1D(i, j, cols);
if (i - 1 >= 0 && Direction.NORTH.hasWall(currentRoom)) {
int nextRoom = xyTo1D(i-1, j, cols);
unionUF.union(currentRoom, nextRoom);
int tmp = numberOfRoomsInMaxRegion(unionUF, numberOfRooms);
if (tmp > maxCount) {
maxCount = tmp;
row = i + 1;
col = j + 1;
direction = 2;
}
unionUF = constructUnionUF(rooms); //restore
}
}
}
result[2] = maxCount;
result[3] = row;
result[4] = col;
result[5]= direction;
return result;
}
private WeightedQuickUnionUF constructUnionUF(int rooms[][]) {
int rows = rooms.length;
int cols = rooms[0].length;
int numberOfRooms = rows * cols;
WeightedQuickUnionUF unionUF = new WeightedQuickUnionUF(numberOfRooms);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int room = rooms[i][j];
int currentRoomOrder = xyTo1D(i, j, cols);
if (!Direction.WEST.hasWall(room) && j - 1 >= 0) {
int order = xyTo1D(i, j - 1, cols);
unionUF.union(currentRoomOrder, order);
}
if (!Direction.NORTH.hasWall(room) && i - 1 >= 0) {
int order = xyTo1D(i-1, j, cols);
unionUF.union(currentRoomOrder, order);
}
if (!Direction.EAST.hasWall(room) && j + 1 < cols) {
int order = xyTo1D(i, j + 1, cols);
unionUF.union(currentRoomOrder, order);
}
if (!Direction.SOUTH.hasWall(room) && i + 1 < rows) {
int order = xyTo1D(i + 1, j, cols);
unionUF.union(currentRoomOrder, order);
}
}
}
return unionUF;
}
private int numberOfRoomsInMaxRegion(WeightedQuickUnionUF unionUF, int numberOfRooms) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < numberOfRooms; i++) {
Integer p = unionUF.find(i);
Integer count = map.get(p);
map.put(p, count == null ? 1 : count + 1);
}
int maxNumberOfRooms = 0;
for (Integer count : map.values()) {
if (count > maxNumberOfRooms) {
maxNumberOfRooms = count;
}
}
return maxNumberOfRooms;
}
private int xyTo1D(int x, int y, int cols) {
return x * cols + y;
}
private enum Direction {
WEST, NORTH, EAST, SOUTH;
public boolean hasWall(int room) {
Integer[] west = {1, 3, 5, 7, 9, 11, 13, 15};
Integer[] north = {2, 3, 6, 7, 10, 11, 14, 15};
Integer[] east = {4, 5, 6, 7, 12, 13, 14, 15};
Integer[] south = {8, 9, 10, 11, 12, 13, 14, 15};
switch (this) {
case WEST: return room % 2 == 1;
case NORTH: return Arrays.asList(north).contains(room);
case EAST: return Arrays.asList(east).contains(room);
case SOUTH: return Arrays.asList(south).contains(room);
default: return true;
}
}
}
public static void main(String[] args) {
Monastery monastery = new Monastery();
int[][] rooms = {{3, 2, 6, 3, 6},
{1, 8, 4, 1, 4},
{13, 7, 13, 9, 4},
{3, 0, 2, 6, 5},
{9, 8, 8, 12, 13}};
System.out.println(Arrays.toString(monastery.resolve(rooms)));
}
}