Questions tagged [backtracking]
Backtracking is a general algorithm for finding solutions to some computational problem, that incrementally builds candidates to the solutions.
backtracking
1,651
questions
0
votes
0
answers
16
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 ...
0
votes
1
answer
69
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
47
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
62
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 ...
4
votes
1
answer
125
views
Can Knuth's "Algorithm B" be written without goto statements or recursion?
Though I've already implemented 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 ...
1
vote
1
answer
113
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 ...
0
votes
1
answer
70
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
21
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
76
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
53
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
53
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
36
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
65
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
28
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
90
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; //Пример ...