读书人

Java基础算法-数独

发布时间: 2013-03-21 10:08:17 作者: rapoo

Java基础算法--数独

转载自IT学习社区:http://bbs.itcast.cn/thread-6273-1-1.html

作者:梁桐

?

无聊时偶尔会玩一玩益智类游戏。有一种游戏叫“数独”,满足每一行、每一列、每一个粗线宫内的数字均含1-9,不重复

Java基础算法-数独

突发奇想,使用Java基础编写一个自己的获得数独数据小程序。

//? ?? ?? ?? ?? ?? ?? ?? ?print(soduku);? ?? ?? ?? ?? ? }? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???}?? ?? ???private static void changeValue(int[][] soduku, int currentRow, int currentCols, int num) {? ?? ?? ?? ?? ? int endRow = (currentRow / 3 + 1) * 3;? ?? ?? ?? ?? ? int endCols = (currentCols / 3 + 1) * 3;? ?? ?? ?? ?? ? for(int i = currentRow ; i < endRow ; i ++){? ?? ?? ?? ?? ?? ?? ?? ?int startCols = currentCols / 3 * 3;? ?? ?? ?? ?? ?? ?? ?? ?if(i == currentRow){? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???startCols = currentCols;? ?? ?? ?? ?? ?? ?? ?? ?}? ?? ?? ?? ?? ?? ?? ?? ?for(int j = startCols ; j < endCols ; j ++){? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???int temp = soduku[i][j];? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???if(temp == num){? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? soduku[i][j] = soduku[currentRow][currentCols];? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? soduku[currentRow][currentCols] = temp;? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???}? ?? ?? ?? ?? ?? ?? ?? ?}? ?? ?? ?? ?? ? }? ?? ???}?? ?? ???/**? ?? ?? ?* 查询当前的之前是否使用过num? ?? ?? ?* @param soduku? ?? ?? ?* @param currentRow? ?? ?? ?* @param currentCols? ?? ?? ?* @param num? ?? ?? ?* @return? ?? ?? ?*/? ?? ???private static boolean findHasValue(int[][] soduku, int currentRow, int currentCols, int num) {? ?? ?? ?? ?? ? return findAreaValue(soduku, currentRow, currentCols, num)? ?? ?? ?? ?? ?? ?? ?? ?|| findRowValue(soduku, currentRow, currentCols, num)? ?? ?? ?? ?? ?? ?? ?? ?|| findColsValue(soduku, currentRow, currentCols, num);? ?? ???}? ?? ???? ?? ???/**? ?? ?? ?* 当前行查询? ?? ?? ?* @param soduku? ?? ?? ?* @param currentRow? ?? ?? ?* @param currentCols? ?? ?? ?* @param num? ?? ?? ?* @return? ?? ?? ?*/? ?? ???private static boolean findRowValue(int[][] soduku, int currentRow, int currentCols, int num) {? ?? ?? ?? ?? ? for(int i = 0 ; i < currentCols ; i++ ){? ?? ?? ?? ?? ?? ?? ?? ?int exists = soduku[currentRow][i];? ?? ?? ?? ?? ?? ?? ?? ?if(exists == num ){? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???return true;? ?? ?? ?? ?? ?? ?? ?? ?}? ?? ?? ?? ?? ? }? ?? ?? ?? ?? ? return false;? ?? ???}? ?? ???? ?? ???/**? ?? ?? ?* 当前列查询? ?? ?? ?* @param soduku? ?? ?? ?* @param currentRow? ?? ?? ?* @param currentCols? ?? ?? ?* @param num? ?? ?? ?* @return? ?? ?? ?*/? ?? ???private static boolean findColsValue(int[][] soduku, int currentRow, int currentCols, int num) {? ?? ?? ?? ?? ? for(int i = 0 ; i < currentRow ; i ++){? ?? ?? ?? ?? ?? ?? ?? ?int exists = soduku[i][currentCols];? ?? ?? ?? ?? ?? ?? ?? ?if(exists == num){? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???return true;? ?? ?? ?? ?? ?? ?? ?? ?}? ?? ?? ?? ?? ? }? ?? ?? ?? ?? ? return false;? ?? ???}? ?? ???? ?? ???/**? ?? ?? ?* 当前区域查询? ?? ?? ?* @param soduku? ?? ?? ?* @param currentRow? ?? ?? ?* @param currentCols? ?? ?? ?* @param num? ?? ?? ?* @return? ?? ?? ?*/? ?? ???private static boolean findAreaValue(int[][] soduku, int currentRow, int currentCols, int num) {? ?? ?? ?? ?? ? //currentRow 1??currentCols 5? ?? ?? ?? ?? ? int startRow = currentRow / 3 * 3;??//3? ?? ?? ?? ?? ? int startCols = currentCols / 3 * 3;? ?? ?? ?? ?? ? for(int i = startRow ; i <= currentRow ; i ++ ){??// 345? ?? ?? ?? ?? ?? ?? ?? ?int cols = startCols + 3;? ?? ?? ?? ?? ?? ?? ?? ?if(i == currentRow){? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???cols = currentCols;? ?? ?? ?? ?? ?? ?? ?? ?}? ?? ?? ?? ?? ?? ?? ?? ?for(int j = startCols ; j < cols ; j ++) {? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???int exists = soduku[i][j];? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???if(exists == num){? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? return true;? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???}? ?? ?? ?? ?? ?? ?? ?? ?}? ?? ?? ?? ?? ? }? ?? ?? ?? ?? ? return false;? ?? ???}? ?? ???? ?? ????? ?? ???/**? ?? ?? ?* 生成数据? ?? ?? ?* @param soduku? ?? ?? ?*/? ?? ???private static void getData(int[][] soduku) {? ?? ?? ?? ?? ? for(int m = 0 ; m < 9 ; m ++){? ?? ?? ?? ?? ?? ?? ?? ?for(int n = 0 ; n < 9 ; n ++){? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???soduku[m][n] = (m % 3) * 3 + (n % 3) + 1 ;? ?? ?? ?? ?? ?? ?? ?? ?}? ?? ?? ?? ?? ? }? ?? ???}?? ?? ???? ?? ???private static List<Integer> lineNumList;? ?? ???private static List<Integer> getValueList(){? ?? ?? ?? ?? ? Random random = new Random();? ?? ?? ?? ?? ? List<Integer> list = new ArrayList<Integer>();? ?? ?? ?? ?? ? while(list.size() < 9){? ?? ?? ?? ?? ?? ?? ?? ?//保证行数据唯一? ?? ?? ?? ?? ?? ?? ?? ?int num = random.nextInt(10);? ?? ?? ?? ?? ?? ?? ?? ?if(!list.contains(num) && num > 0){? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???list.add(num);? ?? ?? ?? ?? ?? ?? ?? ?}? ?? ?? ?? ?? ? }? ?? ?? ?? ?? ? return list;? ?? ???}?? ?? ???/**? ?? ?? ?* 输出? ?? ?? ?* @param soduku? ?? ?? ?*/? ?? ???private static void print(int[][] soduku) {? ?? ?? ?? ?? ? for(int m = 0 ; m < 9 ; m ++){? ?? ?? ?? ?? ?? ?? ?? ?for(int n = 0 ; n < 9 ; n ++){? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???System.out.print(soduku[m][n]);? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???if( ( (n + 1) % 3 == 0 ) && n < 8 ){? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? System.out.print(" | ");? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???}? ?? ?? ?? ?? ?? ?? ?? ?}? ?? ?? ?? ?? ?? ?? ?? ?if( ( (m + 1) % 3 == 0 ) && m < 8){? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???System.out.println();? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???for(int i = 0 ; i < 15 ; i ++){? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? System.out.print("-");? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???}? ?? ?? ?? ?? ?? ?? ?? ?}? ?? ?? ?? ?? ?? ?? ?? ?System.out.println();? ?? ?? ?? ?? ? }? ?? ?? ?? ?? ? System.out.println("#############");? ?? ???}}?

读书人网 >其他相关

热点推荐