-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaze.java
More file actions
131 lines (124 loc) · 2.72 KB
/
maze.java
File metadata and controls
131 lines (124 loc) · 2.72 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
import java.util.Queue;
import java.util.LinkedList;
import java.util.ArrayList;
class Box
{
int r,c;
int dist;
public Box(int r,int c,int dist)
{
this.r=r;
this.c=c;
this.dist=dist;
}
}
class Solution{
public static boolean inMaze(int r,int c,int h,int w)
{
return !(r<0 || r>=h || c<0 || c>=w);
}
public static int shortest(int[][] map)
{
int h=map.length;
int w=map[0].length;
int r,c;
Queue<Box> toVisit=new LinkedList<Box>();
toVisit.add(new Box(0,0,1));
map[0][0]=-1;
while(!toVisit.isEmpty())
{
Box current=toVisit.remove();
r=current.r;
c=current.c;
//System.out.println("Visiting ("+r+","+c+")");
if(r==h-1 && c==w-1) //If reached end
return current.dist;
//Check neighbors
//LEFT
if(inMaze(r,c-1,h,w) && map[r][c-1]!=1 && map[r][c-1]!=-1)
{
toVisit.add(new Box(r,c-1,current.dist+1));
map[r][c-1]=-1;
}
//RIGHT
if(inMaze(r,c+1,h,w) && map[r][c+1]!=1 && map[r][c+1]!=-1)
{
toVisit.add(new Box(r,c+1,current.dist+1));
map[r][c+1]=-1;
}
//UP
if(inMaze(r-1,c,h,w) && map[r-1][c]!=1 && map[r-1][c]!=-1)
{
toVisit.add(new Box(r-1,c,current.dist+1));
map[r-1][c]=-1;
}
//DOWN
if(inMaze(r+1,c,h,w) && map[r+1][c]!=1 && map[r+1][c]!=-1)
{
toVisit.add(new Box(r+1,c,current.dist+1));
map[r+1][c]=-1;
}
}
return Integer.MAX_VALUE ;//Should never happen
}
public static void createCopy(int[][] map,int[][] copy)
{
for(int i=0;i<map.length;i++)
{
for(int j=0;j<map[0].length;j++)
copy[i][j]=map[i][j];
}
}
public static int solution(int[][] map)
{
int[][] copy=new int[map.length][map[0].length];
ArrayList<Integer> distances=new ArrayList<Integer>();
createCopy(map,copy);
distances.add(shortest(copy));
for(int i=0;i<map.length;i++)
{
for(int j=0;j<map[0].length;j++)
{
if(map[i][j]==1)
{
createCopy(map,copy);
copy[i][j]=0;
distances.add(shortest(copy));
}
}
}
int min=map.length*map[0].length;
for(int x:distances)
{
if(x<min)
min=x;
}
System.out.println(distances);
return min;
}
public static int[][] testMap()
{
int[][] ret=new int[20][20];
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
ret[i][j]=0;
}
}
return ret;
}
public static void main(String[] args)
{
int[][] map1=new int[][]{{0,1,1,0},{0,0,0,1},{1,1,0,0},{1,1,1,0}};
int[][] map2=new int[][]{{0,0,0,0,0,0},{1,0,1,1,1,0},{0,0,0,0,0,0},{0,1,1,1,1,1},{0,1,1,1,1,1},{0,0,0,0,0,0}};
int[][] map3=testMap();
int[][] map4=new int[][]{{0,1,1,1,0,0},
{0,1,0,0,0,0},
{0,0,0,1,1,0},
{1,1,1,1,0,0},
{1,0,0,0,0,0}};
int[][] map5=new int[][]{{0,0,0},{1,1,1},{0,0,0}};
System.out.println(solution(map5));
}
}