随机生成数独

版权声明:如若转载请标明出处 https://blog.csdn.net/hwy499/article/details/90243851
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int mapp[9][9];//记录数独九宫格
int show_mapp[9][9];//记录数独九宫格
bool check(int x,int y,int num){ //检测当前mapp[x][y]是否可以放置num
    for(int i = 0; i < 9; i++){
        if(mapp[i][y] == num&&i!=x){//检测当前列是否能放num
            return false;
        }
        if(mapp[x][i] == num&&i!=y){//检测当前行能否放置num
            return false;
        }
    }
    //检测当前的小九宫格内能否放置num
    int row = x/3;
    int col = y/3;  
    for(int i = row*3; i < (row+1)*3; i++){
        for(int j = col*3; j < (col+1)*3; j++){
            if(num==mapp[i][j]&&x!=i&&y!=j){
                return false;
            }
        }
    }
    return true;
}
bool flag = false; //DFS是否成功产生一个数独
int num[9]; //存放1-9的数字
bool is_use[10];//标记num[i]是否生成过
void dfs(int step){
	if(flag){
		return ;
	}
    if(step==81){//成功产生一个数独
        for(int i = 0; i < 9; i++){
            printf("%d",mapp[i][0]);
            for(int j = 1; j < 9; j++){
                printf(" %d",mapp[i][j]);
            }
            printf("\n");
        }
        for(int i = 0; i < 9; i++){
			for(int j = 0; j < 9; j++){
				show_mapp[i][j] = mapp[i][j];
			}
		}
        flag = true;
        return ;
    }else{
        int x = step/9;
        int y = step%9;
        if(mapp[x][y]== 0){
            for(int i = 0; i < 9; i++){
                if(check(x,y,num[i])){
                    mapp[x][y] = num[i];
                    dfs(step+1);
                    mapp[x][y] = 0;
                }
            }
        }else{
            dfs(step+1);
        }
    }
}
void randShudu(){ //产生随机序列(1-9)
	memset(is_use,false,sizeof(is_use));
	for(int i = 0; i < 9; i++){
		num[i] = rand()%9+1;
		while(is_use[num[i]]){
	   	 	num[i] = rand()%9+1;
	    }
	   	is_use[num[i]] = true;
	 }	   		   	 	 	   	 	
}
void shielding(){
	for(int i = 0; i < 3; i++){
		for(int j = 0; j < 3; j++){
			int n = rand()%9+1;
			int row = i*3;
			int col = j*3;
			while(n--){
				int  c = rand()%3;
				int  r = rand()%3;
				show_mapp[row+c][col+r] = 0;
			}
		    
		}
	}
	printf("屏蔽了\n");
	for(int i = 0; i < 9; i++){
        printf("%d",show_mapp[i][0]);
        for(int j = 1; j < 9; j++){
            printf(" %d",show_mapp[i][j]);
        }
        printf("\n");
    }
}
bool check1(int x,int y,int num){
    for(int i = 0; i < 9; i++){
        if(show_mapp[i][y] == num&&i!=x){
            return false;
        }
        if(show_mapp[x][i] == num&&i!=y){
            return false;
        }
    }
    int row = x/3;
    int col = y/3;
    for(int i = row*3; i < (row+1)*3; i++){
        for(int j = col*3; j < (col+1)*3; j++){
            if(num==show_mapp[i][j]&&x!=i&&y!=j){
                return false;
            }
        }
    }
    return true;
}
void dfss(int step){
    if(step==81){
    	printf("填充完毕\n");
        for(int i = 0; i < 9; i++){
            printf("%d",show_mapp[i][0]);
            for(int j = 1; j < 9; j++){
                printf(" %d",show_mapp[i][j]);
            }
            printf("\n");
        }
        return ;
    }else{
        int x = step/9;
        int y = step%9;
        if(show_mapp[x][y]== 0){
            for(int i = 0; i < 9; i++){
                if(check1(x,y,num[i])){
                    show_mapp[x][y] = num[i];
                    dfss(step+1);
                    show_mapp[x][y] = 0;
                }
            }
        }else{
            dfss(step+1);
        }
    }
}
int main(){
	string s;
	while(cin>>s&&s[0]!='N'){
		 randShudu();
		 memset(mapp,0,sizeof(mapp));
	   	 flag = false;
	   	 dfs(0);
	   	 shielding();
	   	 randShudu();
	   	 dfss(0);
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hwy499/article/details/90243851