• [leetcode]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.



    代码:

    public class Valid_Sudoku {  //java
    
    	public boolean isValidSudoku(char[][] board) {
            
    		int size = 9;
    		int [] member = new int[size];   //record if is occur; 
    		
    		//valid row
    		for(int i = 0; i < 9; i++){
    			
    			for(int k = 0; k < 9; k++)
    				member[k] = 0;
    			for(int j = 0; j < 9; j++){
    				if(board[i][j] == '.')
    					continue;
    				int pos = board[i][j]-'0';
    				if(member[pos-1] == 1)
    					return false;
    				else member[pos-1] = 1;
    			}
    		}
    		
    		//valid col
    		for(int i = 0; i < 9; i++){
    			for(int k = 0; k < 9; k++)
    				member[k] = 0;
    			for(int j = 0; j < 9; j++){
    				if(board[j][i] == '.')
    					continue;
    				int pos = board[j][i]-'0';
    				if(member[pos-1] == 1)
    					return false;
    				else member[pos-1] = 1;
    			}
    		}
    		
    		//valid cube
    		for(int ibegin = 0; ibegin < 9; ibegin = ibegin+3){
    			for(int jbegin = 0; jbegin < 9; jbegin = jbegin+3){
    				for(int k = 0; k < 9; k++)
    					member[k] = 0;
    				for(int i = ibegin; i < ibegin+3; i++){
    					for(int j = jbegin; j < jbegin+3; j++){
    						if(board[i][j] == '.')
    							continue;
    						int pos = board[i][j]-'0';
    						if(member[pos-1] == 1)
    							return false;
    						else member[pos-1] = 1;
    					}
    				}
    			}
    		}
    		return true;
        }
    	
    	public static void main(String [] args){
    		String[] boardStr = {"......5..",
    							 ".........",
    							 ".........",
    							 "93..2.4..",
    							 "..7...3..",
    							 ".........",
    							 "...34....",
    							 ".....3...",
    							 ".....52.."};
    		char [][] board = new char [9][9];
    		for(int i =0; i< boardStr.length; i++){
    			for(int j = 0; j<boardStr[i].length(); j++){
    				board[i][j] = boardStr[i].charAt(j);
    			}
    		}
    		
    		Valid_Sudoku vs = new Valid_Sudoku();
    		System.out.println(vs.isValidSudoku(board));
    	}
    }
    


  • 相关阅读:
    Struts学习之手动验证
    Struts学习之文件上传
    Struts学习之模型驱动
    Struts学习之类型转换
    Struts学习之自定义拦截器
    Java基础之"=="和 和 equals 方法的区别
    Hadoop学习之Hadoop案例分析
    Hadoop学习之Hadoop集群搭建
    Hadoop学习之自定义二次排序
    Hadoop学习之Mapreduce执行过程详解
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6905587.html
Copyright © 2020-2023  润新知