如何正确获得数独网格9x9? [英] How do I get a sudoku grid 9x9 properly ?

查看:100
本文介绍了如何正确获得数独网格9x9?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <bits/stdc++.h>
#include <cstdlib>

using namespace std;

#define SIZE 9

int table[SIZE][SIZE];




void init_table();
void print_table();
void fill_table(int);
void rand_mark(int,int);
bool isValid () ;




void init_table(){
	for (int i=0; i<SIZE; i++){
		for (int j=0; j<SIZE; j++){
			table[i][j] = 0;
		}
	}
}

void print_table(){
	for (int i=0; i<SIZE; i++){
		for (int j=0; j<SIZE; j++){
			if (table[i][j] != 0)
				cout << table[i][j] << "|";
			else
				cout << ' ' << "|";
		}
		cout << endl;
	}
}


void fill_table(int num){
	int x_pos;
	int y_pos;


	int counter = 0;
    while (counter < num){
        x_pos = (rand() % (SIZE));
        y_pos = (rand() % (SIZE));

        if(table[x_pos][y_pos] == 0 ) {
            rand_mark (x_pos, y_pos);
            counter++;
		}
	}
}

void rand_mark(int x_pos, int y_pos){
	int r_num;
	bool exit = false;

	while (exit == false ){
		exit = true;
		r_num = (rand() % (SIZE));

		for (int i=0; i<SIZE; i++)
			if (table[x_pos][i] == r_num || table[i][y_pos] == r_num){
				exit = false;
				break;
			}
	};

	table[x_pos][y_pos] = r_num;
}


bool isValid(int i, int j, const int table[][9])
{
 // Check whether grid[i][j] is valid at the i's row

    for (int column = 0; column < 9; column++)
        if (column != j && table[i][column] == table[i][j])
            return false;

 // Check whether grid[i][j] is valid at the j's column
    for (int row = 0; row < 9; row++)
        if (row != i && table[row][j] == table[i][j])
            return false;

 // Check whether grid[i][j] is valid in the 3-by-3 box
    for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)
        for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
            if (row != i && col != j && table[row][col] == table[i][j])
            return false;
    return true; // The current value at grid[i][j] is valid

}



int main(){
    srand(time(NULL));
	int num;
	init_table();
    cout << "Generate how many numbers (40 max)? ";
    cin >> num;

    while (true) {
	fill_table(num);


	print_table();
    }



}





我是什么尝试过:



i有功能检查网格是否遵循数独规则但无法使其工作



What I have tried:

i have function to check if the grid follow sudoku rule but can't get it to work

推荐答案

不要用随机数填充数独网格:结果几乎没有机会获得有效的 - 更不可解决的网格。特别是如果你不去检查任何数字!



我从这里开始:数独算法:在0.018秒内生成有效的数独 [ ^ ] - 它在VB中,而不是C ++,但它是一个非常琐碎的语言,这是你想要的算法。

或者更好,有这样的:数独谜题的产生:从容易到邪恶

[ ^ ]涵盖了整个过程,包括解算器,以及你生成的数独谜题中的挖洞来改变难度级别。





我刚刚运行了数字,以及有效的Sudo数量ku网格(忽略孔)是:

Don't fill a Sudoku grid with random numbers: there is almost no chance you will get a valid - much less solvable - grid as a result. Particularly if you don't do any checking of any of the digits as you go!

I'd start here: Sudoku Algorithm: Generates a Valid Sudoku in 0.018 seconds[^] - it's in VB, not C++ but it's a pretty trivial language and it's the algorithm you are after.
Or better, there is this: Sudoku Puzzles Generating: from Easy to Evil
[^] which covers the whole process, including solvers, and "digging holes" in your generated sudoku puzzle to change the difficulty level.

[edit]
I just ran the numbers, and the number of valid Sudoku grids (ignoring holes) is:
6,670,903,752,021,072,936,960

您可以使用随机数生成的可能不同的9 x 9网格的数量为:

The number of possible different 9 x 9 grids you can generate with random numbers is:

196,627,050,475,552,913,618,075,908,526,912,116,283,103,450,944,214,766,927,315,415,537,966,391,196,809

给你一个概率

Giving you a probability of

1:29,475,324,151,691,010,931,910,048,396,714,644,277,009,639,903,774,243,212

仅使用随机数生成有效的数独网格......可能需要一段时间...... :)

[/ edit]

of generating a valid Sudoku grid using random numbers alone...Might take you a while... :)
[/edit]


我将从函数和原型匹配开始:

I would start by making function and prototype match:
bool isValid () ;
...
bool isValid(int i, int j, const int table[][9]) {



未使用 isValid 的事实,也无济于事。


The fact that isValid is not used, will not help either.


这篇关于如何正确获得数独网格9x9?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆