-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreasureQuest.java
More file actions
175 lines (132 loc) · 5.46 KB
/
Copy pathTreasureQuest.java
File metadata and controls
175 lines (132 loc) · 5.46 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
import java.util.Scanner;
import java.util.LinkedList;
import java.util.Queue;
public class TreasureQuest {
public static int[] findSpecialTiles(char[][] myMap) {
int[] specialTiles = new int[2]; // 0 for S, 1 for E
int cols = myMap[0].length;
boolean foundS = false, foundE = false;
for (int i = 0; i < myMap.length; i++) {
for (int j = 0; j < myMap[i].length; j++) {
if (myMap[i][j] == 'S') {
specialTiles[0] = node(i, j, cols);
foundS = true;
} else if (myMap[i][j] == 'E') {
specialTiles[1] = node(i, j, cols);
foundE = true;
}
}
if (foundS && foundE) break;
}
return specialTiles;
}
private static boolean isInBounds(int x, int y, int rows, int cols) {
return x >= 0 && x < rows && y >= 0 && y < cols;
}
private static int node(int i, int j, int cols) {
return (i * cols) + j;
}
public static Graph buildGraphFromMap(boolean[][] SafeMatrix) {
int rows = SafeMatrix.length;
int cols = SafeMatrix[0].length;
Graph graph = new Graph(rows * cols);
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // up, down, left, right
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (SafeMatrix[i][j]) {
for (int[] dir : directions) {
int newX = i + dir[0];
int newY = j + dir[1];
if (isInBounds(newX, newY, rows, cols) && SafeMatrix[newX][newY]) {
graph.addEdge(node(i, j, cols), node(newX, newY, cols), 1);
}
}
}
}
}
return graph;
}
public static char[][] readMap(int n1, int n2) {
Scanner scanner = new Scanner(System.in);
char[][] mapMatrix = new char[n1][n2];
for (int i = 0; i < n1; i++) {
String line = scanner.nextLine().trim();
mapMatrix[i] = line.toCharArray();
}
return mapMatrix;
}
public static int[][] readMonsterPositions(int numOfMonsters,int n, int m) {
Scanner scanner = new Scanner(System.in);
int[][] monsterPositions = new int[numOfMonsters][3];
for (int i = 0; i < numOfMonsters; i++) {
monsterPositions[i][0] = scanner.nextInt() - 1; // convert to 0-based index
monsterPositions[i][1] = scanner.nextInt() - 1;
monsterPositions[i][2] = scanner.nextInt();
if(monsterPositions[i][0] < 0 || monsterPositions[i][1] < 0 || monsterPositions[i][0]>n || monsterPositions[i][1]>m){
System.out.println("Invalid monster position: " + (monsterPositions[i][0]+1) + " " + (monsterPositions[i][1]+1));
}
}
return monsterPositions;
}
public static void shotest(int start, int end, Graph graph){
for(int i = 0; i < graph.adjacencylist.length; i++){
if(i== start){
}
}
}
public static boolean[][] markMonitoredTiles(char[][] mapMatrix, int n1, int n2, int[][] monsterPositions) {
boolean[][] safe = new boolean[n1][n2];
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
safe[i][j] = (mapMatrix[i][j] != '#');
}
}
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for (int[] monster : monsterPositions) {
int x = monster[0];
int y = monster[1];
int range = monster[2];
safe[x][y] = false;
for (int[] dir : directions) {
int dx = dir[0];
int dy = dir[1];
for (int r = 1; r <= range; r++) {
int newX = x + dx * r;
int newY = y + dy * r;
if (!isInBounds(newX, newY, n1, n2)){
break;
}
if (mapMatrix[newX][newY] == '#'){break;}
safe[newX][newY] = false;
}
}
}
return safe;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int k = scanner.nextInt();
scanner.nextLine();
// Read map
char[][] map = readMap(n, m);
// Find S and E positions
int[] startEnd = findSpecialTiles(map);
int start = startEnd[0];
int end = startEnd[1];
System.out.println("Start: " + start + ", End: " + end);
int[][] monsters = readMonsterPositions(k, n ,m);
boolean[][] safeMap = markMonitoredTiles(map, n, m, monsters);
int startX = start / m;
int startY = start % m;
int endX = end / m;
int endY = end % m;
if (!safeMap[startX][startY] || !safeMap[endX][endY]) {
System.out.println("IMPOSSIBLE");
return;
}
Graph graph = buildGraphFromMap(safeMap);
graph.dijkstra_GetMinDistances(start,end);
}
}