backtracking

Brute force Sudoku algorithm [duplicate]

隐身守侯 提交于 2019-12-11 11:10:12
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: Sudoku algorithm, brute force For several days I have tried to write a brute force algorithm for solving sudoku, my problem is that I never realy get the algorithm to work 100 %, can someone please direct me and give some help ? The Algorithm is located in Square class, recursive function. public abstract class Square { private Square next; private Box box; private Row row; private Columne columne; private int

Knight's Tour GUI in Processing

╄→尐↘猪︶ㄣ 提交于 2019-12-11 07:12:13
问题 I am working on a knight's tour problem with basic gui,i want to take user input in the two text fields which make up the (x,y) from user and then in one text box i print if solution is posiible and in the other i write the path adopted by the knight.My algorithm works fine and i have problem in gui.i have given a few default values to (x,y) for that i get correct output .But when i change the value of (x,y) in the textfield,no change occurs.this is the main file ,there is another event

How to use backtrack to increment a variable infinitely in Prolog

谁都会走 提交于 2019-12-11 06:38:32
问题 I am currently reading a Prolog book and I am stuck on one of the challenge exercises. I am meant to create a predicate with one argument. When this argument is a variable, it will return the following with backtracking, and X will continue to increment with no limit. X = 0, X = 1, X = 2, X = 3, X = ... I made the simple predicate below which backtracks through 0-2 but I can't figure out a way to make it continue infinitely. backtracking_exercise(X) :- X = 0; X = 1; X = 2. I was considering

Find min of list using fail, backtracking Prolog

為{幸葍}努か 提交于 2019-12-11 03:38:04
问题 I want to calculate the minimum of list, using fail which causes to backtrack. How do i change min(Min, X, Min), to make it work. %min(X, A, B) X is the min of A, B min(X, X, Y) :- X =< Y. min(Y, X, Y) :- Y < X. member(X, [X|_]). member(X, [_|Ys]) :- member(X,Ys). program :- Min is 1000, (member(X, [1, 2, 3, 4]), writeln(X), min(Min, X, Min), %This is wrong ! fail; writeln(Min), true). My previous working code for calculating min solve([Head|Rest], Ans) :- solve(Rest, Till), min(Ans, Head,

Recursive backtracking in Java for solving a crossword

北城以北 提交于 2019-12-10 16:34:51
问题 I need to solve a crossword given the initial grid and the words (words can be used more than once or not at all) . The initial grid looks like that: ++_+++ +____+ ___+__ +_++_+ +____+ ++_+++ Here is an example word list: pain nice pal id The task is to fill the placeholders (horizontal or vertical having length > 1) like that: ++p+++ +pain+ pal+id +i++c+ +nice+ ++d+++ Any correct solution is acceptable, and it's guaranteed that there's a solution. In order to start to solve the problem, I

Backtracking algorithm for prime sequence

偶尔善良 提交于 2019-12-10 12:05:59
问题 I'm having trouble doing backtracking and not sure if what I'm doing is backtracking exactly. I have n amount of integers for my example it will be [5,6,7,8]. From those integers I need to find if a prime sequence exists and if it does display it. The prime sequence for this example is 7,6,5,8 since 7+6=13 6+5=11 5+8=13 To get the answer I can go through each n and then try to see if its a prime sequence. Starting at 5: 5,6[7,8] 5,6,7[8] Since 7+8 isn't prime. Go onto next integer. Since 5+7

Is backtracking absolutely necessary for cycle detection using DFS in directed graph?

痞子三分冷 提交于 2019-12-10 10:53:27
问题 I came across this SO post where it is suggested that cycle detection using DFS in a directed graph is faster because of backtracking. Here I quote from that link: Depth first search is more memory efficient than breadth first search as you can backtrack sooner. It is also easier to implement if you use the call stack but this relies on the longest path not overflowing the stack. Also if your graph is directed then you have to not just remember if you have visited a node or not, but also how

Simplified Travelling Salesman in Prolog

大憨熊 提交于 2019-12-10 02:33:59
问题 I've looked through the similar questions but can't find anything that's relevant to my problem. I'm struggling to find an algorithm or set of 'loops' that will find a path from CityA to CityB , using a database of distance(City1,City2,Distance) facts. What I've managed to do so far is below, but it always backtracks at write(X), and then completes with the final iteration, which is what I want it to do but only to a certain extent. For example, I don't want it to print out any city names

Problem with recursive backtracking

房东的猫 提交于 2019-12-10 00:10:54
问题 Hey guys, recently posted up about a problem with my algorithm. Finding the numbers from a set which give the minimum amount of waste Ive amended the code slightly, so it now backtracks to an extent, however the output is still flawed. Ive debugged this considerablychecking all the variable values and cant seem to find out the issue. Again advice as opposed to an outright solution would be of great help. I think there is only a couple of problems with my code, but i cant work out where. /

Python permutations recursive

依然范特西╮ 提交于 2019-12-08 12:11:45
问题 I'm trying to solve a problem using backtracking and I need the permutations of numbers for it. I have this basic algorithm that does it but the problem is... the results don't come in the normal order. def perm(a,k=0): if(k==len(a)): print(a) else: for i in range(k,len(a)): a[k],a[i] = a[i],a[k] perm(a, k+1) a[k],a[i] = a[i],a[k] Example: for [1,2,3] the normal results would be: [1,2,3] [1,3,2] [2,1,3] [2,3,1] [3,1,2] [3,2,1] Whereas this algorithm will interchange the last 2 elements. I