backtracking

Minimum number of clicks to solve Flood-It-like puzzle

╄→尐↘猪︶ㄣ 提交于 2019-11-30 12:56:05
I have grid N × M in which each cell is coloured with one colour. When the player clicks on any cell of the grid of colour α, the cell in the top-leftmost corner of the grid, of colour β, receives the colour α, but not only it: all those cells which are connected to the source by paths which use only the colours α or β also receive the colour α. The connection between cells should be considered only in the horizontal and vertical directions to form the paths. For example, when the player clicks on the cell highlighted in the figure to the left, the grid receives the colouring of the figure to

Finding the numbers from a set which give the minimum amount of waste

烂漫一生 提交于 2019-11-30 12:34:57
A set is passed to this method below, and a length of a bar is also passed in. The solution should output the numbers from the set which give the minimum amount of waste if certain numbers from the set were removed from the bar length. So, bar length 10, set includes 6,1,4, so the solution is 6 and 4, and the wastage is 0. Im having some trouble with the conditions to backtrack though the set. Ive also tried to use a wastage "global" variable to help with the backtracking aspect but to no avail. SetInt is a manually made set implementation, which can add, remove, check if the set is empty and

How do I resolve the “Crypt Kicker” exercise proposed in “Programming Challenges (The Programming Contest Training Manual)”?

≡放荡痞女 提交于 2019-11-30 11:32:18
"Programming Challenges (The Programming Contest Training Manual)" is probably one of the nicest exercises book on algorithms. I've resolved the first 11 exercises, but now I am stuck with "Crypt Kicker" problem: Crypt Kicker A common but insecure method of encrypting text is to permute the letters of the alphabet. In other words, each letter of the alphabet is consistently replaced in the text by some other letter. To ensure that the encryption is reversible, no two letters are replaced by the same letter. Your task is to decrypt several encoded lines of text, assuming that each line uses a

Prevent backtracking after first solution to Fibonacci pair

北战南征 提交于 2019-11-30 10:08:19
The term fib(N,F) is true when F is the N th Fibonacci number. The following Prolog code is generally working for me: :-use_module(library(clpfd)). fib(0,0). fib(1,1). fib(N,F) :- N #> 1, N #=< F + 1, F #>= N - 1, F #> 0, N1 #= N - 1, N2 #= N - 2, F1 #=< F, F2 #=< F, F #= F1 + F2, fib(N1,F1), fib(N2,F2). When executing this query (in SICStus Prolog), the first (and correct) match is found for N (rather instantly): | ?- fib(X,377). X = 14 ? When proceeding (by entering ";") to see if there are any further matches (which is impossible by definition), it takes an enormous amount of time (compared

Find all possible dominoes chains with recursion and backtracking [closed]

谁都会走 提交于 2019-11-30 07:42:50
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed last year . I'm working on a challange where I need to find all possibilities for linear chains of dominoes tiles. I understand the principle of recursion but not how to translate it to code. If someone could maybe explain the problem (solution) in simple steps, which I could then follow and

How to delete the last element from an array?

人走茶凉 提交于 2019-11-30 02:52:09
问题 Now I'm working with the recursive backtracking,my assignment is to find the longest path in the maze,the mass is presented as the field covered with the coordinates,and the coordinates of the walls are sore in the file. I have made a parser to parse the input file and build the walls,but I have also stored this coordinates in the array of an object type Coordinate,to check whether it is possible to move the next piece of the "snake" on the next field,then I have created this method,now I

How to find the first solution only with this backtracking

纵饮孤独 提交于 2019-11-30 00:02:36
问题 i'm trying to write a Sudoku solver which will return only the first possible solution. i managed to print all possible solutions with void methods but i can't stop on the first find. i know the preferred way is to switch to boolean methods and return true up the tree - but i can't find the right way to write it. any way i tried always give compilation errors ( method must return boolean ). public boolean recursiveSolve(int line, int column) { if(line == N) // N is the board size (9) return

Minimum number of clicks to solve Flood-It-like puzzle

ぐ巨炮叔叔 提交于 2019-11-29 18:11:27
问题 I have grid N × M in which each cell is coloured with one colour. When the player clicks on any cell of the grid of colour α, the cell in the top-leftmost corner of the grid, of colour β, receives the colour α, but not only it: all those cells which are connected to the source by paths which use only the colours α or β also receive the colour α. The connection between cells should be considered only in the horizontal and vertical directions to form the paths. For example, when the player

Recursive solution to Sudoku generator

流过昼夜 提交于 2019-11-29 10:23:43
I'm trying to code an algorithm that creates a legal Sudoku board in either Java or Javascript. Neither work, and I'm not entirely sure why. Essentially, the problem in both programs is that either x or y is getting incremented more than it should (skipping the square). I can't for the life of me figure out how this is happening. I can provide the HTML that completes the JS solution if need be. My best guess is it has to do with how I've created a stack using recursion, but as far as I can tell, it should work. In my old code there was an incorrect for loop, I'm aware of this. I pasted an old

Find all possible dominoes chains with recursion and backtracking [closed]

自闭症网瘾萝莉.ら 提交于 2019-11-29 05:22:55
I'm working on a challange where I need to find all possibilities for linear chains of dominoes tiles. I understand the principle of recursion but not how to translate it to code. If someone could maybe explain the problem (solution) in simple steps, which I could then follow and try to code them. Example: Tiles : [3/4] [5/6] [1/4] [1/6] Possible chain : [3/4]-[4/1]-[1/6]-[6/5] It's allowed to flip the tiles. (switching the numbers) The process is very simple: you start with a collection of dominoes D, and an empty chain C. for each domino in the collection: see if it can be added to the chain