Dancing Cover的Cover Column / Uncover Column伪代码是否正确?

时间:2014-03-06 16:28:36

标签: algorithm

我正在使用算法X(Dancing Links)进行一个项目,我想知道列覆盖的伪代码是否正确。我没有找到许多处理“下一行”和“下一列”的伪代码,如Knuth's paper所解释的,所以我真的无法将其余部分与我的进行比较。但是他的论文显然对实现细节并不大,所以我也无法与它完全比较。

当我想要返回多个解决方案时,我的Dancing Links代码出了问题(38x用于2x2空板,并且它与更大的板崩溃)从代码的角度来看,一切都是“正确的”,所以我想也许这个概念是错误的。

这是伪代码(我知道它看起来很多但是因为我将它分成4个部分以最大限度地减少错误并使其更易于阅读,实际上它真的非常简单):

node::cover()
/*Does the unlinking/relinking of the Cover procedures*/
{
    if this is the only visible node in the column
        null its header's firstRow and lastRow pointers
    else
        this->previousRow sets its nextRow to this->nextRow
        this->nextRow sets its previousRow to this->previousRow
        if this is the first row of this->header
            set this->header's first row as this->nextRow
        else if this is the last row of this->header
            set this->header's last row as this->previousRow
    decrease this->header's amount of rows by 1
}

node::uncover()
/*Does the unlinking/relinking of the Uncover procedures*/
{
    if this is the only visible node in the column
        set its header's firstRow and lastRow pointers to this
    else
        this->previousRow sets its nextRow to this
        this->nextRow sets its previousRow to this
        if this->nextRow is the first row of this->header
            set this->header's first row as this
        else if this->previousRow is the last row of this->header
            set this->header's last row as this
    increase this->header's amount of rows by 1
}

node::coverRow(header H)
/*Covers the entire row.*/
{
    index = this->nextNode
    while index->header is different from H
        index->cover()
        index->nextNode
}

node::uncoverRow(header H)
/*Uncovers the entire row.*/
{
    index = this->previousNode
    while index->header is different from H
        index->uncover()
        index->previousNode
}

header::cover()
/*Covers column and calls to coverRow for each row*/
{
    Set this->previousColumn as this->nextColumn
    Set this->nextColumn as this->previousColumn
    index = this->firstRow
    if my index is not null
        while index is different from lastRow
            index->coverRow(this)
            index = next row
        lastRow->coverRow(this)
}

header::uncover()
/*Uncovers column and calls to coverRow for each row*/
{
    Set previousColumn->nextColumn as this
    Set nextColumn->previousColumn as this
    index = this->lastRow
    if my index is not null
        while index is different from firstRow
            index->uncoverRow(this)
            index = previous row
        firstRow->uncoverRow(this)
}

coverColumns(node selectedRow)
/*Gets a list of the headers (from the first node to the next node of the row) and goes through them, first to last, covering them.*/
{
    get a list of the headers that are linked to that selectedRow
    index = first node of the list
    while index != end of the list
        index->header->cover()
        index = next node of the list
}

uncoverColumns(node selectedRow)
/*Gets a list of the headers (from the first node to the next node of the row) and goes through them, last to first, covering them.*/
{
    get a list of the headers that are linked to that selectedRow
    index = last node of the list
    while index != beginning of the list
        index->header->cover()
        index = previous node of the list
}

您拥有的第一件事是selectedRow,您必须使用它作为参数调用uncoverColumns。从那里我想(虽然,现在考虑它不工作我不太确定)你必须获得该行满意的所有列。一旦你有三个,你就可以覆盖他们所有的行。

我希望伪代码是可读的,这是我第一次为其他人编写伪代码。

提前多多感谢。

1 个答案:

答案 0 :(得分:0)

您需要以与uncover相反的顺序在cover中执行操作。目前,您以相同的顺序执行这些操作。