数独生成以及解数独

数独

昨天给数独游戏这题出个数据,看了看网上代码有点少,所以就自己操作了一下(可能有地方操作不到位,望见谅!代码功底不是很好!
废话不多上那就直接上代码了!

生成代码

  • 代码有些地方都加了注释,生成速度还是蛮快的,
  • 原本rand的想使用C++的mt19937,可是不太会用
#include <bits/stdc++.h>
using namespace std;
char mp[9][9];
int flag = 0;
//检查函数
bool check(int x,int y,char k){
    //检查行
    for(int i = 0 ; i < 9 ; i ++){
        if(mp[x][i] == k)
            return false;
    }
    //检查列
    for(int i = 0 ; i < 9 ; i ++){
        if(mp[i][y] == k)
            return false;
    }
    //检查九宫格
    for(int i = x / 3 * 3 ; i < x / 3 * 3 + 3 ; i ++){
        for(int j = y / 3 * 3 ; j < y / 3 * 3 + 3 ; j ++){
            if(mp[i][j] == k)
                return false;
        }
    }
    return true;
}
//搜索函数
bool dfs(){
    for(int i = 0 ; i < 9 ; i ++){
        for(int j = 0 ; j < 9 ; j ++){
            if(mp[i][j] != '?')
                continue;
            else{
                for(char k = '1' ; k <= '9' ; k ++){
                    if(check(i,j,k)){
                        mp[i][j] = k;
                        if(dfs())
                            return true;
                        else
                            mp[i][j] = '?';
                    }
                }
                return false;
            }
        }
    }
    return true;
}


class cShuDu {
    static const int MAXN = 15;
    bool vis[MAXN][MAXN][MAXN]{};     // 标记每个3x3里出现了哪些数字
    bool vis_x[MAXN][MAXN]{};         // 标记行里出现了哪些数字
    bool vis_y[MAXN][MAXN]{};         // 标记列里出现了哪些数字
    char cmap[MAXN * MAXN]{};           // 地图
    int num{};                        // 需要生成的个数
    int sum[MAXN]{};

    vector<int> randVe;             // 随机1-9 顺序打乱
    vector<vector<char> > mapVe;        // 存放地图

    void randNum9() {
		
        for(int i = 1; i <= 222; i++){
			int l = rand() % 9, r = rand() % 9; 
//            cout << l << " " << r << endl;
            swap(randVe[l], randVe[r]);
        }
//        cout << "-------------------------------" << endl;
//        for(auto i : randVe) {
//            cout << i << " ";
//        }
//        cout << endl;
//        cout << "-------------------------------" << endl;
    }

    void print(int now) {
        for(int i = 1; i <= 81; i++){
            mapVe[now].push_back(cmap[i]);
        }
    }

    bool check(int xx, int yy, int val) {
        return !(vis[(xx + 2) / 3][(yy + 2) / 3][val] || vis_x[xx][val] || vis_y[yy][val] || sum[val] >= 9);
    }

    void dfs(int now, int cnt) {
        if(num == 0) return ;
        if(now == 82) {
            num--;
            print(cnt - 1);
            return ;
        }
        randNum9();                 //打乱1-9
        for(auto j : randVe) {
            int xx = (now + 8) / 9;
            int yy = (now - 1) % 9 + 1;
            if(check(xx, yy, j)) {
                vis[(xx + 2) / 3][(yy + 2) / 3][j] = vis_x[xx][j] = vis_y[yy][j] = true;
                cmap[now] = char ('0' + j);
                sum[j]++;
                dfs(now + 1, cnt);
                if(num == 0) return ;
                sum[j]--;
                vis[(xx + 2) / 3][(yy + 2) / 3][j] = vis_x[xx][j] = vis_y[yy][j] = false;
                cmap[now] = '\0';
            }
        }
    }

public:
    void init() {
        memset(vis, false, sizeof(vis));
        memset(vis_x, false, sizeof(vis_x));
        memset(vis_y, false, sizeof(vis_y));
        memset(cmap, '\0', sizeof(cmap));
        memset(sum, 0, sizeof(sum));
        randVe.clear();
        for(int i = 1; i <= 9; i++) randVe.push_back(i);
    }

    vector<vector<char> > getShuDu(int _num) {
        mapVe.clear();
        mapVe.resize(_num + 5);
        for(int i = 1; i <= _num; i++){
            init();
            num = 1;
            dfs(1, i);
        }
        return mapVe;
    }
};
int main()
{
    ios::sync_with_stdio(false);
//    ifstream fileIn;
    ofstream fileIn, fileOut;
    srand(time(0));
    for(int i = 1; i <= 20; i++){
        int num = rand() % 6666 + 5555;
//		int num = 1;
        cShuDu tmp;
        vector<vector<char> > ve = tmp.getShuDu(num);
        // 输出 in
        fileIn.open("data" + to_string(i) + ".in", ios::out);
        for(int j = 0; j < num; j++){
            int now = 0;
            cout << "*********************" << endl;
            int maxn = 80; 			// 最大问号数
            for(auto &t : ve[j]) {
                now++;
                if(now % 9 != 1) {
                    fileIn << " ";
                    cout << " ";
                }
                if((rand() + now) % 5 == 0 && maxn > 0) {
                    t = '?';
                    maxn--;
                }
                fileIn << t;
                cout << t;
                if(now % 9 == 0) {
                    fileIn << endl;
                    cout << endl;
                }
            }
            cout << "*********************" << endl;
        }
        fileIn.close();

        // 输出out
        fileOut.open("data" + to_string(i) + ".out", ios::out);
        for(int j = 0; j < num; j++) {
            int now = 0;
            cout << "*********************" << endl;
            for(auto t : ve[j]) {
                now++;
                mp[(now - 1) / 9][(now - 1) % 9] = t;
            }
            dfs();
            for(int x = 0; x < 9; x++){
                for(int y = 0; y < 9; y++){
                    if(y != 0) {
                        cout << " ";
                        fileOut << " ";
                    }
                    fileOut << mp[x][y];
                    cout << mp[x][y];
                }
                fileOut << endl;
                cout << endl;
            }
            cout << "*********************" << endl;
            if(j != num - 1) {
                fileOut << endl;
                cout << endl;
            }
        }
        fileOut.close();
    }

    return 0;
}

HUSTOJ的spj

#include <bits/stdc++.h>
using namespace std;
char user_map[25][25];
char oj_map[25][25];
bool xx[25][25], yy[25][25], vis[25][25][25];
int main(int argc, char* argv[]) {
    
    FILE * f_in=fopen(argv[1],"r");//测试输入
	FILE * f_out=fopen(argv[2],"r");//测试输出
	FILE * f_user=fopen(argv[3],"r");//用户输出
	int ret=0; //AC=0,WA=1
	
	
	/*****spj代码区域*******/
	while(fgets(oj_map[1], 25, f_in) != NULL) {
		memset(xx, false, sizeof(xx));
		memset(yy, false, sizeof(yy));
		memset(vis, false, sizeof(vis)); 
		for(int i = 2; i <= 9; i++){
			fgets(oj_map[i], 25, f_in);
		}
		for(int i = 1; i <= 10; i++){
			fgets(user_map[i], 25, f_user);
		}
		for(int i = 1; i <= 9; i++){
			for(int j = 0; j < 18; j += 2) {
				int tmp = (j + 2) / 2;
				if(oj_map[i][j] == '?') {
					;
				}
				else{
					if(oj_map[i][j] != user_map[i][j]){
						ret = 1;
					}
				}
				
				if(vis[(i + 2) / 3][(tmp + 2) / 3][user_map[i][j] - '0'] || xx[i][user_map[i][j] - '0'] || yy[tmp][user_map[i][j] - '0']){
					ret = 2;
				}
				vis[(i + 2) / 3][(tmp + 2) / 3][user_map[i][j] - '0'] = xx[i][user_map[i][j] - '0'] = yy[tmp][user_map[i][j] - '0'] = true;
			}
		} 
	}
	/*****spj-end********/ 
	
    fclose(f_in);
    fclose(f_out);
    fclose(f_user);
    return ret;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、数独说明:数独由九行,九列组成,又分为顺序排列的九宫。每行、每列及每宫都包含九个格,九个格中填放1到9的不重复的数字。 二、自动计算原理(三步法): 1、基础法:找出空格中唯一可填的数字。方法是,先假设某空格中可填入九个数字,然后去掉所在行、所在列、所在宫中的已有数字。余下如果是唯一一个数字,那么这个数字就是结果 2、找唯一法:例如果某行、某列或某宫中只剩一个空格,那么九个数字中缺少的那个就是结果。 3、求唯余法:对于存在多个可能值的空格,循环取其中一个作为假设值,然后反复利用方法1和方法2去测试,如果出错冲突或导致别的空格无值可填时,说明假设的值是错误的。并对别剩余未找到唯一值的空格进行同样操作,直至找到可行的一个方案。 三、自动出题,是自动求的反向过程,先给出答案,再组合题目: 1、数独难易程度跟数独已知的数字个数有一定关系,但不是必然关系。可分为四级,根据网友“数独难度分级”的文章https://wenku.baidu.com/view/af550ed51a37f111f1855ba0.html,结果是分布在0到1之间的一系列值,值越少越容易。 2、出题时,先利用随机数往81个空格中填入不冲突的值。方法是,因为对角线的三宫中的数字互不干扰,用随机数填充这三宫,然后根据数独规则要求随机填入另外六宫。 3、这是最终结果,然后根据难易要求,随机将结果中的一定数量(可以用随机数)的方格清空。数独题已经形成。再根据网友提供的级别计算公式,计算形成的数独题的难易程度是否符合要求。(此时的数独答案不是唯一的) 4、难易程度具体计算公式是:两个空格所有可能值如果有依赖关系值为1,没依赖关系值为0。如此汇总所有空格之间的关系值为A,再除以空格个数B的18倍。即A/(18*B)。0—0.25为0级,0.25—0.5为1级,0.5—0.75为2级,0.75—1为3组。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值