backtracking

Solving crosswords

落爺英雄遲暮 提交于 2019-12-18 11:56:33
问题 I have a crossword puzzle and a list of words which can be used to solve it (words can be placed multiple times or not even once ). There is always a solution for the given crossword and word list. I searched for clues on how to solve this problem and found out that it is NP-Complete. My maximal crossword size is 250 by 250, the maximal length of the list (amount of words which can be used to solve it) is 200. My goal is to solve crosswords of this size by brute force/backtracking, which

Using recursion and backtracking to generate all possible combinations

巧了我就是萌 提交于 2019-12-17 23:37:52
问题 I'm trying to implement a class that will generate all possible unordered n-tuples or combinations given a number of elements and the size of the combination. In other words, when calling this: NTupleUnordered unordered_tuple_generator(3, 5, print); unordered_tuple_generator.Start(); print() being a callback function set in the constructor. The output should be: {0,1,2} {0,1,3} {0,1,4} {0,2,3} {0,2,4} {0,3,4} {1,2,3} {1,2,4} {1,3,4} {2,3,4} This is what I have so far: class NTupleUnordered {

Baktracking function which calculates change exceeds maximum recursion depth

让人想犯罪 __ 提交于 2019-12-14 03:45:20
问题 I'm trying to write a function that finds all possible combinations of coins that yield a specified amount, for example it calculates all possible way to give change for the amount 2 British pounds from the list of denominations 1p, 2p, 5p,10p,20p,50p,1pound,2pound. I'm stuck with this and can't find the right solution. I want the main function to be recursive, because I want to understand recursion better. The algorithm must backtrack, if the combination found at some moment exceeds the

how to type Permutation for a given string in C# [duplicate]

情到浓时终转凉″ 提交于 2019-12-14 03:23:52
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: How to type sets for a given string I'm trying to type the permutation for a string for example the string "123" should give me 123 132 213 231 321 312 I need help to fix my code using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestAAD { class Program { static int len = 0; static int lenPerm = 0; private static void Permutations(string str, string perm , int i) {

Why is this sudoku backtracking getting stuck?

▼魔方 西西 提交于 2019-12-13 06:35:51
问题 I'm writing a sudoku backtracking solver, it's getting stuck and I don't understand why. I think my recursive calls are alright. What I'm missing? Input is read from input.txt file with the grid initial layout in a single line: input.txt: 004020000201950070090004852005490001006000900800051300958100020010072608000080500 Edit: I mean 'stuck' as not finishing its solving of the grid This is a sample output: current move count is 6 3 6 4 7 2 8 1 9 0 2 0 1 9 5 0 0 7 0 0 9 0 0 0 4 8 5 2 0 0 5 4 9 0

How to pass python list dynamically to another python file

牧云@^-^@ 提交于 2019-12-13 06:11:35
问题 I am developing an N-Queen Simulation using pygame. class NQ: def __init__(self,n): self.size = n self.columns = [] * self.size self.places = 0 self.backtracks = 0 def place(self, startRow=0): if len(self.columns) == self.size: return self.columns else: for row in range(startRow, self.size): if self.isSafe(len(self.columns), row) is True: self.columns.append(row) self.places += 1 return self.place() else: lastRow = self.columns.pop() self.backtracks += 1 return self.place(startRow=lastRow + 1

Sudoku backtracking algorithm (Java)

橙三吉。 提交于 2019-12-13 01:27:17
问题 I've created a Sudoku solver that will solve a Sudoku as a human might- by checking possibilities + definite values in squares corresponding to the square being checked. (Source: http://pastebin.com/KVrXUDBF) However, I would like to create a random Sudoku generator (from a blank grid), and so have decided to use a backtracking algorithm. I understand the concept of backtracking, but am confused about one thing: How do I know which previous node to return to (and change) once I know a certain

Parse C-Style Comments with Regex, avoid Backtracking

拈花ヽ惹草 提交于 2019-12-12 15:23:24
问题 I want to match all block and multiline comments in a JavaScript file (these are C-Style comments). I have a pattern that works well. However, it creates some backtracking which slows it down significantly, especially on larger files. Pattern: \/\*(?:.|[\r\n])*?\*\/|(?:\/\/.*) Example: https://www.regex101.com/r/pR6eH6/2 How can I avoid the backtracking? 回答1: You have heavy backtracking because of the alternation. Instead of the (?:.|[\r\n]) , you may consider using a character class [\s\S]

What OCaml libraries are there for lazy list handling?

…衆ロ難τιáo~ 提交于 2019-12-12 13:39:59
问题 What OCaml libraries are out there that provide lazy list handling? I am looking for something along these lines: type 'a lazy_list = (*'*) | Nil | Cons of 'a * 'a lazy_list lazy_t let from f = let rec gen n = lazy ( match f n with | Some x -> Cons (x, gen (n + 1)) | None -> Nil ) in gen 0 Integration with the Stream type and syntactic sugar for backtracking Camlp4 parsers would be nice. 回答1: Ocaml Batteries has a lazy list module, check out the to_stream function. As for backtracking, you

php - backtracking maze generation (converting everything to 2d array)

别说谁变了你拦得住时间么 提交于 2019-12-12 06:36:57
问题 Here generating a maze by backtracking works correctly but the thing is that I am trying to implement this in a pixel game (Minecraft)... so the problem is in drawing the maze. In this game, the size of the wall blocks should be the exact same as the size of an empty block/space, so the only solution I thought of was a bonus 2d array called totalmaze. Its purpose is to store both empty spaces and wall blocks so I made its size (x*3, y*3) and tried outputting the walls but unfortunately this