KenKen 拼图加法:REDUX A(更正)非递归算法

KenKen puzzle addends: REDUX A (corrected) non-recursive algorithm(KenKen 拼图加法:REDUX A(更正)非递归算法)

本文介绍了KenKen 拼图加法:REDUX A(更正)非递归算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与 KenKen 拉丁方拼图的那些部分有关,这些拼图要求您找到具有 x 值的 ncells 数字的所有可能组合,使得 1 <= x <= maxval 和 x(1) + ... +x(ncells) = 目标和.在测试了几个更有希望的答案后,我将把答案奖授予 Lennart Regebro,因为:

This question relates to those parts of the KenKen Latin Square puzzles which ask you to find all possible combinations of ncells numbers with values x such that 1 <= x <= maxval and x(1) + ... + x(ncells) = targetsum. Having tested several of the more promising answers, I'm going to award the answer-prize to Lennart Regebro, because:

  1. 他的例行程序和我的一样快(+-5%),并且

  1. his routine is as fast as mine (+-5%), and

他指出我原来的例程在某个地方有一个错误,这让我看到了它真正想要做什么.谢谢,伦纳特.

he pointed out that my original routine had a bug somewhere, which led me to see what it was really trying to do. Thanks, Lennart.

chrispy 贡献了一个算法,看起来和 Lennart 的算法差不多,但 5 小时后,太棒了,先到了电线上.

chrispy contributed an algorithm that seems equivalent to Lennart's, but 5 hrs later, sooo, first to the wire gets it.

备注:Alex Martelli 的基本递归算法是一个示例,它生成所有可能的组合,然后将它们全部扔进筛子中,然后查看哪些通过了孔.这种方法比 Lennart 或我的方法长 20 多倍.(将输入提升到 max_val = 100、n_cells = 5、target_sum = 250,在我的盒子上是 18 秒对 8 分钟以上.)道德:不生成所有可能的组合是好的.

A remark: Alex Martelli's bare-bones recursive algorithm is an example of making every possible combination and throwing them all at a sieve and seeing which go through the holes. This approach takes 20+ times longer than Lennart's or mine. (Jack up the input to max_val = 100, n_cells = 5, target_sum = 250 and on my box it's 18 secs vs. 8+ mins.) Moral: Not generating every possible combination is good.

另一条评论:Lennart 和我的例程以相同的顺序生成相同的答案.从不同的角度来看,它们实际上是相同的算法吗?我不知道.

Another remark: Lennart's and my routines generate the same answers in the same order. Are they in fact the same algorithm seen from different angles? I don't know.

我想到了一些事情.如果您对答案进行排序,例如从 (8,8,2,1,1) 开始并以 (4,4,4,4,4) 结束(您得到的结果是 max_val=8, n_cells=5, target_sum=20),该系列形成了一种最慢下降",第一个是热",最后一个是冷",中间有尽可能多的阶段.这与信息熵"有关吗?查看它的正确指标是什么?是否有一种算法可以按热量的降序(或升序)产生组合?(这个没有,据我所知,虽然它在很短的时间内很接近,看看标准化的标准开发.)

Something occurs to me. If you sort the answers, starting, say, with (8,8,2,1,1) and ending with (4,4,4,4,4) (what you get with max_val=8, n_cells=5, target_sum=20), the series forms kind of a "slowest descent", with the first ones being "hot" and the last one being "cold" and the greatest possible number of stages in between. Is this related to "informational entropy"? What's the proper metric for looking at it? Is there an algorithm that producs the combinations in descending (or ascending) order of heat? (This one doesn't, as far as I can see, although it's close over short stretches, looking at normalized std. dev.)

这是 Python 例程:

Here's the Python routine:

#!/usr/bin/env python
#filename: makeAddCombos.07.py -- stripped for StackOverflow

def initialize_combo( max_val, n_cells, target_sum):
    """returns combo
    Starting from left, fills combo to max_val or an intermediate value from 1 up.  
    E.g.:  Given max_val = 5, n_cells=4, target_sum = 11, creates [5,4,1,1].
    """
    combo = []
    #Put 1 in each cell.
    combo += [1] * n_cells
    need = target_sum - sum(combo)
    #Fill as many cells as possible to max_val.
    n_full_cells = need //(max_val - 1)
    top_up = max_val - 1
    for i in range( n_full_cells): combo[i] += top_up
    need = target_sum - sum(combo)
    # Then add the rest to next item.
    if need > 0:
        combo[n_full_cells] += need
    return combo
#def initialize_combo()

def scrunch_left( combo):
    """returns (new_combo,done)
    done   Boolean; if True, ignore new_combo, all done;
            if Falso, new_combo is valid.

    Starts a new combo list.  Scanning from right to left, looks for first
    element at least 2 greater than right-end element.  
    If one is found, decrements it, then scrunches all available counts on its
    right up against its right-hand side.  Returns the modified combo.
    If none found, (that is, either no step or single step of 1), process
    done.
    """
    new_combo = []
    right_end = combo[-1]
    length = len(combo)
    c_range = range(length-1, -1, -1)
    found_step_gt_1 = False
    for index in c_range:
        value = combo[index]
        if (value - right_end) > 1:
            found_step_gt_1 = True
            break
    if not found_step_gt_1:
        return ( new_combo,True)

    if index > 0:
        new_combo += combo[:index]
    ceil = combo[index] - 1
    new_combo += [ceil]
    new_combo += [1] * ((length - 1) - index)
    need = sum(combo[index:]) - sum(new_combo[index:])
    fill_height = ceil - 1
    ndivf = need // fill_height
    nmodf = need % fill_height
    if ndivf > 0:
        for j in range(index + 1, index + ndivf + 1):
            new_combo[j] += fill_height
    if nmodf > 0:
        new_combo[index + ndivf + 1] += nmodf
    return (new_combo, False)
#def scrunch_left()

def make_combos_n_cells_ge_two( combos, max_val, n_cells, target_sum):
    """
    Build combos, list of tuples of 2 or more addends.
    """
    combo = initialize_combo( max_val, n_cells, target_sum)
    combos.append( tuple( combo))
    while True:
        (combo, done) = scrunch_left( combo)
        if done:
            break
        else:
            combos.append( tuple( combo))
    return combos
#def make_combos_n_cells_ge_two()

if __name__ == '__main__':

    combos = []
    max_val     = 8
    n_cells     = 5
    target_sum  = 20
    if n_cells == 1: combos.append( (target_sum,))
    else:
        combos = make_combos_n_cells_ge_two( combos, max_val, n_cells, target_sum)
    import pprint
    pprint.pprint( combos)

推荐答案

首先,我会使用有意义的变量名,以便代码易于理解.然后,在我理解了这个问题之后,这显然是一个递归问题,因为一旦你选择了一个数字,为​​其余的平方找到可能值的问题是完全相同的问题,但有不同的值.

First of all, I'd use variable names that mean something, so that the code gets comprehensible. Then, after I understood the problem, it's clearly a recursive problem, as once you have chosen one number, the question of finding the possible values for the rest of the squares are exactly the same problem, but with different values in.

所以我会这样做:

from __future__ import division
from math import ceil

def make_combos(max_val,target_sum,n_cells):
    combos = []
    # The highest possible value of the next cell is whatever is 
    # largest of the max_val, or the target_sum minus the number 
    # of remaining cells (as you can't enter 0).
    highest = min(max_val, target_sum - n_cells + 1)
    # The lowest is the lowest number you can have that will add upp to 
    # target_sum if you multiply it with n_cells.
    lowest = int(ceil(target_sum/n_cells))
    for x in range(highest, lowest-1, -1):
        if n_cells == 1: # This is the last cell, no more recursion.
            combos.append((x,))
            break
        # Recurse to get the next cell:
        # Set the max to x (or we'll get duplicates like
        # (6,3,2,1) and (6,2,3,1), which is pointless.
        # Reduce the target_sum with x to keep the sum correct.
        # Reduce the number of cells with 1.
        for combo in make_combos(x, target_sum-x, n_cells-1):
            combos.append((x,)+combo)
    return combos

if __name__ == '__main__':
    import pprint
    # And by using pprint the output gets easier to read
    pprint.pprint(make_combos( 6,12,4))

我还注意到您的解决方案似乎仍然有问题.对于值 max_val=8, target_sum=20 和 n_cells=5,您的代码找不到解决方案 (8,6,4,1,1,),因为一个例子.我不确定这是否意味着我错过了这方面的规则,但据我所知,这些规则应该是一个有效的选项.

I also notice that your solution still seems buggy. For the values max_val=8, target_sum=20 and n_cells=5 your code doesn't find the solution (8,6,4,1,1,), as an example. I'm not sure if that means I've missed a rule in this or not, but as I understand the rules that should be a valid option.

这是一个使用生成器的版本,如果值真的很大,它可以节省几行代码和内存,但是作为递归,生成器可能很难获取".

Here's a version using generators, It saves a couple of lines, and memory if the values are really big, but as recursion, generators can be tricky to "get".

from __future__ import division
from math import ceil

def make_combos(max_val,target_sum,n_cells):
    highest = min(max_val, target_sum - n_cells + 1)
    lowest = int(ceil(target_sum/n_cells))
    for x in xrange(highest, lowest-1, -1):
        if n_cells == 1:
            yield (x,)
            break
        for combo in make_combos(x, target_sum-x, n_cells-1):
            yield (x,)+combo

if __name__ == '__main__':
    import pprint
    pprint.pprint(list(make_combos( 6,12,4)))

这篇关于KenKen 拼图加法:REDUX A(更正)非递归算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:KenKen 拼图加法:REDUX A(更正)非递归算法

基础教程推荐