backtracking

Python permutation using backtracking

断了今生、忘了曾经 提交于 2019-12-12 04:40:42
问题 I'm just trying to learn permutation using backtracking. I've written the following code but it stops after first output. def permutations(str_in, soFar): if len(str_in) != 0: for c in str_in: soFar.append(c) temp_str = str_in temp_str.remove(c) print temp_str, soFar permutations(temp_str, soFar) else: print soFar inp = "ABCD" str_in = list(inp) permutations(str_in, []) This is the output I'm getting for this: ['B', 'C', 'D'] ['A'] ['C', 'D'] ['A', 'B'] ['D'] ['A', 'B', 'C'] [] ['A', 'B', 'C'

How does Prolog answer this query?

你。 提交于 2019-12-12 04:07:28
问题 likes(alice, sports). likes(alice, music). likes(carol, music). likes(david,animals). likes(david,X) :- likes(X,sports). likes(alice,X) :- likes(david,X). ?- likes(alice,X). I've been trying to learn prolog an few days now, and when I attempted this question, I realised that I don't completely understand when the variables are instantiated and used. The initial goal is : likes(alice , X) . After that, the next goal to prove is likes(david , X) ? Then is it likes(X, sports) . Then does X

Longest Common Substring using Recursion and DP

时光毁灭记忆、已成空白 提交于 2019-12-12 02:32:38
问题 I'm trying to find the Longest Common Substring of two strings using Recursion and DP. Please note that I'm not referring to Longest Contiguous subsequence. So, if the two strings were String s1 = "abcdf"; String s2 = "bzcdf" Longest Common Substring == "cdf" (not "bcdf"). Basically they have to be continuous elements I am trying to do this using recursion and backtracking. However, the problem is that if I use a recursion such as below, the +1 are added upfront in a frame, that is higher up

ambigous call on recursive sudoku_backtracker function.

[亡魂溺海] 提交于 2019-12-12 02:02:31
问题 This is my program to solve a sudoku puzzle by using a backtracking algorithm. The program will recursively call itself until it is either solved or if it is unsolvable. The problem is that when I run it the compiler says that the sudoku_backtracker() function call in line 19 of sudoku_solver.cpp is ambiguous. Can someone please explain to me why it says that and how can I fix it. If there are other problems I would also appreciate the help. Thanks alot. #include <iostream> #include <string>

Stack overflow error bruteforcing a magic square. Any possible solution?

99封情书 提交于 2019-12-12 00:28:13
问题 Here my problem, for an exercise I need to generate a magic square by bruteforcing it in backtracking. I thought it could be useful to allocate the matrix as a vector and a function that changes the coordinates. As you can imagine even with a 3x3 magic square it gave me a stack overflow problem. Debugging it i discovered it happen, more or less, at half of the generating, more precisely where the function chk_magic(int *m, int n) call change_coord(i, j, m, n); . Here it is the entire code,

Why does Prolog does not backtrack on comparison?

让人想犯罪 __ 提交于 2019-12-11 14:47:30
问题 I would expect that the following should always be true if a comparison backtrack, right? unless it goes into an infinite loop! ?- Y=2 , random:random(1,3,X), X =\= Y. Y = 2, X = 1. ?- Y=2 , random:random(1,3,X), X =\= Y. false. but I got false! In general, my question is why doesn't comparison backtrack? Thanks for all the answers. My confusion seemed to come primarily from my expectation of random keep generating new-random numbers, so I confused that comparison was not backtracking,

recursive Permutation of a 3 Digit Number

无人久伴 提交于 2019-12-11 13:05:54
问题 I am working on finding All permutations of a 3-Digit Number recursively. I am tired with making up the following permutation Method: static int a = 1; static int b = 2; static int c = 3; static int aCount; static int bCount; static int cCount; static void perm(int a, int b, int c) { Console.WriteLine("( {0}, {1}, {2} )", a, b, c); // (1,2,3 ) if (aCount < 1 && bCount<1 &&cCount<1) { aCount++; perm(a, c, b); } else if (aCount==1 && bCount < 1 && cCount<1) { bCount++; perm(b, a, c); } else if

Finding all the subsets that sum n via backtracking

冷暖自知 提交于 2019-12-11 12:51:10
问题 I want to find all the integer subsets that sum n via backtracking For example for the integers: 1 2 3 4 5 6 7 and n = 7 I want to ouput 1 2 4 1 6 2 5 3 4 7 I think that I should pass the position in the integer array that I'm evaluating as argument, but I'm stuck writing the rest of the logic. My code so far: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import

How to solve Sudoku by backtracking and recursion?

99封情书 提交于 2019-12-11 12:31:57
问题 the reason why I am creating this new thread instead of just reading the answers to this particular question that have been given before is that I feel I just don't fully understand the whole idea behind it. I can't seem to get my head around the whole backtracking concept. So I need to fully understand backtracking and then solve the particular sudoku problem. What I understand so far is that backtracking is a technique to go back, in (e.g.) a recursive flow if one discovers that decisions

Peg Solitaire Solutions with Backtracking

末鹿安然 提交于 2019-12-11 12:15:51
问题 I'm currently trying to write a program that will be able to find the solutions for the game peg solitaire using back tracking. My program takes in a txt file that contains a starting board. Everything is done except for the solve() function where the actual back tracking part is contained, this is proving conceptually really difficult for me. I've been working on it on a piece of paper for a while but I don't think I'm making much progress. Any help would be greatly appreciated. Sample txt