forked from codestar-work/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.java
executable file
·169 lines (153 loc) · 3.24 KB
/
maze.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
import java.io.*;
import java.net.*;
import java.util.*;
class Maze {
public static void main(String args[]) {
Maze m = new Maze();
m.main();
}
void main() {
read();
// dfs(startRow, startCol);
bfs();
}
void bfs() {
Item first = new Item(startRow, startCol, -1);
queue.put(first);
while (!queue.isEmpty()) {
Item current = queue.get();
if (current.row == goalRow && current.col == goalCol) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (maze[i][j] == 'O') {
maze[i][j] = ' ';
}
}
}
while (current.parent != -1) {
maze[current.row][current.col] = 'O';
current = queue.get(current.parent);
}
print();
} else {
if (maze[current.row+1][current.col ] == ' ') {
maze[current.row+1][current.col ] = 'O';
Item next = new Item(current.row+1, current.col ,
queue.front-1);
queue.put(next);
}
if (maze[current.row-1][current.col ] == ' ') {
maze[current.row-1][current.col ] = 'O';
Item next = new Item(current.row-1, current.col ,
queue.front-1);
queue.put(next);
}
if (maze[current.row ][current.col+1] == ' ') {
maze[current.row ][current.col+1] = 'O';
Item next = new Item(current.row , current.col+1,
queue.front-1);
queue.put(next);
}
if (maze[current.row ][current.col-1] == ' ') {
maze[current.row ][current.col-1] = 'O';
Item next = new Item(current.row , current.col-1,
queue.front-1);
queue.put(next);
}
}
}
}
Queue<Item> queue = new Queue<>();
void dfs(int r, int c) {
maze[r][c] = 'O';
if (r == goalRow && c == goalCol) {
print();
} else {
if (maze[r+1][c ] == ' ') {
dfs (r+1, c );
maze[r+1][c ] = ' ';
}
if (maze[r-1][c ] == ' ') {
dfs (r-1, c );
maze[r-1][c ] = ' ';
}
if (maze[r ][c+1] == ' ') {
dfs (r , c+1);
maze[r ][c+1] = ' ';
}
if (maze[r ][c-1] == ' ') {
dfs (r , c-1);
maze[r ][c-1] = ' ';
}
}
}
void read() {
maze = new char[MAX][MAX];
height = 0;
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
width = s.length();
for (int j = 0; j < width; j++) {
maze[height][j] = s.charAt(j);
}
height++;
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (maze[i][j] == 'R') {
startRow = i;
startCol = j;
maze[i][j] = ' ';
}
if (maze[i][j] == 'X') {
goalRow = i;
goalCol = j;
maze[i][j] = ' ';
}
}
}
}
void print() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
System.out.print(maze[i][j]);
}
System.out.println();
}
}
int MAX = 256;
char [][] maze;
int width;
int height;
int startRow = -1, startCol = -1, goalRow = -1, goalCol = -1;
}
class Item {
int row;
int col;
int parent;
Item(int r, int c, int p) {
row = r;
col = c;
parent = p;
}
}
class Queue<T> {
ArrayList<T> queue = new ArrayList<>();
int front = 0;
int rear = 0;
void put(T t) {
queue.add(t);
rear++;
}
T get() {
front++;
return queue.get(front-1);
}
T get(int index) {
return queue.get(index);
}
boolean isEmpty() {
return front == rear;
}
}