backtracking

How to make Sudoku solver with backtracking algorithm go back?

点点圈 提交于 2019-12-22 10:49:50
问题 This weekend I worked on a Sudoku Solver (Ruby quiz) based on a backtracking algorithm. The sudoku is loaded in an array sudoku_arr of 81 integers (9x9 grid) where 0's are empty spots. There is a valid? method which checks if the sudoku_arr can be a valid sudoku. The official backtracking algorithm goes like this: try value on next empty spot, check if it is valid sudoku, if not increase value by 1 (upto 9), if valid go on and try first value on next spot, if not increase value of previous 0.

Browser tab stucks when my RegEx is executed and do not match the input

心不动则不痛 提交于 2019-12-22 05:07:59
问题 The problem is next. I created an input field which has validation and this is valid data: 1-12, 14, 16, 22, 25-35, 41, 49, 55-90 1230-1992, 2001-2099, 9931 1-2 13 1,3,4,5,6,10 all Basically, any combination of those numbers (ranges, comma-separated ranges, comma-separated number, spaces after commas, no spaces after commas, word: 'all') My RegEx: /^(([0-9]{0,4},?\s{0,})+([0-9]{1,4}-[0-9]{1,4}){0,},?\s{0,})+$|^(all)$|^([0-9]{1,4}-[0-9]{1,4}){0,},?\s{0,}$/ It works almost fine, there is only 1

CUDA: Stop all other threads

蓝咒 提交于 2019-12-21 05:39:23
问题 I have a problem that is seemingly solvable by enumerating all possible solutions and then finding the best. In order to do so, I devised a backtracking algorithm that enumerates and stores the best solution if found. It works fine so far. Now, I wanted to port this algorithm to CUDA. Therefore, I created a procedure that generates some distinct basic cases. These basic cases should be processed in parallel on the GPU. If one of the CUDA-threads finds an optimal solution, all the other

Max path triangle (Python)

我只是一个虾纸丫 提交于 2019-12-21 05:17:05
问题 I have a triangle with two-hundred rows, where I have to find the maximum distance to get from the top to the bottom of the triangle. 5 9 8 5 4 6 9 7 3 4 Here, the shortest distance would be 5+8+4+3=20. The maximum distance would be 5+9+5+9=28. I have a good idea of the algorithm I want to implement but I am struggling to turn it into code. My plan is: start at the 2nd to last row, add the maximum of the possible paths from the bottom row, and iterate to the top. For instance, the above

Recursive function to match a string against a wildcard pattern

亡梦爱人 提交于 2019-12-21 04:23:13
问题 So I've been trying to solve this assignment whole day, just can't get it. The following function accepts 2 strings, the 2nd (not 1st) possibly containing * 's (asterisks). An * is a replacement for a string (empty, 1 char or more), it can appear appear (only in s2) once, twice, more or not at all, it cannot be adjacent to another * ( ab**c ), no need to check that. public static boolean samePattern(String s1, String s2) It returns true if strings are of the same pattern. It must be recursive

How to calculate time complexity of backtracking algorithm?

允我心安 提交于 2019-12-20 10:08:28
问题 How to calculate time complexity for these backtracking algorithms and do they have same time complexity? If different how? Kindly explain in detail and thanks for the help. 1. Hamiltonian cycle: bool hamCycleUtil(bool graph[V][V], int path[], int pos) { /* base case: If all vertices are included in Hamiltonian Cycle */ if (pos == V) { // And if there is an edge from the last included vertex to the // first vertex if ( graph[ path[pos-1] ][ path[0] ] == 1 ) return true; else return false; } /

Determine whether a Sudoku has a unique solution

我与影子孤独终老i 提交于 2019-12-20 04:31:28
问题 I'm struggling with a backtracking algorithm to determine wether a Sudoku has a unique solution or if it has multiple Solutions. Here's the backtracking code i use: static boolean solve(int i, int j, int[][] cells) { if (i == 9) { i = 0; if (++j == 9) return true; } if (cells[i][j] != 0) // skip filled cells return solve(i+1,j,cells); for (int val = 1; val <= 9; ++val) { if (legal(i,j,val,cells)) { cells[i][j] = val; if (solve(i+1,j,cells)) return true; } } cells[i][j] = 0; // reset on

Sudoku Backtracking Non valid Sudoku

强颜欢笑 提交于 2019-12-20 03:33:16
问题 I created a Sudoku Backtracking solver and it works just fine, but now i want to give out an error if the sudoku cant be solved because it isnt valid for example if this sudoku is given: http://img5.imageshack.us/img5/2241/sudokugq.jpg what can i do to make my solving method give out an error if it isnt solveable? I always end up with Zeros or stuck in a loop. public void solve( int row, int col ) throws Exception { // Throw an exception to stop the process if the puzzle is solved if( row > 8

Constructing a randomised matrix with no duplicates but fixed partial input

人走茶凉 提交于 2019-12-20 01:09:54
问题 I´m facing a problem with constructing a randomised matrix where I partially already have values (that need to stay fixed - so no further randomisation there). Lets see: matrix should end up being 10 by 10 n <- 10 I do want my first rows to be the data I enter. e.g: row1<- c(1,4,7,6,5,3,2,8,9,10) row2<- c(10,7,3,2,1,4,5,9,8,6) row3<- c(9,2,4,3,8,7,10,1,6,5) To bild a matrix with 10 rows (and 10 columns) I combined those rows with samples (no replace because I want each number to be unique in

Prevent backtracking after first solution to Fibonacci pair

天大地大妈咪最大 提交于 2019-12-18 13:36:31
问题 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