recursive-backtracking

How to solve Sudoku by backtracking and recursion?

99封情书 提交于 2019-12-11 12:31:57
问题 the reason why I am creating this new thread instead of just reading the answers to this particular question that have been given before is that I feel I just don't fully understand the whole idea behind it. I can't seem to get my head around the whole backtracking concept. So I need to fully understand backtracking and then solve the particular sudoku problem. What I understand so far is that backtracking is a technique to go back, in (e.g.) a recursive flow if one discovers that decisions

Recursive backtracking in Java for solving a crossword

北城以北 提交于 2019-12-10 16:34:51
问题 I need to solve a crossword given the initial grid and the words (words can be used more than once or not at all) . The initial grid looks like that: ++_+++ +____+ ___+__ +_++_+ +____+ ++_+++ Here is an example word list: pain nice pal id The task is to fill the placeholders (horizontal or vertical having length > 1) like that: ++p+++ +pain+ pal+id +i++c+ +nice+ ++d+++ Any correct solution is acceptable, and it's guaranteed that there's a solution. In order to start to solve the problem, I

What's time complexity of this Algorithm for breaking words? (Dynamic Programming)

萝らか妹 提交于 2019-12-10 10:32:33
问题 Word Break (with Dynamic Programming: Top->Down) Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example , given s = "catsanddog", dict = ["cat", "cats", "and", "sand", "dog"]. A solution is ["cats and dog", "cat sand dog"]. Question: Time complexity ? Space complexity ? Personally I think, Time complexity = O(n!), without Dynamic Programming, n is the length of the

how to write iterative algorithm for generate all subsets of a set?

老子叫甜甜 提交于 2019-12-06 10:30:59
问题 I wrote recursive backtracking algorithm for finding all subsets of a given set. void backtracke(int* a, int k, int n) { if (k == n) { for(int i = 1; i <=k; ++i) { if (a[i] == true) { std::cout << i << " "; } } std::cout << std::endl; return; } bool c[2]; c[0] = false; c[1] = true; ++k; for(int i = 0; i < 2; ++i) { a[k] = c[i]; backtracke(a, k, n); a[k] = INT_MAX; } } now we have to write the same algorithm but in an iterative form, how to do it ? 回答1: You can use the binary counter approach.

how to write iterative algorithm for generate all subsets of a set?

ぃ、小莉子 提交于 2019-12-04 16:29:55
I wrote recursive backtracking algorithm for finding all subsets of a given set. void backtracke(int* a, int k, int n) { if (k == n) { for(int i = 1; i <=k; ++i) { if (a[i] == true) { std::cout << i << " "; } } std::cout << std::endl; return; } bool c[2]; c[0] = false; c[1] = true; ++k; for(int i = 0; i < 2; ++i) { a[k] = c[i]; backtracke(a, k, n); a[k] = INT_MAX; } } now we have to write the same algorithm but in an iterative form, how to do it ? You can use the binary counter approach. Any unique binary string of length n represents a unique subset of a set of n elements. If you start with 0

Fixed values not repeated over column and row

北城余情 提交于 2019-12-02 11:17:30
问题 I want to create a matrix in R with a set number of variables (e.g. 1 to 10). Those variables should be randomly assigned over rows and columns BUT should not be repeated in either (so number 1 should be once in row 1 and once in column 1)! So for example: 1,2,3,4,5,6,7,8,9,10 2,3,4,5,6,7,8,9,10,1 3,4,5,6,7,8,9,10,1,2 4,5,6,7,8,9,10,1,2,3 5,6,7,8,9,10,1,2,3,4 6,7,8,9,10,1,2,3,4,5 7,8,9,10,1,2,3,4,5,6 8,9,10,1,2,3,4,5,6,7 9,10,1,2,3,4,5,6,7,8 10,1,2,3,4,5,6,7,8,9 But of course in that example

backtracking n staircases at most k steps in a single jump

一曲冷凌霜 提交于 2019-12-02 04:09:46
You need to climb a staircase that has n steps, and you decide to get some extra exercise by jumping up the steps. You can cover at most k steps in a single jump. Return all the possible sequences of jumps that you could take to climb the staircase, sorted. My implementation is obviously giving me the wrong answer. def climbingStaircase(n, k): final_res=[] final_res.append(CSR(n,k,[])) return final_res def CSR(n,k,res): if n == 0: return res else: for i in range(1,k+1): if n-i>=0: res.append(i) n=n-i res=CSR(n,i,res) return res For n = 4 and k = 2, the output should be [[1, 1, 1, 1], [1, 1, 2]

Backtracking bruteforce Java password cracker

让人想犯罪 __ 提交于 2019-12-01 14:19:50
I have this homework assignment to make a recursive method to crack a password of a given length, n (unlimited and unknown!) made of small English letters, a-z ONLY. Here's the class "Password" that creates a random password: import java.util.Random; public class Password { private String _password = ""; public Password(int length) { Random generator = new Random(); for (int i = 0; i < length; ++i) { this._password = this._password + (char) (generator.nextInt(26) + 97); } } public boolean isPassword(String st) { return st.equals(this._password); } public String getPassword() { return this.