37. Sudoku Solver
来源:互联网 发布:ubuntu安装maven 编辑:程序博客网 时间:2024/06/09 16:12
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.
#include "SudokuSolver.h"bool SudokuSolver::check(vector<vector<char>> &board, int i, int j, char val){ //检验board[i][j]在值为val的时候是否有效 int row = i - i % 3, column = j - j % 3; for (int x = 0; x<9; x++) if (board[x][j] == val) return false; for (int y = 0; y<9; y++) if (board[i][y] == val) return false; for (int x = 0; x<3; x++) for (int y = 0; y<3; y++) if (board[row + x][column + y] == val) return false; return true;}bool SudokuSolver::solveSudoku(vector<vector<char>> &board, int i, int j){ if (i == 9) return true; if (j == 9) return solveSudoku(board, i + 1, 0); if (board[i][j] != '.') return solveSudoku(board, i, j + 1); for (char c = '1'; c <= '9'; c++) { if (check(board, i, j, c)) { board[i][j] = c; if (solveSudoku(board, i, j + 1)) return true; //这里若迭代后的结果不符合条件,要将该位置重新置为'.' board[i][j] = '.'; } } return false;}void SudokuSolver::solveSudoku(vector<vector<char>>& board) { solveSudoku(board, 0, 0);}
阅读全文
0 0
- LeetCode --- 37. Sudoku Solver
- LeetCode 37.Sudoku Solver
- [Leetcode] 37. Sudoku Solver
- 37. Sudoku Solver
- 37. Sudoku Solver(Hard)
- [leetcode] 37. Sudoku Solver
- Leetcode 37. Sudoku Solver
- 37. Sudoku Solver
- leetcode 37. Sudoku Solver
- 37. Sudoku Solver
- 37. Sudoku Solver
- LeetCode 37. Sudoku Solver
- 37. Sudoku Solver
- leetcode.37. Sudoku Solver
- LeetCode-37.Sudoku Solver
- leetcode 37. Sudoku Solver
- (Leetcode)37. Sudoku Solver
- 37. Sudoku Solver
- linux下一些简单的用户操作
- 使用Android studio把项目分享到Github
- python 删除指定路径下的csv文件
- peewee入门
- 实验之链队列
- 37. Sudoku Solver
- STM32 ST-LINK Utility安装及使用
- SSH免密码登录
- Android中WebView加载H5页面JS不执行的问题
- loj #6004. 「网络流 24 题」圆桌聚餐(最大流)
- 输出100个100以内的随机数(C++)
- DDL、DML和DCL的区别与理解
- Linux编程编程实战(三)
- Python 初探2