backtracking

Tracking an App's running time iOS

狂风中的少年 提交于 2019-12-04 08:40:03
Is there a way that I can know how long the apps in the foreground has been running? I have three possible solutions in mind: I. use battery consumption and battery consumption rate (iOS 8 and later tell you the battery usage of the app, but the batter consumption will be difficult to handle) II. use system processes monitor III. use Apple's diagnostics logs. This approach is quite "backdoor." Plus I am not sure if Apple allows us to use the information or not. Can someone tell me if any of the above solution is realistic? If not, I want to know is it possible to find out the duration of a

Knapsack solution with Backtraking in c++

笑着哭i 提交于 2019-12-04 06:17:58
问题 Im having troubles trying to resolve the Knapsack problem using backtraking. For example, for the following values, the Knapsack function will return 14 as the solution, but the correct result should be 7. int n = 3, weights[] = {2, 3, 1}, values[] = {6, 15, 7}; int solution = 0, max = 2; void Knapsack (int i, int max, int value, int *solution) { int k; for (k = i; k < n; k++) { if (weights[k] <= max) { Knapsack (k, max - weights[k], value + values[k], solution); if (value+ values[k] >

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

自作多情 提交于 2019-12-04 05:11:19
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[:index+1]): return close(new_str, num_close_parens-1, index+1, solutions) else: return close(current

Finding the numbers from a set which give the minimum amount of waste

两盒软妹~` 提交于 2019-12-03 19:54:10
问题 A set is passed to this method below, and a length of a bar is also passed in. The solution should output the numbers from the set which give the minimum amount of waste if certain numbers from the set were removed from the bar length. So, bar length 10, set includes 6,1,4, so the solution is 6 and 4, and the wastage is 0. Im having some trouble with the conditions to backtrack though the set. Ive also tried to use a wastage "global" variable to help with the backtracking aspect but to no

CUDA: Stop all other threads

六眼飞鱼酱① 提交于 2019-12-03 19:34:56
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 threads can - of course - stop their work. So, I wanted kind of the following: The thread that finds the

12 dominating knights puzzle (backtracking)

允我心安 提交于 2019-12-03 16:29:52
问题 I've been searching for hours and haven't found a fully working solution for this kind of puzzle yet. So I followed similar problem with bishops. What I need to do is place 12 knights on the chess board in such a way, that all free squares of the board are attacked by at least one piece. The final result should look like this: The problem is that my program only tries different combinations with two last pieces and then somehow crashes. EDITED What I have done so far: #include <iostream>

RegEx debugging

点点圈 提交于 2019-12-03 15:03:29
问题 I'm debugging a Regular Expression ^(A+)*B over a string AAAC (example from rexegg.com) by two separate debugging tools I have access: regex101.com RegexBuddy v4 Below is the results (regex101 in the left side): The question I have is not mainly about the number of steps which is also important to me, but is how backtracks are done. Why do we see differences? (regex101 uses PCRE lib and I set RegexBuddy lib the same) A comprehensive step by step explanation is really in my favor. 回答1: I

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

╄→尐↘猪︶ㄣ 提交于 2019-12-03 14:53:26
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(board, row+1, 0) else: column += 1 #there is no column that satisfies the conditions. Backtrack for c in

Recursive function to match a string against a wildcard pattern

泪湿孤枕 提交于 2019-12-03 12:23:55
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 , not use any loops, static & global variables. Can use local variables & method overloading. Can use

Backtracking solution for programming exercise (fitting pipes)

旧城冷巷雨未停 提交于 2019-12-03 12:21:56
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 (it doesn't quite work yet). But since the grid can be 10x10 this will be too slow. Can someone suggest