给数独验证题目编写测试代码
一、题目介绍:
数独是一种填数游戏,玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行、每一列、每一个粗线宫(3*3)内的数字均含1-9,不重复。
每个数独有唯一解。下图是一个数独填数后的解(黑色数字是已知数字,绿色数字是填数数字)。
输入填数后的9×9盘面,写函数判断其是否是解,返回1或0。
原题目地址为 https://blog.csdn.net/2203_75720729/article/details/128789015
输入要求:
测试次数
每组测试数据是1个9*9的数字阵(元素值:1~9)
输出要求:
每组测试数据,如果满足数独要求,输出YES,否则输出NO
二、修改思路:
我个人认为每次测试此代码都需要输入1个9*9的数字阵实在是麻烦,所以我在题目原有的代码上增加了我个人的代码使之可以快速高效的测试。
所以我增加以下三个函数来实现测试
void fprintf1 负责打印9*9的数字阵;
int myRand 是通过九宫格accord的第一行为[6, 4, 5, 7, 3, 9, 8, 1, 2], 则可获得的九宫格squa的第一行第一列元素这样产生:看accord对应元素为6,则看root中6后一位的数为7,则所求数字为7
int reRank 获得一个填满的九宫格accord
三 源代码
#include<iostream>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<cstring>
using namespace std;
int row(int arr[][10])
{
int count[10], totalsub;//用count记录每个数字出现的次数,如果符合条件每个数字只出现一次
for (int i = 1; i <= 9; i++)
{
memset(count, 0, 10 * sizeof(int));
for (int j = 1; j <= 9; j++)
{
count[arr[i][j]]++;
}
totalsub = 1;
for (int k = 1; k <= 9; k++)
{
totalsub *= count[k];//累乘只能等于1
}
if (totalsub != 1)
{
return 0;
}
}
return 1;
}
int col(int arr[][10])//与行同理
{
int count[10], totalsub;
for (int i = 1; i <= 9; i++)
{
memset(count,
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<cstring>
using namespace std;
int row(int arr[][10])
{
int count[10], totalsub;//用count记录每个数字出现的次数,如果符合条件每个数字只出现一次
for (int i = 1; i <= 9; i++)
{
memset(count, 0, 10 * sizeof(int));
for (int j = 1; j <= 9; j++)
{
count[arr[i][j]]++;
}
totalsub = 1;
for (int k = 1; k <= 9; k++)
{
totalsub *= count[k];//累乘只能等于1
}
if (totalsub != 1)
{
return 0;
}
}
return 1;
}
int col(int arr[][10])//与行同理
{
int count[10], totalsub;
for (int i = 1; i <= 9; i++)
{
memset(count,