c++ - 编程练习的回溯解决方案(安装管道)

标签 c++ algorithm recursion backtracking

我正在审查本地编程竞赛中的一个编程问题。

您可以下载问题here (PDF)。它是荷兰语,但图片将有助于理解它。

您收到一个 m*m 网格作为输入,其中包含一些管道和一些缺失点(问号)。 其余的管道必须放置在网格中,以便它们与其他管道连接。

每个管道都表示为一个字母(参见第 2 页的图片)。字母“A”的值为 1,“B”的值为 2,..

我尝试通过回溯来解决它(它还不能很好地工作)。但是由于网格可以是 10x10,这将太慢。 有人可以提出更好(更快)的解决方案/方法吗?

#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

#define sz(a) int((a).size())
#define pb push_back

int m, found;
string letters;
vector<int> done;
vector<string> a;

int ok(char letter, int c, int r)
{
    int val = letter - 'A' + 1;

    //checking if no side goes outside
    if (r == 0 && (val & 1))
        return 0;
    if (r == m - 1 && (val & 4))
        return 0;
    if (c == 0 && (val & 8))
        return 0;
    if (c == m - 1 && (val & 2))
        return 0;

    //check if the side is connected the other pipe on the grid
    if (r > 0 && a[r - 1][c] != '?' && (a[r - 1][c] & 4) && !(val & 1))
        return 0;
    if (c > 0 && a[r][c - 1] != '?' && (a[r][c - 1] & 2) && !(val & 8))
        return 0;
    if (r < m - 1 && a[r + 1][c] != '?' && (a[r + 1][c] & 1) && !(val & 4))
        return 0;
    if (c < m - 1 && a[r][c + 1] != '?' && (a[r][c + 1] & 8) && !(val & 2))
        return 0;

    return 1;
}

void solve(int num_placed, int pos)
{
    if (found) return;

    //done
    if (num_placed == sz(letters)) {
        for (int i = 0; i < m; ++i)
            cout << a[i] << endl;
        found = 1;
        return;
    }

    int c = pos % m;
    int r = pos / m;
    if (a[r][c] != '?')
        solve(num_placed, pos + 1);

    //try all the pipes on this position
    for (int i = 0; i < sz(letters); ++i) {
        if (!done[i] && ok(letters[i], c, r)) {
            a[r][c] = letters[i];
            done[i] = 1;
            solve(num_placed + 1, pos + 1);
            done[i] = 0;
            a[r][c] = '?';
        }
    }
}

int main()
{
    freopen("input.txt", "r", stdin);

    int n;
    cin >> n;

    while (n--) {
        cin >> m;
        cin >> letters;

        cout << m << endl;
        a.clear();
        for (int i = 0; i < m; ++i) {
            string line;
            cin >> line;
            a.pb(line);
        }

        done = vector<int>(sz(letters), 0);

        found = 0;
        solve(0, 0);
    }

    return 0;
}

最佳答案

原始回复

您必须自己编写所有代码还是有兴趣探索其他工具?因为我建议查看约束传播/线性规划。你已经有很多边界约束——外边缘不能有管道,加上内边缘——所以我想这会非常有效。此外,约束看起来像是简单的等式,所以应该很容易设置。

我没有足够的经验在这里提供更多细节(尽管如果我下周有时间我可能会在某个时候试一试),但如果这种事情很有趣,还有更多背景在 another answer i wrote some time ago .

ps 有趣的问题;感谢您发布此内容。

[编辑:如果您不能使用其他库,那么您可以自己进行约束传播。有一个精彩article by norvig这显示了如何为数独做到这一点。我强烈建议您阅读一下——我想您会看到如何将这些技术运用到其中,即使它是数独和 python。]

更新回复(2012-04-06 - 更新了博客引用;旧评论有问题)

深度优先搜索,其中下一个空单元格被每个可用的一致图 block 填充,并且一致性检查包括边缘约束(没有管道离开边缘)和最近的邻居,是相当有效率。我在 clojure 中有一个未优化的实现,它将在 0.4 毫秒左右(JVM 预热后 360 毫秒内 1000 个)解决较小的示例,在 3 毫秒内解决较大的示例(cedric van goethem 报告优化了 1 毫秒 - 但仍然是 OO - java 实现,这似乎是合理的)。它可以在 12 秒内解决一个 10x10 的谜题(没有初始提示的同心圆管)。

我还花时间研究了一种“智能”方法,该方法可以跟踪每个单元格的约束,就像上面 norvig 的论文一样。然后我尝试使用 choco . blog posts here 中更详细地描述了所有这些。 (我在这个答案的更新中确实有更多细节,但它们是基于错误的代码——博客有更多、更好的信息)。源代码也可供下载。

所有这些的一般结论是直接搜索可以达到 10x10。在那之后,更多的聪明可能会有所帮助,但很难确定,因为生成测试用例很困难(它们往往是模棱两可的,即使给出了很多单元格)。

关于c++ - 编程练习的回溯解决方案(安装管道),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9689436/

相关文章:

Python:调用复制函数时调用 Python 对象时超出最大递归深度

c++ - gcc 4.7 和递归 constexpr 函数

c++ - Qt QDialog 每次关闭并再次显示时都会稍微移动位置

c++ - 生成文件 'fdopen: Bad file descriptor' 错误

c++ - Lambda 表达式等效 C++ 代码

c++ - 在 Visual Studio 中执行 C 程序时,表达式必须具有指向对象类型 ERROR 的指针

algorithm - 这种多线程程序生成算法有哪些潜在问题?

recursion - 如何在 Elixir 中模拟 Ruby 的 until 循环?

java - 枚举外部驱动器

c++ - 最长公共(public)递增子序列动态规划