LeetCode OJ:Sudoku Solver
来源:互联网 发布:淘宝如何清洗订单 编辑:程序博客网 时间:2024/04/28 13:11
Sudoku Solver
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.
A sudoku puzzle...
...and its solution numbers marked in red.
暴力深搜
class Solution { int map[10][10];//预先赋值的数组用来判断第i行第j列是第几个九宫格 bool r[10][10];//r[i][j]表示第i行是否已经有j了,如果有了就为true否则是false bool c[10][10];//c[i][j]表示第i列是否已经有j了,如果有了就为true否则是false bool s[10][10];//s[i][j]表示第i个九宫格中是否已经有j了,如果有了就为true否则是false(九宫格排序按从上到下从左到右) int counts, p[100], q[100];//counts记录的是一共有多少个格子没有填数字,p数组记录他们的行序号,q数组记录他们的列序号 //bool flag;//记录搜索的状态,如果找到答案了就为true,没找到为falsepublic: void MakeMap() { for(int i = 1; i <= 9; i++) { for(int j = 1; j <= 9; j++) { if(i <= 3) { if(j <= 3) map[i][j] = 1; else if(j <= 6) map[i][j] = 2; else map[i][j] = 3; } else if(i <= 6) { if(j <= 3) map[i][j] = 4; else if(j <= 6) map[i][j] = 5; else map[i][j] = 6; } else { if(j <= 3) map[i][j] = 7; else if(j <= 6) map[i][j] = 8; else map[i][j] = 9; } } } } void initial(vector<vector<char>> &board){ counts=0;memset(r,false,sizeof(r));memset(c,false,sizeof(c));memset(s,false,sizeof(s)); for(int i = 1; i <= 9; i++){for(int j = 1; j <= 9; j++){if(board[i-1][j-1] != '.'){r[i][board[i-1][j-1]-'0'] = true;c[j][board[i-1][j-1]-'0'] = true;s[map[i][j]][board[i-1][j-1]-'0'] = true;}else//记录没有填数字的格子{counts++;p[counts] = i;q[counts] = j;}}} } bool DFS(int cur,vector<vector<char>> &board) { if(cur == counts + 1) { return true; } for(int i = 1; i <= 9; i++)//p[cur]表示未填数字的格子的行序号,q[cur]表示未填数字的格子的列序号 { if(r[p[cur]][i] == false && c[q[cur]][i] == false && s[map[p[cur]][q[cur]]][i] == false) { board[p[cur]-1][q[cur]-1] = i + '0'; r[p[cur]][i] = true; c[q[cur]][i] = true; s[map[p[cur]][q[cur]]][i] = true; if(DFS(cur + 1 , board))return true; //board[p[cur]-1][q[cur]-1] = '.'; r[p[cur]][i] = false; c[q[cur]][i] = false; s[map[p[cur]][q[cur]]][i] = false; } } } void solveSudoku(vector<vector<char> > &board) { MakeMap();//map矩阵的初始化 initial(board);//初始化操作 DFS(1,board); }};
0 0
- LeetCode OJ:Sudoku Solver
- LeetCode OJ - Sudoku Solver
- LeetCode OJ Sudoku Solver
- LeetCode OJ --问题与解答 Sudoku Solver
- LeetCode OJ 之 Sudoku Solver(求数独)
- LeetCode OJ算法题(三十六):Sudoku Solver
- 【LeetCode】Valid Sudoku && Sudoku Solver
- [Leetcode]Sudoku Solver&&Valid Sudoku
- leetcode Valid Sudoku & Sudoku Solver
- Leetcode 37 Sudoku Solver
- [Leetcode] Sudoku Solver
- Leetcode 37: Sudoku solver
- LeetCode Sudoku Solver
- [LeetCode]Sudoku Solver
- Leetcode Sudoku Solver
- LeetCode-Sudoku Solver
- LeetCode - Sudoku Solver
- LeetCode:Sudoku Solver
- 用MDK+h-converter+ulink2烧写norflash sst39vf1601
- c#摄像头编程大全(源码)
- Matlab 学习 入门篇 三 M文件
- 【windowsApi运用】随机取点画线条---ShinePans
- 如何编写一个最简单的嵌入式操作系统(1)简单任务调度
- LeetCode OJ:Sudoku Solver
- java线程之CyclicBarrier
- 新博客开通
- popwindow的大小
- [01背包]HDU 3466 Proud Merchants
- BZOJ 1783 [Usaco2010 Jan]Taking Turns
- thinkphp 代理检测 AgentCheck
- 【C++运用】(控制台)-概率的运用,石头剪刀布,抛硬币,圆周率的计算---ShinePans
- 【备战蓝桥杯】USACO--> Transformation