-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBacktrackingDFS
More file actions
211 lines (185 loc) · 8.41 KB
/
BacktrackingDFS
File metadata and controls
211 lines (185 loc) · 8.41 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Veronika Levasheva
// ISE-03
import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Math.abs;
public class BacktrackingDFS {
public static void main(String[] args) {
// reading initial data
Scanner scanner = new Scanner(System.in);
String typeOfPerception = scanner.nextLine();
Integer perception = Integer.parseInt(typeOfPerception);
String keyMakerLoc = scanner.nextLine();
String[] keyMx = keyMakerLoc.split(" ");
// reading the coordinates of the Keymaker
Integer xKeyM = Integer.parseInt(keyMx[0]);
Integer yKeyM = Integer.parseInt(keyMx[1]);
// filling in the matrix that will be used to mark the locations of enemies and Keymaker/Key
String[][] M = new String[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
M[i][j] = "";
}
}
int answer;
// accessing the function that will start the algorithm
answer = backtracking(M, xKeyM, yKeyM);
// answer is the map is solvable or otherwise
if (answer != -1) {
System.out.println("e " + answer);
} else {
System.out.println("e -1");
}
}
// the backtracking algorithm
public static int backtracking(String[][] Matrix, Integer xKeyM, Integer yKeyM) {
ArrayList<ArrayList<Integer>> visited = new ArrayList<>();
for (int i = 0; i < 9; i++) {
ArrayList<Integer> ar = new ArrayList<>();
for (int j = 0; j < 9; j++) {
ar.add(0);
}
visited.add(ar);
}
ArrayList<String> currentPath = new ArrayList<>();
currentPath.add("0 0");
Scanner scanner = new Scanner(System.in);
int curLocX = 0;
int curLocY = 0;
Boolean game = true;
// initial step
System.out.println("m " + curLocX + " " + curLocY);
Integer shortestPath = 100000;
// algorithm starts
while (game) {
// reading the received data
Integer numberOfEnemies = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < numberOfEnemies; i++) {
String[] agent = scanner.nextLine().split(" ");
Integer locX = Integer.parseInt(agent[0]);
Integer locY = Integer.parseInt(agent[1]);
String type = agent[2];
Matrix[locX][locY] = type;
if (type.equals("P") || type.equals("A") || type.equals("S")) {
visited.get(locX).set(locY, 1);
}
}
// System.out.println("Visited:");
// for (int i = 0; i < 9; i++) {
// for (int j = 0; j < 9; j++) {
// System.out.print(visited.get(i).get(j) + " ");
// }
// System.out.print("\n");
// }
// System.out.println();
ArrayList<String> validNeighbors = new ArrayList<>();
Integer min = 100000;
Integer indexX = -1;
Integer indexY = -1;
// move down the Matrix
if ((curLocX + 1) < 9 && visited.get(curLocX + 1).get(curLocY) == 0) {
if (!currentPath.contains(Integer.toString(curLocX + 1) + " " + Integer.toString(curLocY))) {
validNeighbors.add(Integer.toString(curLocX + 1) + " " + Integer.toString(curLocY));
Integer distance = abs(xKeyM - (curLocX + 1)) + abs(yKeyM - curLocY);
// checking if this move is the most reasonable
if (distance < min) {
min = distance;
indexX = curLocX + 1;
indexY = curLocY;
}
}
}
// move up the Matrix
if ((curLocX - 1) >= 0 && visited.get(curLocX - 1).get(curLocY) == 0) {
if (!currentPath.contains(Integer.toString(curLocX - 1) + " " + Integer.toString(curLocY))) {
validNeighbors.add(Integer.toString(curLocX - 1) + " " + Integer.toString(curLocY));
Integer distance = abs(xKeyM - (curLocX - 1)) + abs(yKeyM - curLocY);
// checking if this move is the most reasonable
if (distance < min) {
min = distance;
indexX = curLocX - 1;
indexY = curLocY;
}
}
}
// move to the right
if ((curLocY + 1) < 9 && visited.get(curLocX).get(curLocY + 1) == 0) {
if (!currentPath.contains(Integer.toString(curLocX) + " " + Integer.toString(curLocY + 1))) {
validNeighbors.add(Integer.toString(curLocX) + " " + Integer.toString(curLocY + 1));
Integer distance = abs(xKeyM - curLocX) + abs(yKeyM - (curLocY + 1));
// checking if this move is the most reasonable
if (distance < min) {
min = distance;
indexX = curLocX;
indexY = curLocY + 1;
}
}
}
// move to the left
if ((curLocY - 1) >= 0 && visited.get(curLocX).get(curLocY - 1) == 0) {
if (!currentPath.contains(Integer.toString(curLocX) + " " + Integer.toString(curLocY - 1))) {
validNeighbors.add(Integer.toString(curLocX) + " " + Integer.toString(curLocY - 1));
Integer distance = abs(xKeyM - curLocX) + abs(yKeyM - (curLocY - 1));
// checking if this move is the most reasonable
if (distance < min) {
min = distance;
indexX = curLocX;
indexY = curLocY - 1;
}
}
}
if (currentPath.size() >= shortestPath) {
validNeighbors.clear();
}
if (validNeighbors.isEmpty()) {
visited.get(curLocX).set(curLocY, 1);
if (currentPath.size() - 2 >= 0) {
String[] prev = currentPath.get(currentPath.size() - 2).split(" ");
Integer prevX = Integer.parseInt(prev[0]);
Integer prevY = Integer.parseInt(prev[1]);
System.out.println("m " + prev[0] + " " + prev[1]);
currentPath.remove(currentPath.size() - 1);
curLocX = prevX;
curLocY = prevY;
}
}
else {
if (Matrix[indexX][indexY].equals("K")) {
if (currentPath.size() < shortestPath) {
shortestPath = currentPath.size();
}
}
currentPath.add(Integer.toString(indexX) + " " + Integer.toString(indexY));
curLocX = indexX;
curLocY = indexY;
System.out.println("m " + indexX + " " + indexY);
visited.get(curLocX).set(curLocY, 1);
}
// case when the map is unsolvable
if (curLocX == 0 && curLocY == 0 && visited.get(0).get(1) == 1 && visited.get(1).get(0) == 1 && shortestPath == 100000) {
numberOfEnemies = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < numberOfEnemies; i++) {
String[] agent = scanner.nextLine().split(" ");
}
return -1;
}
// case when the shortest path has been found
if (curLocX == 0 && curLocY == 0 && visited.get(0).get(1) == 1 && visited.get(1).get(0) == 1 && shortestPath != 100000) {
numberOfEnemies = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < numberOfEnemies; i++) {
String[] agent = scanner.nextLine().split(" ");
}
return shortestPath;
}
// case when the shortest path is equal to the predicted shortest path
if (shortestPath == (xKeyM + yKeyM)) {
numberOfEnemies = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < numberOfEnemies; i++) {
String[] agent = scanner.nextLine().split(" ");
}
return shortestPath;
}
}
return -1;
}
}