-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmazonQ2.java
More file actions
121 lines (101 loc) · 3.77 KB
/
AmazonQ2.java
File metadata and controls
121 lines (101 loc) · 3.77 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
import java.util.*;
public class AmazonQ2 {
private static void printGrid(int[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
System.out.print(array[i][j]);
if (j != array[0].length - 1) {
System.out.print(",");
}
}
System.out.print('\n');
}
}
private static void breadthSearchFirst (List<List<Integer>> grid) {
int rows = grid.size();
int cols = grid.get(0).size();
// Create visited
int array[][] = new int [rows][cols];
// Initiate list of servers that have files
Queue<Queue<Position>> queue = new LinkedList<>();
Queue<Position> innerQ = new LinkedList<Position>();
queue.add(innerQ);
int y = 0;
int x = 0;
for (List<Integer> row : grid) {
for (int i = 0; i < row.size(); i++) {
int value = row.get(i);
array[y][x] = 0;
if (value == 1) {
innerQ.add(new Position(x, y));
}
x++;
}
y++;
x = 0;
}
printGrid(array);
int totalCount = 0;
int roundCount = 0;
// Breadth deep search
while (!queue.isEmpty()) {
Queue<Position> innerQ_ = queue.poll();
Queue<Position> newQ = new LinkedList<>();
while (!innerQ_.isEmpty()) {
Position pos = innerQ_.poll();
if (array[pos.y][pos.x] == 1) {
continue;
}
// Mark as visited
array[pos.y][pos.x] = 1;
// Print current state
System.out.println("-----");
System.out.println("Exam: pos.x: " + pos.x + ", pos.y: " + pos.y);
printGrid(array);
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if ((i == 0 && j == 0) || (i != 0 && j != 0)) {
continue;
}
int realx = pos.x + i;
int realy = pos.y + j;
if (realx < 0 || realx >= cols) {
continue;
}
if (realy < 0 || realy >= rows) {
continue;
}
if (array[realy][realx] == 0) {
newQ.add(new Position(realx, realy));
System.out.println("Added: pos.x: " + realx + ", pos.y: " + realy);
}
}
}
totalCount++;
}
if (!newQ.isEmpty()) {
queue.add(newQ);
}
roundCount++;
}
System.out.println("Total count: " + totalCount + ", minimum hours: " + roundCount);
}
public void run() {
System.out.println("Hello World!");
List<List<Integer>> grid = new ArrayList<>();
grid.add(new ArrayList<Integer>(Arrays.asList(0, 1, 0, 0, 0, 0, 0, 0, 0, 0)));
grid.add(new ArrayList<Integer>(Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)));
grid.add(new ArrayList<Integer>(Arrays.asList(0, 0, 0, 0, 1, 0, 0, 0, 0, 0)));
grid.add(new ArrayList<Integer>(Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 1, 0)));
grid.add(new ArrayList<Integer>(Arrays.asList(0, 0, 0, 1, 0, 0, 0, 0, 0, 0)));
breadthSearchFirst(grid);
}
private static class Position {
public int x;
public int y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
};
}