1,650 questions
2
votes
0
answers
136
views
How do I get the backtracking technique to solve large mazes too?
I'm developing a backtracking function to solve a maze given by a matrix of 0 and 1. The problem is that, for small matrices, it correctly finds the shortest path, but for matrices 15x15 or larger, it ...
0
votes
1
answer
66
views
Recursive Permutations JavaScript problem returning empty array
A permutations problem (LeetCode), which finds the permutations of an array [1,2,3], is returning an empty array through a backtracking recursive function. The console prints out the following:
Input
...
1
vote
1
answer
90
views
Can't tell the difference between two python n-queens solutions
Reading up on backtracking led me to a page on geeksforgeeks.org about solutions to the n-queens problem. The first solution is introduced as the "naive approach" that generates all possible ...
0
votes
1
answer
78
views
Backtracking confusion
Concerning Javascript, I've been trying to understand backtracking lately and it's really confusing. Specifically, I've been trying to understand the permutation problem, which when given an array, we ...
4
votes
1
answer
79
views
construct Backtrack function from state-space tree
Q. How do I decide whether to pass i+1 or j+1 in the recursive call when constructing a backtracking function from a state-space tree diagram?
Key Insights I develop from the state-space tree diagram:
...
0
votes
0
answers
58
views
Finding the deviation from a navigation trajectory to it's ideal path
Background: I have recorded navigation trajectory positional x,y and its time point (50hz) data from a virtual reality navigation in an empty room.
Problem: I want to calculate how much the navigation ...
1
vote
1
answer
52
views
Implement a Knight's Tour algorithm using backtracking in Python
I try to solve Knight tour problem with python and backtracking, but the code doesn't respond likely I want...
This is my code in python:
import random
board = [
[0,0,0,0,0,0],
[0,0,0,0,0,0],
...
0
votes
1
answer
88
views
time complexity of returning power set (leetcode 78 subsets)
Why the time complexity of generating power set of given array is O(n * 2^n). The solution which I created or even the solution which is shared on leetcode runs 2^n times. 1 loop to generate 1 subset.
...
0
votes
0
answers
58
views
.Dll file dependecy error in UNIDEC software after changing path to my desired file in unidecgui
I wanted to check the part of unidec softwares code that converts agilent .d folders to txt file so i opened the agilent.py file and changed the path to my desired file after running the command it ...
0
votes
1
answer
151
views
Is My Greedy Approach to Finding the Largest Independent Set Optimal?
I am working on a task to find the largest independent set in a graph. After researching various approaches, I found that backtracking is often recommended, but it has exponential time complexity, ...
-2
votes
1
answer
82
views
What's going on with this backtracking problem?
class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> combine(int n, int k) {
List<Integer> list = new ...
-4
votes
1
answer
232
views
Time Complexity of Backtracking solution - Leetcode 473. Matchsticks to Square [duplicate]
The following is a Leetcode problem: 473. Matchsticks to Square (https://leetcode.com/problems/matchsticks-to-square/description/)
Problem Statement
You have an array, matchsticks, of size n, with ...
-1
votes
1
answer
174
views
Sudoku Solver not working (backtracking, forward checking, c++)
I am working on an optimized Sudoku solver. The basic idea of backtracking remains, though I have added forward checking. The domain (what numbers can be set into a cell) is stored for every cell. ...
0
votes
0
answers
47
views
Optimizing backtracking algorithm for non-jigsaw puzzle
As my previous post outlined, I am making a puzzle solver, however, my puzzles don't contain jigsaw pieces but normal rectangles instead. My initial post was asking about an algorithm that I can use ...
3
votes
1
answer
145
views
How does Prolog "redo" search when backtracking
I think that I understand how Prolog uses unification and backtracking to find the first match. However I'm having a hard time trying to understand what does it do when it is asked to "redo" ...
0
votes
2
answers
102
views
Issue in backtracking while creating combinations of a list of size k
def combination(n,k):
combi=[]
def backtrack(start,comb):
print(comb)
if len(comb)==k:
combi.append(comb.copy())
return
for ...
1
vote
1
answer
91
views
Why backtracking is giving wrong results in case of 1st question and correct results for 2nd. Leetcode #322 #78 respectively
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the fewest number of coins that you need to make ...
6
votes
1
answer
218
views
Can Knuth's "Algorithm B" for Queens puzzle be written without goto statements or recursion?
Though I'm aware of the basic recursive N queens solution, I'm trying to learn more about backtracking by studying and implementing Knuth's "Algorithm B (Basic backtrack)" for the case of ...
1
vote
1
answer
310
views
Leetcode - Combination sum problem in Python
I am building the logic to the Leetcode - Combination Sum problem. Here is a link to the problem.The problem states:
Given an array of distinct integers candidates and a target integer
target, return ...
1
vote
1
answer
175
views
Backtracking in python - The Knight’s tour problem
Problem Statement: Given a N*N board with the Knight placed on the first block of an empty board. Moving according to the rules of chess knight must visit each square exactly once. Print the order of ...
0
votes
0
answers
75
views
Backtracking not working on a Hitori matrix
It seems the code I wrote doesn't backtrack properly and i have no idea where the backtracking goes wrong.
Hitori is an "reverse" Sudoku. You are given a matrix filled with numbers and have ...
2
votes
1
answer
155
views
Recursive backtracking - Word search in a 2D array
I was attempting the word search problem here on leetcode. Leetcode seems to give me an error for the two test cases:
board = [["a","a"]], word = "aa"
and for : board = [[...
1
vote
1
answer
93
views
Question from recursive code - n queens problem
I was attempting a recursive solution to the n queens problem. Please take a look at this code:
def solve(n,chess_arr,forbidden_cells=[]):
print("1.",n,chess_arr,forbidden_cells)
if n&...
0
votes
1
answer
68
views
Time complexity for recursive calls
I have written a code to recursively remove the adjacent duplicates from a string
class Solution{
String rremove(String s) {
// code here
StringBuilder sb = new StringBuilder();
int i ...
0
votes
0
answers
37
views
python backtracking debug [duplicate]
This is a minimal reproducible code, so please do not concern about the meaningfulness of the code.
I want to input a list with one element, and return once it grows to length of 5. So I use a ...
0
votes
1
answer
108
views
Why does Prolog automatically backtrack in one case but not in another?
I have a Prolog program with two predicates, rr and p, each with its own set of rules and facts. When querying these predicates with specific inputs, I noticed that Prolog automatically backtracks in ...
0
votes
0
answers
41
views
Can some dry run for nQueens problem all ways?
public static void Queens(char b[][],int row) {
if(row==b.length){
print(b);
return;
}
for(int j=0;j<b[0].length;j++){
b[row][j]='Q';
...
0
votes
1
answer
112
views
Return from multiple recursions
I’m doing leetcode 79 C++ solution, where you need to find word in 2D matrix. I’m doing it with backtracking
class Solution
{
private:
vector<vector<char>> matrix; //Пример ...
-1
votes
1
answer
62
views
Leetcode 1255-recursion and backtracking
I dont quite understand why have we used .clone() method when we have a second for loop to restore the letterCount.And when i'm runnig this code without .clone() method its giving me the wrong answer? ...
1
vote
1
answer
104
views
Solving Maze using Backtracking C++
I have got this maze of 2d matrix. '@' indicates the start and '#' indicates the end. 0 means the path is blocked and 1 means that you can pass through. The program needs to solve the maze and print ...
1
vote
1
answer
79
views
Stack overflow when finding all paths through a grid with recursion and backtracking
I have been following a playlist of Java for data structures and algorithms and I have come across the topic of Backtracking in which a board is given to you along with starting point (0,0) and end ...
1
vote
1
answer
80
views
Is there a simple way to make a backtracking algorithm in Python?
I need help making a python algorithm. I want to make a grid and place a "x" on cells based on certain rules. The grid is 15 rows and 100 columns. There should only be 1 "x" per ...
-5
votes
1
answer
122
views
getting TLE in leetcode question 212- word search 2 using backtrcaking even after pruning, how do i optimize it more [closed]
Question
Given an m x n board of characters and a list of strings words, return all words on the board.
Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells ...
1
vote
0
answers
60
views
Divide class students into breakout rooms
In a school, I have multiple classes. Each class could be divided into breakout rooms of 3 or 4 students randomly. We want to avoid duplicate breakout rooms at least we want more randomness and ...
1
vote
1
answer
72
views
Print path to node in a 2D dictionary-structured graph
This question is slightly inspired by the Infinite Craft neal.fun game. I've managed to build a graph structure representing the alchemy additions. For example, { "Water": { "Fire":...
0
votes
1
answer
78
views
i want to make a sudoku solver
Here is the code:
public class sudoku {
public static void printSudoku(int sudoku[][]){
for(int i = 0; i < sudoku.length; i++){
for(int j = 0; j < sudoku[i].length; j++){
...
-1
votes
1
answer
76
views
can anybody explains the code and also recursion tree
i' am unable to make recursion tree
i saw many videos on this question but nobody is telling that after first left most leaf node completion
how the full recursion is made cause after there will two ...
1
vote
0
answers
67
views
Permutation brute force vs Selection brute force algorithm. When to use what?
Which to use and when? And what are the advantages of one approach over the other?
Permutation brute force is the one in which at each recursion step, we can enter any of the array elements. If ...
1
vote
1
answer
94
views
Why this approach to 8 Queens problem always returns 0
Here is my code.
using namespace std;
int ans = 0;
string il[8]; //squares which cannot contain any queens
void quen(int x, int y, bool table1[15],bool table2[15], bool table3[8]){
if(il[y][x]!='*'...
2
votes
1
answer
349
views
Counting paths in a matrix - Backtracking
Given an M*N matrix, I want to calculate the total number of possible paths from the upper-left cell (0,0) to the lower-right cell (m-1, n-1). Possible movements include up, down, right, and left. A ...
1
vote
0
answers
89
views
Error with inference (Backtrack algorithms and Job scheduling problem)
Im currently trying to program a Backtrack resolver using the mantaining arc consistency technique to do inferences on the domains of the other variables but Im stuck because it seems like I cant do ...
0
votes
1
answer
71
views
Exception has occurred: AttributeError 'str' object has no attribute 'variables' [duplicate]
I'm currently trying to program a backtrack resolver for an university project. It should resolve the job scheduling problem. I created 2 classes to do this: JobSchedulingProblem and Constraint.
class ...
0
votes
1
answer
80
views
Debug this backtrack algorithm for Leetcode 46: Permulation problrm?
I'm using the backtracking algorithm to solve the classical. Leetcode permutation problem.
Given an array nums of distinct integers, return all the possible permutations. You can return the answer in ...
1
vote
0
answers
50
views
Quite Hard DLX Optimizing JavaFX 3D Grid Manipulation for Performance
I'm working on a JavaFX project where I need to manage a 3D grid representing a Tetris-like game environment. My current implementation involves multiple nested loops and array manipulations, which I ...
2
votes
0
answers
127
views
Chess Backtracking solution for endgames
I have the following backtracking solution to chess endgames with D=3 figures on the board.
I should have obtained white king to move on Kc2, but I'm getting Qh3h8.
I have printed the print(board....
0
votes
0
answers
87
views
Dictionary values are not correctly updated during a while loop
I am currently attempting to write a Chronological Backtracking Algorithem for a sudoku solver. I have several positions stored as FlexiblePositions, which is represented as two integer in an array. ...
-1
votes
1
answer
52
views
Java program is not showing output even though it compiles with no error
I'm new to Java and I'm trying to build a sudoku solver programm using backtracking and an int[][] matrix.
Function usage:
isSafeSudoku()
this function checks whether the digit (from 1 to 9) is safe ...
-1
votes
1
answer
94
views
Finding permutations using backtracking
I am having trouble understanding a backtracking solution. I am new to algorithms and recursion. This is a leetcode problem. Leetcode 46.
class Solution:
def permute(self, nums: List[int]) -> ...
0
votes
1
answer
66
views
Generate Parenthesis - where am I going wrong
So here's the problem. For a given n we need to generate all valid parenthesis.
For e.g, for n=2 the possible output will be ()(), (()))
So I tried solving using recursion and backtracking.
I take two ...
1
vote
1
answer
188
views
Trying to understand how backtrancking works on php with this simple example
Im trying to understand how backtracking works on php. I understand the theory but then I dont understand the code.
This is an easy example:
function permute($nums)
{
$result = array();
...