随机数独生成算法

Pull Requests
问题描述:数独是一种运用纸、笔进行演算的逻辑游戏。玩家需要根据9x9盘面上的已知数字,推理出所有剩余空格的数字,并满足每- -行、每- -列、每-一个九宫格内的数字均含1-9,不重复。

 

#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
#include<math.h>
#include<time.h>
#include <windows.h>
#include<string.h>
#define MAX 999

struct Node{
Node *up,*down,*left,*right,*colPtr,*rowPtr;
int row_num;
int col_elemCount;
};

struct player{
int m ;
int s ;
char name[20];
int level ;
player* next;
};


int row_size=593;
int col_size = 324;
int result[81];
int index = 0;
int sudoku[81] = {0};
int time_start = 0;
int time_end = 0;

void init(Node* head){

    head->left = head;
    head->right = head;
    head->up = head;
    head->down = head;

    for(int k = 0;k < row_size;k++){
        Node* newNode = (Node*)malloc(sizeof(Node));
        newNode->up = head;
        newNode->down = head->down;
        newNode->left = newNode;
        newNode->right = newNode;
        newNode->down->up = newNode;
        head->down = newNode;
        newNode->row_num = row_size-k;
        newNode->col_elemCount = 0;
    }
}
void init_col(Node* head){
    for(int j = 0;j < col_size;j++){
        Node* newNode = (Node*)malloc(sizeof(Node));
        newNode->right = head->right;
        newNode->left = head;
        newNode->down = newNode;
        newNode->up = newNode;
        newNode->right->left = newNode;
        head->right = newNode;
        newNode->col_elemCount = 0;
    }
}

void link(Node* head,int** matrix){
Node *current_row, *current_col, *current;
current_row = head;
for(int row = 0;row<row_size;row++){
    current_row = current_row->down;
    current_col = head;
    for(int col = 0;col<col_size;col++){
        current_col = current_col->right;
        if(matrix[row][col] == 0)
            continue;

        Node* newNode = (Node*)malloc(sizeof(Node));
        newNode->colPtr = current_col;
        newNode->rowPtr = current_row;
        newNode->down = current_col;
        newNode->up = current_col->up;
        newNode->up->down = newNode;
        current_col->up = newNode;

        if(current_row->col_elemCount == 0){
            current_row->right = newNode;
            newNode->left = newNode;
            newNode->right = newNode;
            current_row->col_elemCount++;
        }

        current = current_row->right;
        newNode->left = current->left;
        newNode->right = current;
        newNode->left->right = newNode;
        current->left = newNode;

        current_col->col_elemCount++;
        }
    }    
}

int** create_matrix()
{
    int** matrix = (int**)malloc(row_size*sizeof(int*));

    for(int m=0;m<row_size;m++)
        matrix[m] = (int*)malloc(col_size*sizeof(int));

    for(int r=0;r<row_size;r++)
        for(int c=0;c<col_size;c++)
            matrix[r][c] = 0;
   
    int i = 0;
    for (int x = 0; x < 81; x++){
        int val = sudoku[x];
        if (val != 0){
            matrix[i][x] = 1;
            matrix[i][81 + x/9*9 + val -1] = 1;
            matrix[i][162 + x%9*9 + val -1] = 1;
            matrix[i][243 + (x/9/3*3+x%9/3)*9 +val -1] = 1;
            i++;

            continue;
        }

        for (int

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值