import java.util.Scanner;
public class Sudoku{
public static int array [][] = new int [9][9];
public static boolean checkRow(int row){
int sum = 0;
for(int i = 0; i < array.length; i++){
sum += array[row][i];
}
if( sum < 45 || sum > 45 ){
return false;
}
return true;
}
public static boolean checkColumn(int column){
int sum = 0;
for(int i = 0; i < array.length; i++){
sum += array[i][column];
}
if( sum < 45 || sum > 45 ){
return false;
}
return true;
}
public static boolean checkLittlePalaceGrid(int box){
int sum = 0;
int row = (box / 3) * 3;
int column = (box < 3) ? box*3 : (box < 6) ? (box - 3)*3 : (box - 6)*3;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
sum += array[i + row][j + column];
}
}
if( sum < 45 || sum > 45 ){
return false;
}
return true;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
boolean row=true,column=true,lpg=true;
while( sc.hasNext() ){
for(int j = 0; j < 9; j++ ){
int a [] = {0,0,0,0,0,0,0,0,0};
for(int k = 0; k < 9; k++){
a[k] = sc.nextInt();
}
array[j] = a;
}
int i = 0;
while(i < array.length){
row = checkRow(i);
column = checkColumn(i);
lpg = checkLittlePalaceGrid(i);
if(row == false || column == false || lpg == false){
System.out.println("no");
break;
}
if( i == array.length - 1 ){
System.out.println("yes");
}
i++;
}
}
}
}
題目
2012年4月6日 星期五
Java 數獨判斷
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言