Skip to main content
The 2024 Developer Survey results are live! See the results

Questions tagged [backtracking]

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

backtracking
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 ...
M3mber's user avatar
  • 43
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" ...
Tim's user avatar
  • 7,352
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 ...
data_geek's user avatar
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 ...
Ashna kohli's user avatar
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 ...
MaroonSphinx's user avatar
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 ...
vikram sah's user avatar
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 ...
MathMan's user avatar
  • 227
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 ...
Alex's user avatar
  • 1
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 = [[...
MathMan's user avatar
  • 227
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&...
MathMan's user avatar
  • 227
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 ...
GlidingSwords997's user avatar
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 ...
lambda's user avatar
  • 25
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 ...
Cardstdani's user avatar
  • 5,184
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'; ...
Astitva Shukla's user avatar
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; //Пример ...
MrLeyt1125's user avatar

15 30 50 per page
1
2 3 4 5
111