backtracking

Lights out game algorithm

余生颓废 提交于 2019-11-26 14:34:03
问题 It's a homework. I have to design and lights out game using backtracking description is below. The game consists of a 5-by-5 grid of lights; when the game starts, a set of these lights (random, or one of a set of stored puzzle patterns) are switched on. Pressing one of the lights will toggle it, and the four lights adjacent to it, on and off. (Diagonal neighbours are not affected.) The game provides a puzzle: given some initial configuration where some lights are on and some are off, the goal

Can you write between/3 in pure prolog?

天大地大妈咪最大 提交于 2019-11-26 11:37:34
问题 I\'ve been trying to understand how to produce a series of values from a Prolog predicate on backtracking. The builtin predicate between/3 will generate all the integers in a range one at a time on backtracking, so an example of how that is written may help me with my task. I looked for an implementation in an existing Prolog system, but the implementation of between/3 for GNU Prolog is a C function, and the trick there is that it calls another C function \"Pl_Create_Choice_Point\" which

Collect all “minimum” solutions from a predicate

谁说胖子不能爱 提交于 2019-11-26 11:35:10
问题 Given the following facts in a database: foo(a, 3). foo(b, 2). foo(c, 4). foo(d, 3). foo(e, 2). foo(f, 6). foo(g, 3). foo(h, 2). I want to collect all first arguments that have the smallest second argument, plus the value of the second argument. First try: find_min_1(Min, As) :- setof(B-A, foo(A, B), [Min-_|_]), findall(A, foo(A, Min), As). ?- find_min_1(Min, As). Min = 2, As = [b, e, h]. Instead of setof/3 , I could use aggregate/3 : find_min_2(Min, As) :- aggregate(min(B), A^foo(A, B), Min)

How to generate all the permutations of a multiset?

独自空忆成欢 提交于 2019-11-26 09:54:58
问题 A multi-set is a set in which all the elements may not be unique.How to enumerate all the possible permutations among the set elements? 回答1: Generating all the possible permutations and then discarding the repeated ones is highly inefficient. Various algorithms exist to directly generate the permutations of a multiset in lexicographical order or other kind of ordering. Takaoka's algorithm is a good example, but probably that of Aaron Williams is better http://webhome.csc.uvic.ca/~haron

generate all partitions of a set [closed]

烈酒焚心 提交于 2019-11-26 07:38:34
问题 For a set of the form A = {1, 2, 3, ..., n} . It is called partition of the set A , a set of k<=n elements which respect the following theorems: a) the union of all the partitions of A is A b) the intersection of 2 partitions of A is the empty set (they can\'t share the same elements). For example. A = {1, 2,... n} We have the partitions: {1, 2, 3} {1, 2} {3} {1, 3} {2} {2, 3} {1} {1} {2} {3} These theoretical concepts are presented in my algorithms textbook (by the way, this subchapter is

Simple AlphaNumeric Regex (single spacing) without Catastrophic Backtracking

被刻印的时光 ゝ 提交于 2019-11-26 03:45:36
问题 I have the following REGEX expression (that works) to allow Alpha-Numeric (as well as \' and - ) and no double spacing: ^([a-zA-Z0-9\'-]+\\s?)*$ Due to the nested grouping, this allows Catastrophic Backtracking to happen - which is bad! How can I simplify this expression to avoid Catastrophic Backtracking?? (Ideally this wouldn\'t allow white-space in first and last characters either) 回答1: Explanation Nested group doesn't automatically causes catastrophic backtracking. In your case, it is