나는 사용자가 수수께끼의 크기와 솔루션을 삽입하는 C로 soduku 솔루션을 확인하는 프로그램을 작성 중입니다. 프로그램이 솔루션이 유효한지 또는 2 차원 어레이 만 사용하는지 확인해야합니다.C에서 soduku를 확인하면 왜 내 코드가 작동하지 않습니까?
내 알고리즘은 모든 행, 열 및 상자를 가져 와서 1d arrray (각각)에 넣고 정렬하고 1-n에서 오는 다른 1d 배열과 비교하는 것입니다.
이유는 모르겠지만 프로그램이 작동하지 않아 도움이 필요합니다.
내 코드 :
/*-------------------------------------------------------------------------
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;
}
그럼 어떻게 될까요? 충돌이 발생합니까? 출력이 잘못됩니까? 무슨 일이야? – MrHug
유효한 4 * 4 솔루션을 입력하려고했는데 프로그램에서 옳고 그른 것을 인쇄하지 않습니다. – Ro168
읽어보기 : https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –