日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java黑皮书课后题第8章:*8.24(检验数独的解决方案)程序清单8-4通过检测棋盘上的每个数字是否是有效的,从而检验一个解决方案是否是有效的。重写该程序,通过检验是否每行、每列、每个小方盒中具有

發布時間:2024/7/23 java 31 豆豆

*8.24(檢驗數獨的解決方案)程序清單8-4通過檢測棋盤上的每個數字是否是有效的,從而檢驗一個解決方案是否是有效的。重寫該程序

  • 題目
    • 題目描述
    • 程序清單8-4
    • 破題
  • 代碼

題目

題目描述

*8.24(檢驗數獨的解決方案)程序清單8-4通過檢測棋盤上的每個數字是否是有效的,從而檢驗一個解決方案是否是有效的。
重寫該程序,通過檢驗是否每行、每列、每個小方盒中具有數字1到9來檢測解決方案的有效性

程序清單8-4

import java.util.Scanner;public class qingdan {public static void main(String[] args) {// Read a Sudoku solutionint[][] grid = readASolution();System.out.println(isValid(grid) ? "Valid solution" : "Invalid solution");}/** Read a Sudoku solution from the console */public static int[][] readASolution(){// Create a ScannerScanner input = new Scanner(System.in);System.out.println("Enter a Sudoku puzzle solution:");int[][] grid = new int[9][9];for (int i = 0 ; i < 9 ; i++){for (int j = 0 ; j < 9 ; j++){grid[i][j] = input.nextInt();}}return grid;}/** Check whether a solution is valid */public static boolean isValid(int[][] grid){for (int i = 0 ; i < 9 ; i++)for (int j = 0 ; j < 9 ; j++)if (grid[i][j] < 1 || grid[i][j] > 9 || !isValid(i, j, grid))return false;// The solution is validreturn true;}/** Check wheter grid[i][j] is valid in the grid */public static boolean isValid(int i, int j, int[][] grid){//Check whether grid[i][j] is unique in i's rowfor (int column = 0 ; column < 9 ; column++)if (column != j && grid[i][column] == grid[i][j])return false;//Check whether grid[i][j] is unique in j's columnfor (int row = 0 ; row < 9 ; row++)if (row != i && grid[row][i] == grid[i][j])return false;//Check whether grid[i][j] is unique in the 3-by-3 boxfor (int row = (i/3)*3 ; row < (i/3)*3+3; row++)for (int col = (j/3)*3 ; col < (j/3)*3+3 ;col++)if (!(row == i && col == j) && grid[row][col] == grid[i][j])return false;return true; // The current value at grid[i][j] is valid} }

運行示例:書上是valid solution,但代碼已經檢查過多遍沒有任何問題

Enter a Sudoku puzzle solution: 9 6 3 1 7 4 2 5 8 1 7 8 3 2 5 6 4 9 2 5 4 6 8 9 7 3 1 8 2 1 4 3 7 5 9 6 4 9 6 8 5 2 3 1 7 7 3 5 9 6 1 8 2 4 5 8 9 7 1 3 4 6 2 3 1 7 2 4 6 9 8 5 6 4 2 5 9 8 1 7 3 Invalid solution

破題

原:檢測棋盤上的每個數字是否是有效的
新:檢驗每行、每列以及每個小的方盒中具有數字1到9

代碼

import java.util.Scanner;public class Test8_24 {public static void main(String[] args) {// Read a Sudoku solutionint[][] grid = readASolution();System.out.println(isValid(grid) ? "Valid solution" : "Invalid solution");}/** Read a Sudoku solution from the console */public static int[][] readASolution(){// Create a ScannerScanner input = new Scanner(System.in);System.out.println("Enter a Sudoku puzzle solution:");int[][] grid = new int[9][9];for (int i = 0 ; i < 9 ; i++){for (int j = 0 ; j < 9 ; j++){grid[i][j] = input.nextInt();}}return grid;}/** Check whether a solution is valid */public static boolean isValid(int[][] grid){int[] num_count = new int[10];boolean bool = true;// 檢驗每行是否有1到9for (int i = 0 ; i < 9 ; i++){grid = to_zero(grid);for (int j = 0 ; j < 9 ; j++){++num_count[grid[i][j]];}for (int m = 1 ; m < 9 ; m++){if (num_count[m] != 1)bool = false;}}// 檢驗每列for (int j = 0 ; j < 9 ; j++){grid = to_zero(grid);for (int i = 0 ; i < 9 ; i++){++num_count[grid[i][j]];}for (int m = 1 ; m < 9 ; m++){if (num_count[m] != 1)bool = false;}}// 檢驗每個小的方盒for (int i = 0 ; i < 9 ; i++){for (int j = 0 ; j < 9 ; j++){for (int row = (i/3)*3 ; row < (i/3)*3+3; row++){grid = to_zero(grid);for (int col = (j/3)*3 ; col < (j/3)*3+3 ;col++){++num_count[grid[row][col]];}for (int m = 1 ; m < 9 ; m++){if (num_count[m] != 1)bool = false;}}}}return bool;}public static int[][] to_zero(int[][] array){for (int i = 0 ; i < array.length ; i++){for (int j = 0 ; j < array[i].length ; j++){array[i][j] = 0;}}return array;} } 與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Java黑皮书课后题第8章:*8.24(检验数独的解决方案)程序清单8-4通过检测棋盘上的每个数字是否是有效的,从而检验一个解决方案是否是有效的。重写该程序,通过检验是否每行、每列、每个小方盒中具有的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。