backtracking

How to return all valid combinations of n-pairs of parentheses?

ぐ巨炮叔叔 提交于 2019-12-05 23:06:46
问题 def paren(n): lst = ['(' for x in range(n)] current_string = ''.join(lst) solutions = list() for i in range(len(current_string)+1): close(current_string, n, i, solutions) return solutions def close(current_string, num_close_parens, index, solutions): """close parentheses recursively""" if num_close_parens == 0: if current_string not in solutions: solutions.append(current_string) return new_str = current_string[:index] + ')' + current_string[index:] if num_close_parens and is_valid(new_str[

Algorithm of N queens

跟風遠走 提交于 2019-12-05 15:12:12
Algorithm NQueens ( k, n) //Prints all Solution to the n-queens problem { for i := 1 to n do { if Place (k, i) then { x[k] := i; if ( k = n) then write ( x [1 : n] else NQueens ( k+1, n); } } } Algorithm Place (k, i) { for j := 1 to k-1 do if (( x[ j ] = // in the same column or (Abs( x [ j ] - i) =Abs ( j – k ))) // or in the same diagonal then return false; return true; } The above code is for solving N Queens problem using backtracking.I think that it can place the first 2 queens of two rows in respective columns and then when it comes to 3rd row queen it can't be placed as no queen needs

How to Solve N-Queens in Scheme?

吃可爱长大的小学妹 提交于 2019-12-05 13:23:05
I'm stuck on the extended exercise 28.2 of How to Design Programs . I used a vector of true or false values to represent the board instead of using a list. This is what I've got which doesn't work: #lang Scheme (define-struct posn (i j)) ;takes in a position in i, j form and a board and ; returns a natural number that represents the position in index form ;example for board xxx ; xxx ; xxx ;(0, 1) -> 1 ;(2, 1) -> 7 (define (board-ref a-posn a-board) (+ (* (sqrt (vector-length a-board)) (posn-i a-posn)) (posn-j a-posn))) ;reverse of the above function ;1 -> (0, 1) ;7 -> (2, 1) (define (get-posn

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

痴心易碎 提交于 2019-12-05 05:07:00
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 major problem. When I start typing, and after some comma-separated numbers I add something that is not

Implementing a backtrack search with heuristic?

限于喜欢 提交于 2019-12-05 05:05:17
I'm getting quite interested in search algorithms and backtrack programming. For now, I have implemented Algorithm X (see my other post here: Determine conflict-free sets? ) to solve an exact cover problem. This works very well but I'm now interested in solving this with a more basic variant of backtracking. I just can't figure out how this can be done. The problem description is the same as before: Suppose you have a bunch of sets, whereas each set has a couple of subsets. Set1 = { (banana, pineapple, orange), (apple, kale, cucumber), (onion, garlic) } Set2 = { (banana, cucumber, garlic),

N-queen backtracking in Python: how to return solutions instead of printing them?

两盒软妹~` 提交于 2019-12-04 22:42:39
问题 def solve(n): #prepare a board board = [[0 for x in range(n)] for x in range(n)] #set initial positions place_queen(board, 0, 0) def place_queen(board, row, column): """place a queen that satisfies all the conditions""" #base case if row > len(board)-1: print board #check every column of the current row if its safe to place a queen while column < len(board): if is_safe(board, row, column): #place a queen board[row][column] = 1 #place the next queen with an updated board return place_queen

Problem with recursive backtracking

牧云@^-^@ 提交于 2019-12-04 22:00:18
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. //from previous post: Basically a set is passed to this method below, and a length of a bar is also passed

Why does using the u and i modifiers cause one version of a pattern to take ~10x more steps than another?

左心房为你撑大大i 提交于 2019-12-04 20:47:55
I was testing two almost identical regexes against a string (on regex101.com), and I noticed that there was a huge difference in the number of steps that they were taking. Here are the two regexes: (Stake: £)(\d+(?:\.\d+)?) (winnings: £)(\d+(?:\.\d+)?) This is the string I was running them against (with modifiers g , i , m , u ): Start Game, Credit: £200.00game num: 1, Stake: £2.00Spinning Reels:NINE SEVEN KINGKING STAR ACEQUEEN JACK KINGtotal winnings: £0.00End Game, Credit: £198Start... side note: the string/regexes are not mine, I just took them from elsewhere on SE and saw this behavior

Backtracking solution for programming exercise (fitting pipes)

旧巷老猫 提交于 2019-12-04 18:41:51
问题 I'm reviewing a programming problem from a local programming contest. You can download the problem here (pdf). It's in dutch but the pictures will help to understand it. You receive a m*m grid as input with some pipes and some missing spots (the questionmarks). The remaining pipes have to be placed in the grid so that they connect with the others. Each pipe is represented as a letter (see picture on page 2). The letter 'A' has value 1, 'B' has value 2, .. I tried solving it with backtracking

Can we solve N Queens without backtracking? and How to calculate and what will be the complexity of the backtracking solution?

流过昼夜 提交于 2019-12-04 18:11:58
I have tried solving this problem with backtracking and it prints all possible solutions. Two questions came up: 1. Can i implement n queen using other techniques? 2. Is it possible to make the code below print only the first solution and then terminate? My current code uses backtracking: n = 8 x = [-1 for x in range(n)] def safe(k,i): for j in xrange(k): if x[j] == i or abs(x[j] - i) == abs(k - j) : return False return True def nqueen(k): for i in xrange(n): if safe(k,i): x[k] = i if k == n-1: print "SOLUTION", x else: nqueen(k+1) nqueen(0) Note : I am interested in techniques that do not