當前位置:首頁 » 操作系統 » 數獨演算法java

數獨演算法java

發布時間: 2024-10-24 21:55:37

java高手進,求一個數獨游戲java代碼

數獨(sudoku)的生成與破解
最近在網上比較流行的智力游戲。筆者本人也玩過,可以下個模擬游戲試試,簡單的還可以,太難就無從下手了。雖然偶腦子不好使,但偶是計算機科班出身,怕你不成,老規矩,編程破解。

首先,偶是第一次做數獨的程序,可能程序不強,以後有時間再改進改進。望高手評析。

還是把數獨游戲的規則說一說吧,或許你是剛剛聽說這個名字的朋友。數獨(sudoku),起源於瑞士,於1970 年代由美國的一家數學邏輯游戲雜志首先發表,當時名為Number Place。及後在日本大力推廣下得以發揚光大,於1984 年取名「數獨」,即「獨立的數字」的省略,在一個9x9的方格中,有81個小方格組成,然後又分9個大塊,每塊由3x3的方格組成,就是中國的九宮圖,大九宮裡面再套9個小九宮,共九九八十一個小格子,游戲開始前會有一些格子上寫好了數,你需要在剩下的格子里填數,真到把所有格子填滿,並且要求,任何一行或一列或者一個小九宮中沒有相同的數字,當然你只能用1-9之間的9個數字

以下是java代碼,能幫我轉換成c++的嗎?(轉換成功再追加100分,謝謝了。)
Java code public class Sudoku { /** * Print the specified Sudoku problem and its solution. The * problem is encoded as specified in the class documentation * above. * * @param args The command-line arguments encoding the problem. */ public static void main(String[] args) { int[][] matrix = parseProblem(args); writeMatrix(matrix); if (solve(0,0,matrix)) // solves in place writeMatrix(matrix); else System.out.println("NONE"); } static boolean solve(int i, int j, int[][] cells) { if (i == 9) { i = 0; if (++j == 9) return true; } if (cells[i][j] != 0) // skip filled cells return solve(i+1,j,cells); for (int val = 1; val <= 9; ++val) { if (legal(i,j,val,cells)) { cells[i][j] = val; if (solve(i+1,j,cells)) return true; } } cells[i][j] = 0; // reset on backtrack return false; } static boolean legal(int i, int j, int val, int[][] cells) { for (int k = 0; k < 9; ++k) // row if (val == cells[k][j]) return false; for (int k = 0; k < 9; ++k) // col if (val == cells[i][k]) return false; int boxRowOffset = (i / 3)*3; int boxColOffset = (j / 3)*3; for (int k = 0; k < 3; ++k) // box for (int m = 0; m < 3; ++m) if (val == cells[boxRowOffset+k][boxColOffset+m]) return false; return true; // no violations, so it's legal } static int[][] parseProblem(String[] args) { int[][] problem = new int[9][9]; // default 0 vals for (int n = 0; n < args.length; ++n) { int i = Integer.parseInt(args[n].substring(0,1)); int j = Integer.parseInt(args[n].substring(1,2)); int val = Integer.parseInt(args[n].substring(2,3)); problem[i][j] = val; } return problem; } static void writeMatrix(int[][] solution) { for (int i = 0; i < 9; ++i) { if (i % 3 == 0) System.out.println(" -----------------------"); for (int j = 0; j < 9; ++j) { if (j % 3 == 0) System.out.print("| "); System.out.print(solution[i][j] == 0 ? " " : Integer.toString(solution[i][j])); System.out.print(' '); } System.out.println("|"); } System.out.println(" -----------------------"); } } -----------------------
| 4 3 | 7 | 9 8 |
| 5 | 3 | |
| 1 | | 3 |
-----------------------
| 6 | 2 7 | |
| 4 7 | | 1 3 |
| | 5 4 | 9 |
-----------------------
| 2 | | 3 |
| | 5 | 4 |
| 5 4 | 1 | 2 6 |
-----------------------
-----------------------
| 2 4 3 | 7 1 6 | 9 5 8 |
| 9 8 5 | 2 3 4 | 7 1 6 |
| 7 1 6 | 8 9 5 | 3 4 2 |
-----------------------
| 6 3 9 | 1 2 7 | 5 8 4 |
| 4 5 7 | 9 6 8 | 1 2 3 |
| 8 2 1 | 5 4 3 | 6 7 9 |
-----------------------
| 1 6 2 | 4 7 9 | 8 3 5 |
| 3 7 8 | 6 5 2 | 4 9 1 |
| 5 9 4 | 3 8 1 | 2 6 7 |
-----------------------

熱點內容
勞拉與馬ftp 發布:2024-10-25 00:21:16 瀏覽:356
奪寶網站源碼 發布:2024-10-25 00:19:02 瀏覽:451
編程文本編輯器 發布:2024-10-25 00:09:28 瀏覽:968
編程徐帥 發布:2024-10-25 00:03:25 瀏覽:302
手機安卓模擬器如何打開文件 發布:2024-10-25 00:02:55 瀏覽:717
pythonday 發布:2024-10-24 23:55:47 瀏覽:422
g編譯c文件 發布:2024-10-24 23:55:03 瀏覽:290
電信上傳速度限制破解 發布:2024-10-24 23:44:17 瀏覽:450
戰地五為什麼連接不了伺服器 發布:2024-10-24 23:37:36 瀏覽:480
安卓如何下載國外網站 發布:2024-10-24 23:30:35 瀏覽:132