The Knight’s tour problem | Backtracking | DFS | Love Babbar DSA sheet

  Рет қаралды 14,372

Aditya Rajiv

Aditya Rajiv

Күн бұрын

Пікірлер: 15
@yuvraj0047
@yuvraj0047 Жыл бұрын
the way knight move was wrong but position is correct😅😂😂
@iEntertainmentFunShorts
@iEntertainmentFunShorts 2 жыл бұрын
Code is incorrect not printing the correct answer showing all 0's in matrix
@hemant-0364
@hemant-0364 Жыл бұрын
Well done brother 👍
@adityarajiv6346
@adityarajiv6346 Жыл бұрын
Thank you so much 😀
@prajwalsable7586
@prajwalsable7586 3 жыл бұрын
We can do it with 1 2d array also
@yakshmahawer5434
@yakshmahawer5434 3 жыл бұрын
Bro I thought the whole logic without breaking any rule of the question and code was going quite well but at one point it had no option to go anywhere as it was at bottom right and on left and right side the boxes were visited so got MAXIMUM STACK error and it had no where to go
@yakshmahawer5434
@yakshmahawer5434 3 жыл бұрын
Sorry left and up
@sonuchaudhary5479
@sonuchaudhary5479 3 жыл бұрын
thank you bro
@adityarajiv6346
@adityarajiv6346 3 жыл бұрын
Welcome 😀. Please share with friends 🙏.
@pranjaygoyal6139
@pranjaygoyal6139 2 жыл бұрын
bro i am not satisfied with your code
@pranjaygoyal6139
@pranjaygoyal6139 2 жыл бұрын
shreya what do u think?
@shreyaagarwal5614
@shreyaagarwal5614 2 жыл бұрын
bro i copied your code but its not showing the correct output
@pranjaygoyal6139
@pranjaygoyal6139 2 жыл бұрын
@@shreyaagarwal5614 yeh ladki sahi bol rhi hai sir ji
@victorvondoom2350
@victorvondoom2350 2 жыл бұрын
@@shreyaagarwal5614 // Javascript program for Knight Tour problem let N = 8; // A utility function to check if i,j are // valid indexes for N*N chessboard function isSafe(x, y, sol) { return(x >= 0 && x < N && y >= 0 && y < N && sol[x][y] == -1); } // A utility function to print solution // matrix sol[N][N] function printSolution(sol) { for(let x = 0; x < N; x++) { for(let y = 0; y < N; y++) console.log(sol[x][y] + " "); console.log("\t"); } } // This function solves the Knight Tour problem // using Backtracking. This function mainly // uses solveKTUtil() to solve the problem. It // returns false if no complete tour is possible, // otherwise return true and prints the tour. // Please note that there may be more than one // solutions, this function prints one of the // feasible solutions. function solveKT() { let sol = new Array(8); for(var i = 0; i < sol.length; i++) { sol[i] = new Array(2); } // Initialization of solution matrix for(let x = 0; x < N; x++) for(let y = 0; y < N; y++) sol[x][y] = -1; // xMove[] and yMove[] define next move of Knight. // xMove[] is for next value of x coordinate // yMove[] is for next value of y coordinate let xMove = [ 2, 1, -1, -2, -2, -1, 1, 2 ]; let yMove = [ 1, 2, 2, 1, -1, -2, -2, -1 ]; // Since the Knight is initially at the first block sol[0][0] = 0; // Start from 0,0 and explore all tours using // solveKTUtil() if (!solveKTUtil(0, 0, 1, sol, xMove, yMove)) { console.log(" solution does not exist"); return false; } else printSolution(sol); return true; } // A recursive utility function to solve Knight // Tour problem function solveKTUtil(x, y, movei, sol, xMove, yMove) { let k, next_x, next_y; if (movei == N * N) return true; // Try all next moves from the // current coordinate x, y for(k = 0; k < 8; k++) { next_x = x + xMove[k]; next_y = y + yMove[k]; if (isSafe(next_x, next_y, sol)) { sol[next_x][next_y] = movei; if (solveKTUtil(next_x, next_y, movei + 1, sol, xMove, yMove)) return true; else sol[next_x][next_y] = -1; // backtracking } } return false; } // Driver code // Function Call solveKT(); // This code is geeksForGeeks
Fastest Way to Learn ANY Programming Language: 80-20 rule
8:24
Sahil & Sarra
Рет қаралды 921 М.
Smart Sigma Kid #funny #sigma
00:33
CRAZY GREAPA
Рет қаралды 31 МЛН
А я думаю что за звук такой знакомый? 😂😂😂
00:15
Денис Кукояка
Рет қаралды 6 МЛН
6 Introduction to Backtracking - Brute Force Approach
8:15
Abdul Bari
Рет қаралды 1,3 МЛН
L15. Sudoko Solver | Backtracking
26:10
take U forward
Рет қаралды 289 М.
Knight's Tour - Numberphile
4:51
Numberphile
Рет қаралды 959 М.
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 745 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 89 МЛН
L14. N-Queens | Leetcode Hard | Backtracking
36:55
take U forward
Рет қаралды 434 М.
6.1 N Queens Problem using Backtracking
13:41
Abdul Bari
Рет қаралды 2,1 МЛН
Recursive Backtracking - DSA Course in Python Lecture 14
12:58
Smart Sigma Kid #funny #sigma
00:33
CRAZY GREAPA
Рет қаралды 31 МЛН