【问题标题】:Checking soduku in C, why my code doesn't work?在 C 中检查 soduku,为什么我的代码不起作用?
【发布时间】:2017-11-21 09:05:44
【问题描述】:

我正在构建一个程序来检查 C 中的 soduku 解决方案,其中用户插入拼图的大小和解决方案。 程序需要仅使用二维数组来检查解决方案是否有效。

我的算法是获取每一行、每一列和每一框,将其放入一个一维数组(每个)中,对其进行排序并将其与另一个从 1-n 开始的一维数组进行比较。

我不知道为什么,但我的程序无法运行,我需要帮助。

我的代码:

    /*-------------------------------------------------------------------------
  Include files:
--------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>


/*=========================================================================
  Constants and definitions:
==========================================================================*/

/* put your #defines and typedefs here*/
void printOpenMessageForSodokoSize();
void printOpenMessageForSodokoSolution();
void printValidSolution();
void printBadSolution();
int root(int a);
void bubbleSort(int arr[], int n);
int compareTo(int arr[], int n);
int checkRows(int n,  int mat[][n]);
int checkColumns(int n, int mat[][n]);
int checkGrids(int n, int mat[][n], int sRow, int sCol);

/*-------------------------------------------------------------------------
  The main program. (describe what your program does here)
 -------------------------------------------------------------------------*/
int main()
{
    printOpenMessageForSodokoSize();
    int n;
    int grids=1, rows=1, cols=1;
    scanf("%d", &n);
    while(root(n)<=0)
        scanf("%d", &n);
    printOpenMessageForSodokoSolution();
    int mat[n][n];
    for (int i=0;i<n;i++)
        for (int j=0;j<n;j++)
            scanf("%d", &mat[i][j]);
    while(grids) //checking each sub-grid
    {
        for (int i=0;i<n;i+=root(n))
            for (int j=0;j<n;j+=root(n))
                grids=checkGrids(n, mat ,i, j);
    }
    rows=checkRows(n, mat);
    cols=checkColumns(n, mat);
    if ((rows==1)&&(cols==1)&&(grids==1))
        printValidSolution();
    else
        printBadSolution();
    return 0;
}
void printOpenMessageForSodokoSize()
{
    printf("Please enter the size of your soduko:\n");
}
void printOpenMessageForSodokoSolution()
{
    printf("Please enter your solution:\n");
}
void printValidSolution()
{
    printf("Valid solution!");
}
void printBadSolution()
{
    printf("Bad solution!");
}
int root(int a) //not using math library on purpose.
{
    if (a==0)
        return 0;
    if (a<0)
        return -1;
    for(int i=1;i<=a;i++)
    {
        if (i*i==a)
            return i;
    }
    return -1;
}
void bubbleSort(int arr[],int n) //sorting an array
{
    int swap;
    for (int i=0;i<(n-1);i++)
        for (int j=0;j<n-i-1;j++)
        {
            if (arr[j] > arr[j+1])
            {
                swap=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=swap;
            }
        }
}
int compareTo(int arr[],int n) //check if 2 arrays are equal
{
    int equal=1; //return 1 if equals 0 if not
    int arr2[n]; //building the array to compare to
    for(int i=0;i<n;i++)
        arr2[i]=i+1;
    while(equal)
    {
        for(int i=0;i<n;i++)
            equal=(arr[i]==arr2[i]);
    }
    return equal;
}
int checkRows(int n, int mat[][n]) //checking the rows
{
    int rows=1;
    int arr[n];
    while (rows)
    {
        for (int i=0;i<n;i++)
        {
            for (int j=0;j<n;j++)
                arr[j]=mat[i][j]; //creating an array for each row
        bubbleSort(arr, n); // sort it
        rows=compareTo(arr, n); // compare it
        }
    }
    return rows;
}
int checkColumns(int n, int mat[][n])
{
    int columns=1;
    int arr[n];
    while (columns)
    {
        for (int i=0;i<n;i++)
        {
            for (int j=0;j<n;j++)
                arr[j]=mat[j][i]; //creating an array for each column
        bubbleSort(arr, n); // sort it
        columns=compareTo(arr, n); //compare it
        }
    }
    return columns;
}
int checkGrids(int n, int mat[][n], int sRow, int sCol)
{
    int grids=1;
    int arr[n];
    int index=0;
    for (int i=sRow;i<root(n);i++)
        for (int j=sCol;j<root(n);j++)
        {
            arr[index]=mat[i][j]; //creating an array for each grid
            index++;
        }
    bubbleSort(arr, n); // sort it
    grids=compareTo(arr, n); //compare it
    return grids;
}

【问题讨论】:

  • 那么出了什么问题?它会崩溃,是否会产生错误的输出?会发生什么?
  • 我尝试输入有效的 4*4 解决方案,但程序不会打印任何内容,无论对错。
  • “我不知道为什么,但是我的程序不起作用,我需要帮助”,那么它有什么作用?哪个部分运行不好?你调试过检查程序吗?你能缩小潜在的故障部分吗?
  • 抱歉不够清楚。我输入任何解决方案,无论是否有效,程序继续运行,不打印任何消息或结束。

标签: c arrays 2d sudoku


【解决方案1】:

printf 使用缓冲输出,这意味着在打印\n 或执行fflush(stdout) 之前,您printf 的任何内容都不会出现在终端上。

尝试以下方法:

void printValidSolution()
{
    printf("Valid solution!\n"); // new line added
}
void printBadSolution()
{
    printf("Bad solution!\n"); // new line added
}

另一个问题

int compareTo(int arr[],int n) //check if 2 arrays are equal
{
    int equal=1; //return 1 if equals 0 if not
    int arr2[n]; //building the array to compare to
    for(int i=0;i<n;i++)
        arr2[i]=i+1;
    while(equal)
    {
        for(int i=0;i<n;i++)
            equal=(arr[i]==arr2[i]);
    }
    return equal;
}

如果arr[n - 1] == arr2[n - 1],此函数将永远不会终止,因为while 循环中没有任何内容会更改数组,因此for 循环最终将始终将equal 设置为最后一次比较的结果。不幸的是,我不确定这个函数应该做什么,所以我不能告诉你如何修复它。

【讨论】:

  • 我试过了,但没有用。我输入了任何解决方案,无论是否有效,程序继续运行,没有打印任何消息或结束。
  • @Ro168 如果程序在没有终止的情况下运行,而不是只是不打印任何内容,您可能应该在问题中提及它。
  • @Ro168 我发现了一个无限循环 - 请参阅修改后的答案。
  • 谢谢你,我真的很感谢你的帮助,很抱歉我的解释不够。 ComapreTo 函数将包含每行、列和框的每个数组与从 1 到 n 的排序数组进行比较,如果两个数组相等,我确定每个数字只显示一次并且解决方案有效。我试图通过在“while”循环中添加另一个条件来根据您注意到的问题修复该函数 - 现在它是“while((equal)||(i==n))”,但程序仍在运行而没有终止. 我花了几个小时尝试调试,我急需帮助。再次感谢
猜你喜欢
  • 2021-03-26
  • 1970-01-01
  • 2016-09-17
  • 1970-01-01
  • 2022-06-16
  • 1970-01-01
  • 2014-01-23
  • 2013-08-06
相关资源
最近更新 更多