Questions tagged [backtracking]

Backtracking is a general algorithm for finding solutions to some computational problem, that incrementally builds candidates to the solutions.

Backtracking is an important tool for solving constraint satisfaction problems, such as crosswords, verbal arithmetic, Sudoku, and many other puzzles. It is often the most convenient (if not the most efficient) technique for parsing, for the knapsack problem and other combinatorial optimization problems. It is also the basis of the so-called logic programming languages such as Icon, Planner and Prolog. Backtracking is also utilized in the (diff) difference engine for the MediaWiki software.

1606 questions
-1
votes
1 answer

Solving N-Queens with Backtracking but in different way

I am a 16 years old guy and recently I was solving n-queens problem but my code is giving different answers.Basically my code's intention is to put a queen on a specific row and then mark the left diagonals as well as right diagonals and columns…
Atef Nazi
  • 40
  • 8
-1
votes
1 answer

How does backtracking work in this "Generate Parentheses" question?

I put the code below in a debugger and saw that for a base case of n=2 when the function reaches line 7, the return statement, it goes back and pops the last 3 parentheses. Why is this, I thought that the return statement is supposed to exit the…
Noel
  • 23
  • 4
-1
votes
1 answer

How does this Sudoku backtracking recursive algorithm trigger backtracking?

let row = this.findEmpty(puzzleString)[0]; let col = this.findEmpty(puzzleString)[1]; let i = this.findEmpty(puzzleString)[2]; if(!this.findEmpty(puzzleString)) return puzzleString for(let num = 1; num < 10; num++){ …
-1
votes
1 answer

C programming - Sodoku solution (backtracking)

the Question is: to get from the user sodoku board and if there is a solution to print it, if not to print no solution! the solution of the soduko: two identical numbers mmust not appear on the same line; two identical numbers must not appear in the…
Y.D
  • 43
  • 4
-1
votes
2 answers

Recursion Question with Valid Parenthesis

I am trying to understand this solution for generating all valid parentheses. The part I don't understand is how the recursion can find ALL of the different combinations. When I debug through the code, I put a watch on the integers "left" and…
-1
votes
1 answer

Create a backtracking program that returns all P-digit numbers such that every 3 consecutive digit sums up to S

As a problem, we're tasked to write up a recursive backtracking program that prints all numbers with P digits such that every 3 consecutive digit of that number sums up to S. I cannot formulate a recursive backtracking approach yet, but I was able…
lambduh
  • 43
  • 11
-1
votes
1 answer

Problem in Knights Tour using backtracking

I am getting a infinite loop when I try and run my solution for the Knights Tour problem using Backtracking My Solution Code: Link: https://ideone.com/Ud92vF code: #include using namespace std; bool valid(int arr[8][8],int r,int…
-1
votes
2 answers

Why does only backtracking work in this scenario?

I am solving this question on LeetCode.com: In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the…
user8305079
-1
votes
1 answer

Sudoku Solver Program - no change in output

solveSudoku function is called from main() function. I have written the following function for solving sudoku : bool isFull(vector>& board){ for(int i = 0 ; i < board.size(); i++){ for(int j = 0 ; j <…
Dark Night
  • 33
  • 1
  • 6
-1
votes
1 answer

Golang pointer to a slice

I was recently doing a backtracking question and came across this weird scenario which I am not able to understand. This question is related to listing out all the possible subsets possible from the given array. Here is the code snippet: func…
-1
votes
2 answers

How do I return Combinations using recursion in Python

I want to print the combinations of all the elements of a list. But every time my codes results in a list of empty lists. What is want is input = [1,2,3] Output = [[3],[1],[2],[1,2,3],[1,3],[2,3],[1,2],[]] What I am getting is…
Mohamed Imran
  • 651
  • 7
  • 20
-1
votes
2 answers

How do I recursively enumerate all subsets adding to a given sum?

When I try to get the output, it shows same solutions with same element for few times before moving on to another one. I want to get different solutions from the array that is equal to sum For instance, Solution 1 = 14, 8 Solution 2 = 14, 5, 3…
-1
votes
2 answers

Sudoku Recursive Backtracking in Go

I am trying to solve a sudoku puzzle in Go using a recursive backtracking algorithm. I created helper functions that check if a certain row, column, or block are valid (i.e no repeated values), as well as a function to print out the current state. I…
user3196284
-1
votes
1 answer

Backtracking Sudoku only sort of works in Python 3

I'm struggling to make this work. It gets a few digits right, but I don't know where the problem is. I've read various other codes and while I understand how they work, I don't know where the problem with my code is. I've tried multiple things and…
Simsat
  • 81
  • 1
  • 5
-1
votes
2 answers

Why this two backtracking algorithm have same outputs?

I have made two implementations of a backtracking algorithm to solve this challenge on Codewars ( https://www.codewars.com/kata/5a6b24d4e626c59d5b000066 ) . My problem is that I grasp why the first implementation works but not why the second one . I…
Tortar
  • 625
  • 5
  • 15
1 2 3
99
100