-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemi6.java
More file actions
155 lines (151 loc) · 7.01 KB
/
Semi6.java
File metadata and controls
155 lines (151 loc) · 7.01 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
import java.util.*;
public class Semi6 {
static class Coordinate {
int x;
int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
static boolean belongingToTheMap(int[][] map, Coordinate ex) {
if (ex.x < 0 & ex.y < 0 | ex.x > map.length & ex.y > map[0].length) return false;
else return true;
}
}
static class MapCreater {
static int[][] createEmptyMap(int rows, int columns) {
return new int[rows][columns];
}
static int[][] getMap() {
return new int[][]{
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, 00, 00, 00, -1, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, -1},
{-1, 00, 00, 00, 00, 00, 00, -1, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, -1},
{-1, 00, 00, 00, -1, 00, 00, -1, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, -1},
{-1, 00, 00, 00, -1, 00, -1, -1, -1, -1, 00, 00, 00, 00, -1, 00, -1, -1, -1, 00, 00, 00, 00, -1},
{-1, 00, 00, 00, -1, 00, -1, 00, 00, -1, 00, 00, 00, 00, -1, 00, -1, 00, 00, 00, 00, 00, 00, -1},
{-1, -1, -1, 00, -1, 00, -1, 00, 00, -1, 00, 00, 00, 00, -1, 00, -1, 00, 00, 00, 00, 00, 00, -1},
{-1, 00, 00, 00, -1, 00, -1, 00, 00, -1, -1, -1, 00, 00, -1, 00, -1, 00, 00, 00, 00, 00, 00, -1},
{-1, 00, 00, 00, -1, 00, 00, 00, 00, -1, 00, 00, 00, 00, -1, 00, 00, 00, 00, 00, 00, 00, 00, -1},
{-1, 00, 00, 00, -1, 00, 00, 00, 00, -1, 00, 00, 00, 00, -1, 00, 00, 00, 00, 00, 00, 00, 00, -1},
{-1, 00, 00, 00, -1, -1, -1, -1, -1, -1, 00, 00, 00, 00, -1, -1, -1, -1, -1, 00, 00, 00, 00, -1},
{-1, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, -1},
{-1, 00, 00, 00, -1, -1, -1, -1, -1, -1, -1, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, -1},
{-1, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, -3, 00, 00, 00, 00, 00, 00, 00, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
};
}
private static void createOutlineMap(int[][] map) {
for (int i = 0; i < map[0].length; i++) {
map[0][i] = -1;
map[map.length - 1][i] = -1;
}
for (int i = 0; i < map.length; i++) {
map[i][0] = -1;
map[i][map[i].length - 1] = -1;
}
}
private static void createWall(int[][] map) {
int row = new Random().nextInt(2, map.length);
int column = new Random().nextInt(3, map[0].length);
for (int i = row; i < map.length - 4; i++) {
map[i][column] = -1;
}
for (int i = column; i < map[0].length - 4; i++) {
map[row][i] = -1;
}
}
static int[][] getRandomMap(int rows, int columns) {
int[] map[] = createEmptyMap(rows, columns);
createOutlineMap(map);
createWall(map);
createWall(map);
createWall(map);
return map;
}
static void printMap(int[][] map) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
System.out.printf("%5d", map[i][j]);
}
System.out.println();
}
}
static void printSolution(int[][] map,Coordinate st,Coordinate ex){
Queue<Coordinate> sol = AlgorithmLi.searchSolution(map,st,ex);
while (sol.size() != 0){
Coordinate step = sol.remove();
map[step.x][step.y] = 0;
}
printMap(map);
}
static void setStart(int[][] map, Coordinate st) {
if (Coordinate.belongingToTheMap(map, st)) map[st.x][st.y] = -3;
else return;
}
static void setExit(int[][] map, Coordinate ex) {
if (Coordinate.belongingToTheMap(map, ex)) {
map[ex.x][ex.y] = -2;
} else return;
}
public class AlgorithmLi {
static Queue<Coordinate> searchSolution(int[][] map, Coordinate st, Coordinate ex) {
Queue<Coordinate> sol = new LinkedList<>();
Coordinate step = ex;
sol.add(step);
while (map[step.x][step.y] != map[st.x][st.y]) {
if (map[step.x - 1][step.y] == map[step.x][step.y] - 1) {
step = new Coordinate(step.x - 1, step.y);
sol.add(step);
} else if (map[step.x][step.y - 1] == map[step.x][step.y] - 1) {
step = new Coordinate(step.x, step.y - 1);
sol.add(step);
} else if (map[step.x + 1][step.y] == map[step.x][step.y] - 1) {
step = new Coordinate(step.x + 1, step.y);
sol.add(step);
} else if (map[step.x][step.y + 1] == map[step.x][step.y] - 1) {
step = new Coordinate(step.x, step.y + 1);
sol.add(step);
}
}
return sol;
}
static void wave(int[][] map, Coordinate st) {
Queue<Coordinate> res = new LinkedList<>();
map[st.x][st.y] = 1;
res.add(st);
while (res.size() > 0) {
Coordinate step = res.remove();
int x = step.x;
int y = step.y;
if (map[x - 1][y] == 0) {
res.add(new Coordinate(x - 1, y));
map[x - 1][y] = map[x][y] + 1;
}
if (map[x][y - 1] == 0) {
res.add(new Coordinate(x, y - 1));
map[x][y - 1] = map[x][y] + 1;
}
if (map[x + 1][y] == 0) {
res.add(new Coordinate(x + 1, y));
map[x + 1][y] = map[x][y] + 1;
}
if (map[x][y + 1] == 0) {
res.add(new Coordinate(x, y + 1));
map[x][y + 1] = map[x][y] + 1;
}
}
}
}
public static void main(String[] args) {
int[] map[] = MapCreater.getRandomMap(10, 20);
Coordinate exit = new Coordinate(8, 10);
Coordinate start = new Coordinate(6, 2);
MapCreater.setStart(map, start);
AlgorithmLi.wave(map, start);
MapCreater.printMap(map);
System.out.println("--");
MapCreater.printSolution(map,start,exit);
}
}
}