LeetCode题解:数独游戏求解方法

在上一篇LeetCode题解:数独游戏有效性检测介绍了如何判断一个待填写的数独是否有效,本篇将进一步通过回溯法 求解填充一个完整的数独。

图示:

求解结果:

 

解题思路:

 

/*
回溯法:
1、遍历输入单元格,如果一个单元格为空,则填入该行为空的数字,更新对应的行、列、子单元;
2、判断填入数字是否满足数独有效性要求,如果不满足,填入该空格可填入的另一个有效数字;
3、重复1,2过程直到每行扫描结束,进行下一行扫描
*/

(注释:该方法类似填入一个数字,不断尝试数字,如果填错了就把填入的数字删除,换一个数字重新尝试。玩过数独,其实有很多有意思的解法,感兴趣的同学可以搜索 更有趣的解法。这样对求解是很复杂的,但对机器来说却很方便。)

c++代码实现:

class Solution {
    //init state
    bool rows[9][9];
    bool cols[9][9];
    bool boxes[9][9];
  public:
	  bool solveSudoku(vector<vector<char>>& board, int x, int y) {
		  if (y == board[x].size())
		  {
			  x++;
			  y = 0;
		  }
		  if (x == board.size()){
			  return true;
          }
          //fill value to .
		  if (board[x][y] != '.'){
              return solveSudoku(board, x, y + 1);
          }
			  
		  // find . to insert value
		  for (char c = '1'; c <= '9'; c++)
		  {
			  int box = x / 3 * 3 + y / 3;
			  if (rows[x][c - '1'] || cols[y][c - '1'] || boxes[box][c - '1']){
                  continue;
              }
              //insert value
			  board[x][y] = c;
              //update state
			  rows[x][c - '1'] = cols[y][c - '1'] = boxes[box][c - '1'] = true;
			  if (solveSudoku(board, x, y + 1)){
                  return true;
              }
			  //back track, set . do it again
			  rows[x][c - '1'] = cols[y][c - '1'] = boxes[box][c - '1'] = false;
			  board[x][y] = '.';
		  }
		  return false;
	  }
	  void solveSudoku(vector<vector<char>>& board) {
		  memset(rows, 0, sizeof(rows));
		  memset(cols, 0, sizeof(cols));
		  memset(boxes, 0, sizeof(boxes));
		  // fill rows
		  for (int i = 0; i < board.size(); i++){
             for (int j = 0; j < board[i].size(); j++)
			  {
				  char c = board[i][j];
				  if (c == '.'){
                      continue;
                  }
                 //set value to update state
                  int box = i / 3 * 3 + j / 3;
				  rows[i][c - '1'] = cols[j][c - '1'] = boxes[box][c - '1'] = true;
			  } 
          }
          //sub function to fill the . with right value
		  solveSudoku(board, 0, 0);
	  }
  };

source link:sudoku-solver

reference:https://leetcode.com/problems/sudoku-solver/discuss/394629/C%2B%2B-faster-than-100

Practice makes perfect !

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值