-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion2.java
More file actions
156 lines (121 loc) · 3.61 KB
/
Recursion2.java
File metadata and controls
156 lines (121 loc) · 3.61 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
156
import java.util.ArrayList;
public class Recursion2 {
public static void main(String[] args) {
System.out.println(count(3, 3));
path("", 3, 3);
//2
boolean [] [] board = {
{true ,true , true},
{true ,true , true},
{true ,true , true}
};
allpaths("", board, 0, 0);
// N QUEEN QUESTION
int n = 4 ;
boolean [] [] board1 = new boolean[n][n];
Queen q = new Queen();
System.out.println(q.queens(board1,0));
}
// mase path question
static int count (int r ,int c){
if (r==1 || c==1){
return 1 ;
}
int left = count(r-1, c);
int right = count(r ,c-1);
return left+ right ;
}
static void path (String p , int r , int c){
if (r==1 && c==1){
System.out.println(p);
return ;
}
if (r>1){
path(p + 'D', r-1, c);
}
if (c>1){
path(p + 'R', r, c-1);
}
}
// in all dirtion :=
static void allpaths(String p , boolean maze [] [] , int r , int c){
if (r == maze.length-1 && c == maze[0].length-1){
System.out.println(p);
return;
}
if (!maze[r][c]){
return ;
}
// to mark visited place false and not go to them
maze [r] [c] = false ;
if (r < maze.length-1){
allpaths(p+'D', maze, r+1, c);
}
if (c < maze[0].length-1){
allpaths(p+'R', maze, r, c+1);
}
if (r>0){
allpaths(p+'U', maze, r-1, c);
}
if (c>0){
allpaths(p+'L', maze, r, c-1);
}
// to reverse the changes that made to allow the next recursion call happen
maze [r] [c] = true ;
}
}
class Queen {
static int queens(boolean [] [] board , int r){
if (r== board.length){
display (board);
System.out.println( );
return 1 ;
}
int count = 0 ;
// place for Queen
for (int c = 0; c < board.length; c++) {
if (isSafe(board , r , c)){
board[r][c] = true ;
count += queens(board, r+1);
board[r][c] = false;
}
}
return count ;
}
static boolean isSafe (boolean board[][] , int r , int c){
// vertical row check
for (int i = 0; i < r; i++) {
if (board[i][c] ){
return false ;
}
}
int maxLeft = Math.min(r, c);
// left diagonal
for (int i = 1; i <= maxLeft; i++) {
if (board[r-i][c-i]){
return false ;
}
}
// right diagonal
int maxRight = Math.min(r, board.length -c-1);
for (int i = 1; i <= maxRight; i++) {
if (board[r-i][c+i]){
return false ;
}
}
return true ;
}
static void display (boolean [][] board){
for (boolean[] r : board){
for(boolean element : r){
if (element){
System.out.print("Q ");
}
else{
System.out.print("X ");
}
}
System.out.println( );
}
}
}