[英]Checking soduku in C, why my code doesn't work?
我正在構建一個程序來檢查 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;
}
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
上次比較的結果。 不幸的是,我不確定這個函數應該做什么,所以我不能告訴你如何修復它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.