Valid Sudoku
2015-08-22 14:41
225 查看
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character
![](http://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
连着几天没敲代码,感觉还是有点手生。数独题,不是很难,但也花了一些时间。
代码:
The Sudoku board could be partially filled, where empty cells are filled with the character
'.'.
![](http://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
连着几天没敲代码,感觉还是有点手生。数独题,不是很难,但也花了一些时间。
代码:
package code; public class Sudoku { public static void main(String[] args) { char [][] board ={ {'.','8','7','6','5','4','3','2','1'}, {'2','.','.','.','.','.','.','.','.'}, {'3','.','.','.','.','.','.','.','.'}, {'4','.','.','.','.','.','.','.','.'}, {'5','.','.','.','.','.','.','.','.'}, {'6','.','.','.','.','.','.','.','.'}, {'7','.','.','.','.','.','.','.','.'}, {'8','.','.','.','.','.','.','.','.'}, {'9','.','.','.','.','.','.','.','.'},} ; System.out.print("res:"+ new Sudoku().isValidSudoku(board)); } // row column 3*3 public boolean isValidSudoku(char[][] board) { int sets = board.length; boolean[] exist_x = new boolean[10]; boolean[] exist_y = new boolean[10]; for (int i = 0; i < exist_x.length; i++) { exist_x[i] = false; exist_y[i] = false; } // row column for (int i = 0; i < 9; i++) { // init for (int k = 0; k < exist_x.length; k++) { exist_x[k] = false; exist_y[k] = false; } for (int j = 0; j < 9; j++) { // row if (board[i][j] != '.') { int num_x = Integer.parseInt(board[i][j] + ""); if (exist_x[num_x] == true) return false; exist_x[num_x] = true; } // column if (board[j][i] != '.') { int num_y = Integer.parseInt(board[j][i] + ""); if (exist_y[num_y] == true) return false; exist_y[num_y] = true; } } } // 3*3 boolean[] exist = new boolean[10]; for (int i = 0; i < exist_x.length; i++) { exist[i] = false; } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { // clear for (int q = 0; q < exist_x.length; q++) { exist[q] = false; } for (int k = 0; k < 3; k++) { for (int t = 0; t < 3; t++) { if (board[3*i+k][3*j+t] != '.') { int num = Integer.parseInt(board[3*i+k][3*j+t] + ""); if (exist[num] == true) return false; exist[num] = true; } } } } } return true; } }
相关文章推荐
- 关于移动文件指针
- Makefile中eval函数用法
- 收发传真包的消息交互
- Spring Auto-Wiring Beans with @Autowired annotation
- 【科普】为什么WiFi自动信道选到的信道多数在1/6/11
- div显示和隐藏
- c++网络通信(与服务器通信聊天)和c#网络通信
- 深度学习调参备忘
- IM项目,使用openfire服务,Smack的android端概述
- ATM机编程
- [8-20]关机命令shutdown、halt、poweroff、reboot小结
- Spring中控制器方法返回字符串的一些细节
- eclipse通过Remote Java Application调试Uiautomator的方法
- C#通过R.Net调用R程序
- 冒泡排序
- 关于file.writelines换行符的添加
- 最小生成树(prime算法、kruskal算法) 和 最短路径算法(floyd、dijkstra)
- 数据结构实验之图论二:基于邻接表的广度优先搜索遍历 (BFS)
- 开发记录
- 欧拉函数