ProjectRoot is working directory for Eclipse. Move file above src directory i.e. to project root (Running from eclipse) openFile = new File("problem1.txt"); Provide path from src or bin to file location (Running from eclipse): openFile = new File("src/superbase/problem1.txt"); ...

I'm only going to comment on findStart for now. There are two things wrong with findStart: findStart is recursively called on every adjacent cell. Unfortunately, the neighbouring cell of any neighbour is the cell itself. The function never checks if you can actually walk on a given cell (I assume...

algorithm,math,backtracking,equation-solving

I'd start with the equals sign. Pick a possible location for that, and split your sequence there. For left and right side independently, find all possible results you could get for each, and store them in a dict. Then match them up later on. Finding all 226 solutions took my...

algorithm,maps,dijkstra,shortest-path,backtracking

Actually the matrix isn't required. The graph can just aswell be generated on runtime. The image can be converted into a map of walls and open spots by simply converting it into a two-colored image, where one color represents walls. This would look like this for your requirements: define node:...

c++,algorithm,dynamic-programming,backtracking,dfs

Given a NxN grid, let ways[i][j] = number of possible paths from grid[0][0] to grid[i][j] initialize grid[0][0] = 1 if grid[i][j] is dead, ways[i][j] = 0 else ways[i][j] = ways[i-1][j] + ways[i][j-1] (but be careful with the edge) An example: grid:(1 means dead) ways: 0 0 1 0 0 1...

java,user-interface,applet,processing,backtracking

There are some problems with above constructions. You do recursive move() method inside draw() method. But in Processing draw() is called many times in each second by animation thread. Common desing for such cases is: you should have variables that hold application state (logical state) what your draw() method draws...

OK, I finally solved it using lists in lists for saving Answers and evaluations. Then I just expand those lists and use it for building more precise solution.

The need for back-tracking in the knights tour problem is critical. The fact that you have not implemented your back-tracking in your code is the main reason why it does not work. To fix it you must at least clear the position at the end of your method. I.e when...

c++,algorithm,backtracking,n-queens

As long as your row < 8 and col < 8 the output you are getting is correct. The expected output you have given is wrong. When row = 8 or col = 8 the array index goes outof bound and tries to assign the value to some memory which...

You seem to be using parameters past_i and past_j to avoid doubling back, but when you recurse, you do not pass the coordinates of the previous position as is required for such a scheme to be effective. For example, it appears that this ... temp = traverse(M, n, posi -...

Basically you want to split the problem into "Can I make a word starting with this letter?" and then recursively descend the Trie to solve for "If I start with this letter can I make a word starting at this node using a subset of the rest of these letters?",...

I suggest you use the tracer to look at the proof tree (which some consider a bad practice, but it does help understand the execution model if you have difficulties with that). So there you go (replacing the not/1 with \+/1): ?- trace(r/1), trace(s/1). true. [debug] ?- r(X, Y), s(Y,...

An open square is a square that does not contain # or *, you're only checking for #. The * check is required to not end up in infinite recursion (ie going back the same way you just came) Changing your code to; static boolean mazeTraversal(char m[][], int i, int...

algorithm,graph,path,backtracking

Start at the top. For each node, pick the maximum of its parents (the nodes above connected to it) and add its own value. Then, in the last row, pick the maximum. This will just give you the value of the longest path, but you could easily get the actual...

recursively check if a list could be divided to two sublists with the same sum You can easily implement the greedy partition algorithm using recursion: def _greedy_part(r, sa, la, sb, lb): if not r: return sa == sb, la, lb; if sb < sa: # swap both lists in...

algorithm,runtime-error,sudoku,backtracking,scilab

Well, one of the easiest way to program a Sudoku solver (not the most efficient) could be to solve each cell with all the possible options recursively (which could be similar to the "Backtracking" algorithm) until a full answer is found. Another options (I would say it's better) is to...

As the error message suggests, you cannot subscript (square bracket) a map object. Maps are a type of iterable in python, meaning the only way to get data out of them is to iterate through them one element at a time. If you want to subscript it, you need to...

algorithm,runtime-error,sudoku,backtracking,scilab

The main issue is this line: solve(board) which should be S = solve(board) A recursive function must provide two things: a way to delegate a modified task to another instance of itself, and a way to return the result to the instance that called it. You didn't have the second...

algorithm,set,combinatorics,backtracking

You can try the recursive answer if your set is not to big (or else use stack) : The principle is the following, you have a function that give back : rec_func(SET) = List of List of Set And work as follow : rec_func(SET) = if SET = {empty}: //...

c,algorithm,recursion,dynamic-programming,backtracking

Don't place a '1' if the previous position is also '1'. For example: if (count == 0 || target[count-1] != '1') { target[count] = '1'; foo(size, count+1); } ...

python,recursion,sudoku,backtracking

There is room for improvement using list comprehensions and some built in functions. For instance instead of: rida = line.split(" ") for i in range (0, 9): if rida[i] != "-": rida[i] = int(rida[i]) else: rida[i] = 0 You can use list comprehensions and do: rida = line.split(" ") rida...

"Backtracking" is a term that occurs in enumerating algorithms. You built a "solution" (that is a structure where every variable is assigned a value). It is however possible that during construction, you realize that the solution is not successful (does not satisfy certain constraints), then you backtrack: you undo certain...

You always "return" a value in Prolog; this value is either success, of failure Yes, it is possible to "continue the execution"; however, this is not the most useful way of looking at it (see below) Yes, it is, but your question is not clear enough to answer the...

The cut-fail thing is an antipattern that's misleading you. It would be much better to say something in the original like this: bird(eagle). bird(penguin). flightless(penguin). can_fly(X) :- bird(X), \+ flightless(X). See @boris's answer for an excellent and much more detailed discussion of this problem. So, you have this database. I'm...

java,parsing,backtracking,top-down

I am bit rusty on my CS classes: but the following code worked for me: public static boolean fullCheck(char[] toTest, char[][] g) { int val = checkOnAll(0, toTest, g); if (val == toTest.length) { return true; } return false; } public static int checkOnAll(int charPos, char[] toTest, char[][] g) {...

recursion,prolog,backtracking,findall

The reason you get different behaviours when querying run. and run2. is because the goal sumValue('M1', _, F) is being satisfied twice: ?- sumValue('M1', _, F). F = 3 ; F = 0. I would also recommend you to use format/2 instead of all those write/1 predicates. It helps for...

Explanation Nested group doesn't automatically causes catastrophic backtracking. In your case, it is because your regex degenerates to the classical example of catastrophic backtracking (a*)*. Since \s in optional in ^([a-zA-Z0-9'-]+\s?)*$, on input without any spaces but has characters outside the allowed list, the regex simply degenerates to ^([a-zA-Z0-9'-]+)*$. You...

The problem is that you don't consider that a person who has already remained on the other side can in the future return with the flashlight. In the example case person number 2 returns after some time. Basically not always a person from the i,j pair will return with the...

optimization,recursion,backtracking

If I've understood your example correctly this is not a genuine example of a recursive algorithm. Since the function only calls itself at most once I don't think there can be much efficiency impact. You are basically right that a different function could be used, or the same function called...

c#,algorithm,combinations,subset,backtracking

As suggested by others you're never reaching the line that allows you to increment the counter. Try this algorithm that gives you all "xxx" combinations (000 to 999) and also avoids the generation of duplicates (Sayse's solution generates duplicates): static void Combo(int a, int b, int c) { if (a...

Here's a trivial Python transcription from http://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/ : def swap(a, i, j): a[i], a[j] = a[j], a[i] def permute(a, i, n): if i == n: print(a) return for j in range(i, n+1): swap(a, i, j) permute(a, i+1, n) swap(a, i, j) # backtrack def main(): a = list('ABC') permute(a, 0,...

java,recursion,stack-overflow,infinite-loop,backtracking

In your function findChange, int coin is an index. You are using the index in the line: answerCoins.add(coin); instead of the values of the coinTypes list, so your first try is with a coin of 0 cents, hence the infinity loop, try changing to: answerCoins.add(coinsType.get(coin)); You also need to change...

java,recursion,multidimensional-array,backtracking,maze

A quick point on style, to save some typing later: maze[][], solution[][] and n are all effectively global, and do not change between recursive calls (maze and solution are just passed as references to the same arrays, and n never changes). This is purely style, but you can write this...

You are settings visited[x][y]=True to true at the end of your algorithm. It has to be set after you check you've bin there. I also made a couple of enhancements for your code: N = 5 # This way you can test it with 5 or 6 and even 100...

c,function,boolean,return,backtracking

In given example what is different between return 1 and return 0? Judging by the following block code if(solve(a)) { return 1; } it seems that a return value of 0 indicates not solved yet and a return value 1 indicates solved. I us int fun() in place bool...

java,algorithm,recursion,multidimensional-array,backtracking

It works if you replace fun(i+1,j,metric); with fun(i+1,0,metric); ...

prolog,backtracking,aggregates,prolog-setof,meta-predicate

What is the idiomatic approach to this class of problems? Is there a way to simplify the problem? Many of the following remarks could be added to many programs here on SO. Imperative names Every time, you write an imperative name for something that is a relation you will...

It is possible to formulate one of the multiple possible original states. Start with the final solution (all numbers are present) Remove one number (chosen randomly or not) Check if this number can be found back given the current state of the board (you already have a solver, it should...

java,algorithm,data-structures,permutation,backtracking

So if I'm reading the question correctly, you want to find the kth permutation, preferrably without using BigIntegers, provided k is not large enough to require a BigInteger. If we look at the sequence 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3...

Not thoroughly tested, but this hopefully will help: import Data.Function (on) import Data.List (minimumBy, delete) import Control.Monad (guard) type Word = String type Path = [String] wordF :: [Word] -> Word -> Word -> Path wordF words start end = start : minimumBy (compare `on` length) (generatePaths words start end)...