Skip to content

Commit 709e75a

Browse files
backtracking
1 parent 389911c commit 709e75a

File tree

3 files changed

+200
-0
lines changed

3 files changed

+200
-0
lines changed
1.7 MB
Binary file not shown.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.kunal.backtracking;
2+
3+
import java.util.Arrays;
4+
5+
public class AllPaths {
6+
public static void main(String[] args) {
7+
boolean[][] board = {
8+
{true, true, true},
9+
{true, true, true},
10+
{true, true, true}
11+
};
12+
int[][] path = new int[board.length][board[0].length];
13+
allPathPrint("", board, 0, 0, path, 1);
14+
}
15+
16+
static void allPath(String p, boolean[][] maze, int r, int c) {
17+
if (r == maze.length - 1 && c == maze[0].length - 1) {
18+
System.out.println(p);
19+
return;
20+
}
21+
22+
if (!maze[r][c]) {
23+
return;
24+
}
25+
26+
// i am considering this block in my path
27+
maze[r][c] = false;
28+
29+
if (r < maze.length - 1) {
30+
allPath(p + 'D', maze, r+1, c);
31+
}
32+
33+
if (c < maze[0].length - 1) {
34+
allPath(p + 'R', maze, r, c+1);
35+
}
36+
37+
if (r > 0) {
38+
allPath(p + 'U', maze, r-1, c);
39+
}
40+
41+
if (c > 0) {
42+
allPath(p + 'L', maze, r, c-1);
43+
}
44+
45+
// this line is where the function will be over
46+
// so before the function gets removed, also remove the changes that were made by that function
47+
maze[r][c] = true;
48+
}
49+
50+
51+
52+
static void allPathPrint(String p, boolean[][] maze, int r, int c, int[][] path, int step) {
53+
if (r == maze.length - 1 && c == maze[0].length - 1) {
54+
path[r][c] = step;
55+
for(int[] arr : path) {
56+
System.out.println(Arrays.toString(arr));
57+
}
58+
System.out.println(p);
59+
System.out.println();
60+
return;
61+
}
62+
63+
if (!maze[r][c]) {
64+
return;
65+
}
66+
67+
// i am considering this block in my path
68+
maze[r][c] = false;
69+
path[r][c] = step;
70+
if (r < maze.length - 1) {
71+
allPathPrint(p + 'D', maze, r+1, c, path, step+1);
72+
}
73+
74+
if (c < maze[0].length - 1) {
75+
allPathPrint(p + 'R', maze, r, c+1, path, step+1);
76+
}
77+
78+
if (r > 0) {
79+
allPathPrint(p + 'U', maze, r-1, c, path, step+1);
80+
}
81+
82+
if (c > 0) {
83+
allPathPrint(p + 'L', maze, r, c-1, path, step+1);
84+
}
85+
86+
// this line is where the function will be over
87+
// so before the function gets removed, also remove the changes that were made by that function
88+
maze[r][c] = true;
89+
path[r][c] = 0;
90+
}
91+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.kunal.backtracking;
2+
3+
import java.util.ArrayList;
4+
5+
public class Maze {
6+
public static void main(String[] args) {
7+
// System.out.println(count(3, 3));
8+
// path("", 3, 3);
9+
// System.out.println(pathRet("", 3, 3));
10+
11+
// System.out.println(pathRetDiagonal("", 3, 3));
12+
13+
boolean[][] board = {
14+
{true, true, true},
15+
{true, false, true},
16+
{true, true, true}
17+
};
18+
19+
pathRestrictions("", board, 0, 0);
20+
}
21+
22+
static int count(int r, int c) {
23+
if (r == 1 || c == 1) {
24+
return 1;
25+
}
26+
int left = count(r-1, c);
27+
int right = count(r, c-1);
28+
return left + right;
29+
}
30+
31+
static void path(String p, int r, int c) {
32+
if (r == 1 && c == 1) {
33+
System.out.println(p);
34+
return;
35+
}
36+
37+
if (r > 1) {
38+
path(p + 'D', r-1, c);
39+
}
40+
41+
if (c > 1) {
42+
path(p + 'R', r, c-1);
43+
}
44+
}
45+
46+
static ArrayList<String> pathRet(String p, int r, int c) {
47+
if (r == 1 && c == 1) {
48+
ArrayList<String> list = new ArrayList<>();
49+
list.add(p);
50+
return list;
51+
}
52+
53+
ArrayList<String> list = new ArrayList<>();
54+
55+
if (r > 1) {
56+
list.addAll(pathRet(p + 'D', r-1, c));
57+
}
58+
59+
if (c > 1) {
60+
list.addAll(pathRet(p + 'R', r, c-1));
61+
}
62+
63+
return list;
64+
}
65+
66+
static ArrayList<String> pathRetDiagonal(String p, int r, int c) {
67+
if (r == 1 && c == 1) {
68+
ArrayList<String> list = new ArrayList<>();
69+
list.add(p);
70+
return list;
71+
}
72+
73+
ArrayList<String> list = new ArrayList<>();
74+
75+
if (r > 1 && c > 1) {
76+
list.addAll(pathRetDiagonal(p + 'D', r-1, c-1));
77+
}
78+
79+
if (r > 1) {
80+
list.addAll(pathRetDiagonal(p + 'V', r-1, c));
81+
}
82+
83+
if (c > 1) {
84+
list.addAll(pathRetDiagonal(p + 'H', r, c-1));
85+
}
86+
87+
return list;
88+
}
89+
90+
static void pathRestrictions(String p, boolean[][] maze, int r, int c) {
91+
if (r == maze.length - 1 && c == maze[0].length - 1) {
92+
System.out.println(p);
93+
return;
94+
}
95+
96+
if (!maze[r][c]) {
97+
return;
98+
}
99+
100+
if (r < maze.length - 1) {
101+
pathRestrictions(p + 'D', maze, r+1, c);
102+
}
103+
104+
if (c < maze[0].length - 1) {
105+
pathRestrictions(p + 'R', maze, r, c+1);
106+
}
107+
}
108+
109+
}

0 commit comments

Comments
 (0)