-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaze.java
184 lines (151 loc) · 5.76 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Classz10;
import java.util.*;
/**
*
* @author ericawang
*/
public class Maze {
public static void print (boolean [][]arr){
for(int i = 0; i<arr.length;i++){
for(int u = 0; u<arr[0].length;u++){
System.out.print(arr[i][u]+" ");
}
System.out.println();
}
System.out.println();
}
public static void main (String [] args){
Scanner input = new Scanner(System.in);
int t = input.nextInt();
for(int NUM = 0; NUM<t; NUM++){
int r = input.nextInt();
int c = input.nextInt();
String nah = input.nextLine();
String [] dir = new String [r];
for(int i = 0; i<r; i++){
dir[i]=input.nextLine();
}
Queue <Integer>locX = new LinkedList();
Queue<Integer>locY = new LinkedList();
Queue<Integer> count = new LinkedList();
int x = 0;
int y = 0;
int co = 1;
Queue<boolean[][]> beenn = new LinkedList();
boolean [][] been = new boolean [r][c];
been[0][0]=true;
int tr = 0;
while(x!=-1){
char cur = dir[x].charAt(y);
if ((x==r-1)&&(y==c-1)){
System.out.println(co);
x = -1;
tr = 1;
}else{
if(cur=='+'){
boolean [][] b = been.clone();
if(x>0){
if(!b[x-1][y]&&dir[x-1].charAt(y)!='*'){
locX.add(x-1);
locY.add(y);
count.add(co+1);
b[x-1][y]=true;
beenn.add(b);
}
}
b = been.clone();
if(x<r-1){
if(!b[x+1][y]&&dir[x+1].charAt(y)!='*'){
locX.add(x+1);
locY.add(y);
count.add(co+1);
b[x+1][y]=true;
beenn.add(b);
}
}
b = been.clone();
if(y<c-1){
if(!b[x][y+1]&&dir[x].charAt(y+1)!='*'){
locX.add(x);
locY.add(y+1);
count.add(co+1);
b[x][y+1]=true;
beenn.add(b);
}
}
b = been.clone();
if(y>0){
if(!b[x][y-1]&&dir[x].charAt(y-1)!='*'){
locX.add(x);
locY.add(y-1);
count.add(co+1);
b[x][y-1]=true;
beenn.add(b);
}
}
}else if(cur == '-'){
boolean [][] b = been.clone();
if(y<c-1){
if(!b[x][y+1]&&dir[x].charAt(y+1)!='*'){
locX.add(x);
locY.add(y+1);
count.add(co+1);
b[x][y+1]=true;
beenn.add(b);
}
}
b = been.clone();
if(y>0){
if(!b[x][y-1]&&dir[x].charAt(y-1)!='*'){
locX.add(x);
locY.add(y-1);
count.add(co+1);
b[x][y-1]=true;
beenn.add(b);
}
}
}else if(cur == '|'){
boolean [][] b = been.clone();
if(x>0){
if(!b[x-1][y]&&dir[x-1].charAt(y)!='*'){
locX.add(x-1);
locY.add(y);
count.add(co+1);
b[x-1][y]=true;
beenn.add(b);
}
}
b = been.clone();
if(x<r-1){
if(!b[x+1][y]&&dir[x+1].charAt(y)!='*'){
locX.add(x+1);
locY.add(y);
count.add(co+1);
b[x+1][y]=true;
beenn.add(b);
}
}
}
}
if(x!=-1){
if(locX.isEmpty()){
x=-1;
}else{
x = locX.poll();
y = locY.poll();
co = count.poll();
been = beenn.poll();
}
}
}
if(tr == 0){
System.out.println(-1);
}
}
}
}