首页 > 解决方案 > C++ 二维数组拼图

问题描述

开发此代码是为了解决我在旧货店发现的一个木制拼图问题,该商店有许多刻有数字的木轮。出于该程序的目的,这些轮子已被转换为二维矩阵,并带有一些描述它们相对运动范围的附加规则。

代码的计算部分正在工作。我从控制台收到的配置布局在拼图的物理和数学范围内是有效的。然而,应该只有 16^4 种可能的配置,并且经过超过 75,000 次迭代,每一个都是唯一的(就我可以看到的字面上接近一百万行输出而言),仍然没有有效的解决方案。

我怎么会少一个?

最重要的是,行需要保持相同的顺序,但可以移动(“旋转”)任意数量的空间,尽管一个空间的 16 次移动覆盖了每个轮子的整个旋转布局。

代表根据工厂配置堆叠时表盘的实际物理布局。在 checkSum() 中对值是来自上方的轮子还是下方的轮子进行排序,它还会为每列生成一个总和以与所需的解决方案进行比较(每列的数字必须加起来为“ 50" 用于要解决的难题),并输出在任何有效解决方案上等于 800 (50 * 16) 列的总和,并且可以轻松缩小解决方案是否有效(如果没有) t 其中有 100,000 个进行分类)。

无论如何,它运行。它输出数据,我只是没有看到它输出有效的解决方案,所以我可能遗漏了一些东西。束手无策。帮助将不胜感激。

控制台屏幕抓取

拼图的图片

#include "pch.h"
#include <iostream>


using namespace System;
using namespace std;
using namespace System::IO;

const int depth = 9; // (ROWS)
const int length = 16; // (COLUMNS)

int target_Column_Total = 50;
int The_Grail[length] = { 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50 };
int Sum[16];

int sum_Total;
long int Iteration;

int data_Input[depth][length] =
{   {16, 4, 7, 0, 16, 8, 4, 15, 7, 10, 1, 10, 4, 5, 3, 15},
    {2, 9, 27, 13, 11, 13, 10, 18, 10, 10, 10, 10, 15, 7, 19, 18},
    { 6, 'N', 10, 'N', 8, 'N', 10, 'N', 9, 'N', 8, 'N', 8, 'N', 9, 'N'},
    { 5, 1, 24, 8, 10, 20, 7, 20, 12, 1, 10, 12, 22, 0, 5, 8 },
    { 0, 'N', 11, 'N', 8, 'N', 8, 'N', 8, 'N', 10, 'N', 11, 'N', 10, 'N' },
    { 20, 8, 19, 10, 15, 20, 12, 20, 13, 13, 0, 22, 19, 10, 0, 5 },
    { 14, 'N', 11, 'N', 8, 'N', 12, 'N', 11, 'N', 3, 'N', 8, 'N', 10, 'N' },
    { 8, 17, 4, 20, 4, 14, 4, 5, 1, 14, 10, 17, 10, 5, 6, 18 },
    { 8, 'N', 16, 'N', 19, 'N', 8, 'N', 17, 'N', 6, 'N', 6, 'N', 8, 'N' } };

int data_Output[5][length];




void checkSum();
void turnDial(int x);




void main()
{
    while (true) {
        for (int i = 0; i < length; i++) {
            turnDial(2);
            turnDial(3);
            checkSum();
            for (int j = 0; j < length; j++) {
                turnDial(4);
                turnDial(5);
                checkSum();
                for (int k = 0; k < length; k++) {
                    turnDial(6);
                    turnDial(7);
                    checkSum();
                    for (int l = 0; l < length; l++) {
                        turnDial(8);
                        checkSum();
                    }
                }
            }
        }
    }
}
    

 


void checkSum() {

    Iteration++;

    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < length; j++) {
            if (i == 0) {
                data_Output[i][j] = data_Input[i][j];
            }

            if (i == 1) {
                if (data_Input[2][j] != 'N') {
                    data_Output[i][j] = data_Input[2][j];
                }
                else {
                    data_Output[i][j] = data_Input[1][j];
                }
            }


            if (i == 2) {
                if (data_Input[4][j] != 'N') {
                    data_Output[i][j] = data_Input[4][j];
                }
                if (data_Input[4][j] == 'N') {
                    data_Output[i][j] = data_Input[3][j];
                }
            }
            if (i == 3) {
                if (data_Input[6][j] != 'N') {
                    data_Output[i][j] = data_Input[6][j];
                }
                if (data_Input[6][j] == 'N') {
                    data_Output[i][j] = data_Input[5][j];
                }
            }
            if (i == 4) {
                if (data_Input[8][j] != 'N') {
                    data_Output[i][j] = data_Input[8][j];
                }
                if (data_Input[8][j] == 'N') {
                    data_Output[i][j] = data_Input[7][j];
                }
            }
        }

    }

    cout << "Data:" << "\n\n";

    for (int x = 0; x < depth; x++) {
        for (int y = 0; y < length; y++) {
            cout << data_Input[x][y] << "\t";
        }
        cout << endl;

    }
    cout << endl;

    cout << "Orienation:" << "\n\n";

    for (int x = 0; x < 5; x++) {
        for (int y = 0; y < length; y++) {
            cout << data_Output[x][y] << "\t";
        }
        cout << endl;
    }

    cout << "\n";
    for (int y = 0; y < 5; y++) {
        for (int x = 0; x < length; x++) {
            if (y == 0) {
                Sum[x] = 0;
            }
            Sum[x] += data_Output[y][x];
            if (y == (4)) {


                cout << Sum[x] << "\t";
            }

        }

    }

    sum_Total = { 0 };

    for (int i = 0; i < length; i++) {
        sum_Total += Sum[i];
    }

    cout << " Sum: " << sum_Total;
    cout << "\t#: " << Iteration << "\n\n";


    if (Sum != The_Grail) {
        cout << "NO DICE." << "\n\n";
  
    }

    if (Sum == The_Grail) {
        cout << "SUCCESS!" << "\n\n";
        exit(888);
     }



    // int Sum[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };



}

void turnDial(int x) {

    int holder = data_Input[x][(length - 1)];

    for (int i = (length - 1); i > 0; i--) {
        data_Input[x][i] = data_Input[x][i - 1];
    }

    data_Input[x][0] = holder;

}

标签: c++arraysmathmatrix

解决方案


我没有详细研究所有内容,但最终比较(Sum == The_Grail)总是会失败,因为这是比较两个数组指针而不是(正如人们可能合理地预期的那样)数组中的每个值。我将从遍历数组开始,比较每个元素。

另外,您确定输入数据可以保证得到解决方案吗?换句话说,输入数据是已知的好数据还是只是随机的?


推荐阅读