数独游戏的两种编程思路+代码

###数独
方法一:
设定三个方法;分别为行不重复,列不重复,单元格不重复;在判断是否重复的时候用了一个Boolean数组,默认值为false,若角标位置为true时那么说明已经重复了

需求:判断是否为数独矩阵
 /* 思路:当每行元素不得重复,并且每列元素不得重复,并且每个小方阵也不得重复*/
 public static void main(String[] args) {
		// TODO Auto-generated method stub
		int grid[][]={
				{5,3,4,6,7,8,9,1,2},
				{6,7,2,1,9,5,3,4,8},
				{1,9,8,3,4,2,5,6,7},
				{8,5,9,7,6,1,4,2,3},
				{4,2,6,8,5,3,7,9,1},
				{7,1,3,9,2,4,8,5,6},
				{9,6,1,5,3,7,2,8,4},
				{2,8,7,4,1,9,6,3,5},
				{3,4,5,2,8,6,1,7,9}};
		if(IsAllrow(grid)&&IsAllcol(grid)&&IsAllcell(grid)){
			System.out.println("是 数独");
		}else{
			System.out.println("不是数独");
		}
	}

	private static boolean IsAllcell(int[][] grid) {
		// TODO Auto-generated method stub
		//找基点
		for(int i=0;i<9;i+=3){
			for(int j=0;j<9;j+=3){
				//找偏移量,小矩阵
				boolean cell[]=new boolean[9];
				for(int x=0;x<3;x++){
					for(int y=0;y<3;y++){
						if(cell[grid[x+i][y+j]-1]==false){
							cell[grid[x+i][y+j]-1]=true;
						}else{
							return false;
						}
					}
				}
			}
		}
		return true;
	}

	private static boolean IsAllcol(int[][] grid) {
		// TODO Auto-generated method stub
		for(int i=0;i<grid.length;i++){
			boolean row[]=new boolean[9];
			for(int j=0;j<grid[i].length;j++){
				if(row[grid[j][i]-1]==false){
					row[grid[j][i]-1]=true;
				}else{
					return false;
				}
			}
		}
		return true;
	}
	

	private static boolean IsAllrow(int[][] grid) {
		// TODO Auto-generated method stub
		for(int i=0;i<grid.length;i++){
			boolean row[]=new boolean[9];
			for(int j=0;j<grid[i].length;j++){
				if(row[grid[i][j]-1]==false){
					row[grid[i][j]-1]=true;
				}else{
					return false;
				}
			}
		}
		return true;
	}
}

方法二:
依然是判断行不能重复列不能重复,这个用的方法是,分为以下几个情况:
一个大情况为grid里的数字不能超过1-9;基于大情况的前提
我们拿出一行,看这一行里有没有和某一个数字相同,那就是行不变列编,并且去除自己本身你这列,比的是这行的其他列;
我们再拿出一列,看这一列里有没有和这个数字相同的,那就是列不变行变,并且去除自己本身你这行,比的是这列的其他行;
我们再拿出一个单元格,看这个单元格里有没有和这个数字相同的,并且去除本身的行列,比的是剩余单元格的元素。


public class Demo_test3_1 {

   public static void main(String[] args) {
   	// TODO Auto-generated method stub
   	int grid[][]={
   			{5,3,4,6,7,8,9,1,2},
   			{6,7,2,1,9,5,3,4,8},
   			{1,9,8,3,4,2,5,6,7},
   			{8,5,9,7,6,1,4,2,3},
   			{4,2,6,8,5,3,7,9,1},
   			{7,1,3,9,2,4,8,5,6},
   			{9,6,1,5,3,7,2,8,4},
   			{2,8,7,4,1,9,6,3,5},
   			{3,4,5,2,8,6,1,7,9}
   			};
   System.out.println(Isvalid(grid)?"Valid solution":"Not valid solution");
   }

   private static boolean Isvalid(int[][] grid) {
   	// TODO Auto-generated method stub
   	for (int  i= 0; i < grid.length; i++) {
   		for (int j = 0; j < grid[i].length; j++) {
   			if(grid[i][j]<1||grid[i][j]>9) {
   				return false;
   			}
   		}
   	}
   	return true;
   	
   }
   public static boolean Isvalid(int [][]grid,int i,int j) {
   	for(int colume=0;colume<9;colume++) {
   		if(colume!=j&&grid[i][colume]==grid[i][j]) {//i行中的元素,与grid[i][j]这个元素比有重复的
   			return false;
   		}
   	}
   	for(int row=0;row<9;row++) {
   		if(row!=i&&grid[i][j]==grid[row][j]) {
   			return false;
   		}
   	}
   	for(int row=(i/3)*3;row<9;row++) {
   		for(int col=(j/3)*3;col<9;col++) {
   			if(row!=i&&col!=j&&grid[i][j]==grid[row][col]) {
   				return false;
   			}
   		}
   	}
   return true;	
   }
}

猜你喜欢

转载自blog.csdn.net/qq_43157982/article/details/84142484