zoukankan      html  css  js  c++  java
  • 37. Sudoku Solver *HARD*

    Write a program to solve a Sudoku puzzle by filling the empty cells.

    Empty cells are indicated by the character '.'.

    You may assume that there will be only one unique solution.

    const int SodukuSize = 9;
    bool row_mask[SodukuSize][SodukuSize];
    bool col_mask[SodukuSize][SodukuSize];
    bool area_mask[SodukuSize][SodukuSize];
    
    bool initSudokuMask(vector< vector<char> > &board){
        //reset the memory
        memset(row_mask, false, sizeof(row_mask));
        memset(col_mask, false, sizeof(col_mask));
        memset(area_mask, false, sizeof(area_mask));
    
        //check each rows and cols
        for(int r=0; r<board.size(); r++){
            for (int c=0; c<board[r].size(); c++){
                if (!isdigit(board[r][c])) {
                    continue;
                };
                int idx =  board[r][c] - '0' - 1;
    
                //check the rows/cols/areas
                int area = (r/3) * 3 + (c/3);
                if (row_mask[r][idx] || col_mask[c][idx] || area_mask[area][idx] ){
                    return false;
                }
                row_mask[r][idx] = col_mask[c][idx] = area_mask[area][idx] = true;
            }
        }
        return true;
    }
    
    
    bool recursiveSudoKu(vector< vector<char> > &board, int row, int col){
    
        if (row >= SodukuSize) {
            return true;
        }
    
        if (col >= SodukuSize){
            return recursiveSudoKu(board, row+1, 0);
        }
        
        if (board[row][col] != '.'){
            return recursiveSudoKu(board, row, col+1);    
        }
        //pick a number for empty cell
        int area;
        for(int i=0; i<SodukuSize; i++){
            area = (row/3) * 3 + (col/3);
            if (row_mask[row][i] || col_mask[col][i] || area_mask[area][i] ){
                continue;
            }
            //set the number and sovle it recursively
            board[row][col] = i + '1';
            row_mask[row][i] = col_mask[col][i] = area_mask[area][i] = true;
            if (recursiveSudoKu(board, row, col+1) == true){
                return true;
            }
            //backtrace
            board[row][col] = '.';
            row_mask[row][i] = col_mask[col][i] = area_mask[area][i] = false;
        }
        return false;
    }
  • 相关阅读:
    《舌尖上的中国》精彩故事
    5年前的笔试题目
    遍历物理模型中的所有表,将表名、表代码、字段名、字段代码全部由小写改成大写
    MongoDB下载文件 百度盘共享
    认识MEAN开发框架[转]
    智能油田
    排课相关参数设置
    spring获取所有被装配类工具
    oracle常用sql集锦
    关于使用easyUI遇到过的一些坑
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/5245095.html
Copyright © 2011-2022 走看看