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

How is backtracking used in a depth-first traversal?

Can anyone tell me in simple terms, how is backtracking used in a depth-first traversal? I am struggling to understand so I could use an example. Thanks.
-1
votes
1 answer

This is a sudoku solving program.I got recursive errors.I imported sys module and set the recursion limit to 1500,but still it shows errors

import sys sys.setrecursionlimit(1500) # the default recursion limit is 1000 def print_grid(arr): for i in range(9): for j in range(9): print(arr[i][j]) print('\n') def find_empty_location(arr, l): for i in…
-1
votes
1 answer

Why is my JAVA code for solving sudoku using backtracking not giving any solution?

This is a code in JAVA for solving the sudoku problem for any 9*9 sudoku grid using backtracking. It is not printing any output. I am not able to find the mistake in this. Please help. I have included one of the sample input int the main function in…
sambhav jain
  • 19
  • 1
  • 3
-1
votes
1 answer

RegularExpression does not backtrack on new line

I was trying to detect incorrectly (over-)intended code like that foo { bar { baz { with regular expressions and I found an interesting behavior. The regex I'm using is /^( *).+\n\1 {3,}/ and in VS Code (standard find&replace dialog) it…
kirilloid
  • 14,011
  • 6
  • 38
  • 52
-1
votes
3 answers

Get all possible combinations for a matrix in C++

We have a classroom. Our main goal is to make pairs of students to work together. How are we going to do it? Through a matrix. This matrix (n x n, n is pair) stores the level of 'preference' each student has with another one. For example, i is a…
alexhzr
  • 153
  • 1
  • 1
  • 12
-1
votes
1 answer

Knight's Tour Infinite loop

I wanted to test if I understand well the backtracking, so I tried the Knight Problem. But my code doesn't seem to work. It seem to do an infinite loop, so maybe my tracking of the path is not well executed. So I wanted to know what I miss in my…
user4699462
-1
votes
1 answer

1D N Queens Array (Python) Solve function

I know N_Queens is a very well covered topic, but i've to find a good python solution which uses a 1D array (and importantly, solves via filling the 1d array and then via function transforms this into a 2d array). I've got so far as completing the…
-1
votes
1 answer

how to use backtracking recursion in java

I'm trying to write a method that get two strings and checks if its able to substring them to get the same string. for example if s1 = "abc" and s2 = "abbbc" it will return true. I want to do it in BackTracking recursion. here is my code that isn't…
Ohadyk
  • 29
  • 4
-1
votes
1 answer

N-Queen Beginner Backtracking

To practice what I've learned about backtracking algorithms, I'm trying to solve the N-Queen problem. I've written some functions to check if a move is legal, but I can't see how to implement those using backtracking. bool manger_ligne (int…
user4699462
-1
votes
1 answer

Two corners coin game in java

The 2 corners coin game receive an array. The goal of the game is accumulate the number of points (values of the elements in the array) the most. you can take points only from the 2 corners of the array. There are 2 conditions for the game: 1) The…
AnnaLA
  • 155
  • 1
  • 11
-1
votes
2 answers

How many ways to place n DNA chains

The problem is to find how many complete structures can be formed using the DNA chains. The rule is that the first letter of the new part has to be the same as the last letter of the previous chain. On the first row you are given an integer: the…
Quiti
  • 132
  • 8
-1
votes
1 answer

finding all k subsets using recursion

I'm trying to find a way to make a recursive algorithm that will give all the k-length subsets of a set of numbers (0 -> n), but I cannot send a list to the function as an argument. Eventually I want to return a list of lists I thought on starting…
Eize
  • 11
  • 3
-1
votes
1 answer

How do I optimize my backtracking solution for matching regexes?

Here is a problem i was trying to solve on LeetCode: Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including…
mettleap
  • 1,390
  • 8
  • 17
-1
votes
1 answer

Backtracking to find all possbile paths

I got this question in a recent interview, Given a gird of letter and a word find all the possible paths that make a word Directions: horizontal, vertical or diagonal to any coordinate with a distance 1 Constraint: Each path has to be a unique set…
-1
votes
1 answer

Chess knight tour using recursive backtracking

My chess knight tour code using recursive-backtracking is not working. #include using namespace std; // goal: knight tour #define N 8 void printArray(int board[N][N]) { for(int i = 0; i < N; i ++) { for(int j = 0; j < N;…
1 2 3
99
100