/
RevolvingDoors.java
192 lines (175 loc) · 6.47 KB
/
RevolvingDoors.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package srm223.divsion1;
import java.util.*;
/**
* Created with IntelliJ IDEA.
* User: emmanuel
* Date: 10/23/13
* Time: 12:42 AM
*/
public class RevolvingDoors {
private int getLinearIndex(char[][] map, int i, int j) {
return i * map[0].length + j;
}
private Position get2dPosition(char[][] map, int linearIndex) {
int i = linearIndex / map[0].length;
int j = linearIndex - (i * map[0].length);
return new Position(i, j);
}
private int findChar(char[][] map, char value) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
if (map[i][j] == value)
return getLinearIndex(map, i, j);
}
}
return -1;
}
private boolean isIndexValid(char[][] map, int i, int j) {
return i >= 0 && j >= 0 && i < map.length && j < map[0].length;
}
private boolean isDoor(char[][] map, int i, int j) {
return isIndexValid(map, i, j) && map[i][j] == 'O';
}
private boolean canRevolveDoor(char[][] map, int linearIndex) {
Position pos = get2dPosition(map, linearIndex);
return isDoor(map, pos.i - 1, pos.j - 1) || isDoor(map, pos.i + 1, pos.j - 1) || isDoor(map, pos.i - 1, pos.j + 1) || isDoor(map, pos.i + 1, pos.j + 1);
}
private boolean canVisit(char[][] map, int i, int j) {
return isIndexValid(map, i, j) && (map[i][j] == ' ' || map[i][j] == 'S' || map[i][j] == 'E');
}
private List<Integer> getAdjacentNodesToVisit(char[][] map, int element) {
Position pos = get2dPosition(map, element);
List<Integer> adj = new LinkedList<Integer>();
if (canVisit(map, pos.i - 1, pos.j))
adj.add(getLinearIndex(map, pos.i - 1, pos.j));
if (canVisit(map, pos.i, pos.j - 1))
adj.add(getLinearIndex(map, pos.i, pos.j - 1));
if (canVisit(map, pos.i, pos.j + 1))
adj.add(getLinearIndex(map, pos.i, pos.j + 1));
if (canVisit(map, pos.i + 1, pos.j))
adj.add(getLinearIndex(map, pos.i + 1, pos.j));
return adj;
}
private boolean bfs(char[][] map, Queue<Integer> doorQueue, Set<Integer> visitedDoors, int start, int end) {
Queue<Integer> queue = new LinkedList<Integer>();
Set<Integer> visited = new HashSet<Integer>();
queue.add(start);
visited.add(start);
while (!queue.isEmpty()) {
Integer curElement = queue.remove();
if (curElement == end) {
return true;
}
if (!visitedDoors.contains(curElement) && canRevolveDoor(map, curElement)) {
doorQueue.add(curElement);
visitedDoors.add(curElement);
}
List<Integer> adjList = getAdjacentNodesToVisit(map, curElement);
for (Integer adj : adjList)
{
if (!visited.contains(adj))
{
visited.add(adj);
queue.add(adj);
}
}
}
return false;
}
private char[][] getCharMap(String[] map)
{
char[][] charMap = new char[map.length][];
for (int i = 0;i<map.length;i++)
{
charMap[i] = new char[map[0].length()];
for (int j=0;j<map[0].length();j++)
{
charMap[i][j] = map[i].charAt(j);
}
}
return charMap;
}
private List<Position> getDoorPositionsFromDiagonalPosition(char[][] map, Position pos)
{
List<Position> positions = new LinkedList<Position>();
if (isDoor(map, pos.i-1, pos.j-1))
positions.add(new Position(pos.i-1, pos.j-1));
if (isDoor(map, pos.i-1, pos.j+1))
positions.add(new Position(pos.i-1, pos.j+1));
if (isDoor(map, pos.i+1, pos.j-1))
positions.add(new Position(pos.i+1, pos.j-1));
if (isDoor(map, pos.i+1, pos.j+1))
positions.add(new Position(pos.i+1, pos.j+1));
assert !positions.isEmpty(); // Code execution should not reach this place.
return positions;
}
private void turnDoor(char[][] map, Position pos)
{
assert (map[pos.i-1][pos.j] == '|' && map[pos.i+1][pos.j] == '|') || (map[pos.i][pos.j-1] == '-' && map[pos.i][pos.j+1] == '-');
if (map[pos.i-1][pos.j] == '|' && map[pos.i+1][pos.j] == '|')
{
map[pos.i-1][pos.j] = ' ';
map[pos.i+1][pos.j] = ' ';
map[pos.i][pos.j-1] = '-';
map[pos.i][pos.j+1] = '-';
}
else if (map[pos.i][pos.j-1] == '-' && map[pos.i][pos.j+1] == '-')
{
map[pos.i-1][pos.j] = '|';
map[pos.i+1][pos.j] = '|';
map[pos.i][pos.j-1] = ' ';
map[pos.i][pos.j+1] = ' ';
}
else
{
assert false; // Code execution should not reach this place.
}
}
public int turns(String[] map) {
char[][] charMap = getCharMap(map);
final int start = findChar(charMap, 'S');
final int end = findChar(charMap, 'E');
Set<Integer> visitedDoors = new HashSet<Integer>();
Queue<Integer> doorQueue = new LinkedList<Integer>();
Queue<Integer> nextLevelDoorQueue = new LinkedList<Integer>();
int turns = 0;
if (bfs(charMap, doorQueue, visitedDoors, start, end))
return turns;
turns++;
while (!doorQueue.isEmpty())
{
int doorToTurn = doorQueue.remove();
List<Position> doorsToTurn = getDoorPositionsFromDiagonalPosition(charMap, get2dPosition(charMap, doorToTurn));
for (Position pos : doorsToTurn)
{
turnDoor(charMap, pos);
visitedDoors.add(doorToTurn);
if (bfs(charMap, nextLevelDoorQueue, visitedDoors, doorToTurn, end))
return turns;
}
if (doorQueue.isEmpty())
{
turns++;
doorQueue = nextLevelDoorQueue;
nextLevelDoorQueue = new LinkedList<Integer>();
visitedDoors = new HashSet<Integer>();
}
}
return -1;
}
private class Position {
int i;
int j;
private Position(int i, int j) {
this.i = i;
this.j = j;
}
@Override
public String toString() {
return "Position{" +
"i=" + i +
", j=" + j +
'}';
}
}
}