码迷,mamicode.com
首页 > 其他好文 > 详细

Valid Sudoku

时间:2015-05-09 13:04:55      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

https://leetcode.com/problems/valid-sudoku/

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 ‘.‘.

技术分享

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.

 

 1 public class Solution {
 2     public static boolean isValidSudoku(char[][] board) {
 3         boolean[][]row=new boolean[9][10];
 4         boolean[][]col=new boolean[9][10];
 5         boolean[][]box=new boolean[9][10];
 6         for(int i=0;i<9;i++){
 7             for(int j=0;j<9;j++){
 8             if(board[i][j]==‘.‘) continue;
 9             int x=board[i][j]-‘0‘;
10             int boxnum=0;
11             if(i<3&&j<3) boxnum=0;
12             else if(i<3&&j>=3&&j<6) boxnum=1;
13             else if(i<3&&j>=6&&j<9) boxnum=2;
14             else if(i>=3&&i<6&&j<3) boxnum=3;
15             else if(i>=3&&i<6&&j>=3&&j<6) boxnum=4;
16             else if(i>=3&&i<6&&j>=6) boxnum=5;
17             else if(i>=6&&j<3) boxnum=6;
18             else if(i>=6&&j>=3&&j<6) boxnum=7;
19             else if(i>=6&&j>=6) boxnum=8;
20              if(row[i][x]==true||col[j][x]==true||box[boxnum][x]==true){
21                  return false;
22              }
23              row[i][x]=true;col[j][x]=true;box[boxnum][x]=true;
24             }
25         }
26         return true;
27     }
28     public static void main(String[]args){
29     String[]t={".87654321","2........","3........","4........","5........","6........","7........","8........","9........"};
30     char[][]board=new char[9][9];
31     for(int i=0;i<9;i++){
32         board[i]=t[i].toCharArray();
33         System.out.println(board[i]);
34     }
35     System.out.println(isValidSudoku(board));
36     }
37 }

 

Valid Sudoku

标签:

原文地址:http://www.cnblogs.com/qq1029579233/p/4489656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!