您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)怎么在Java中利用二維數(shù)組實(shí)現(xiàn)一個(gè)數(shù)獨(dú)問(wèn)題,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
具體如下:
(1)生成簡(jiǎn)易數(shù)獨(dú)
(2)生成數(shù)獨(dú)問(wèn)題**
代碼
import java.util.Random; import java.util.ArrayList; public class Suduku { /** *打印二維數(shù)組,數(shù)獨(dú)矩陣 */ public static void printArray(int a[][]) { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { System.out.print(" "+a[i][j]); if (0==((j+1)%3)) { System.out.print(" "); } } System.out.println(); if(0==((i+1)%3)) { System.out.println(); } } } /** * 產(chǎn)生一個(gè)1-9的不重復(fù)長(zhǎng)度為9的一維數(shù)組 */ public static ArrayList<Integer> creatNineRondomArray() { ArrayList <Integer>list = new ArrayList<Integer>(); Random random=new Random(); for (int i = 0; i < 9; i++) { int randomNum=random.nextInt(9)+1; while (true) { if (!list.contains(randomNum)) { list.add(randomNum); break; } randomNum=random.nextInt(9)+1; } } System.out.println("生成的一位數(shù)組為:"); for (Integer integer : list) { System.out.print(" "+integer.toString()); } System.out.println(); return list; } /** *通過(guò)一維數(shù)組和原數(shù)組生成隨機(jī)的數(shù)獨(dú)矩陣 * *遍歷二維數(shù)組里的數(shù)據(jù),在一維數(shù)組找到當(dāng)前值的位置,并把一維數(shù)組 *當(dāng)前位置加一處位置的值賦到當(dāng)前二維數(shù)組中。目的就是將一維數(shù)組為 *依據(jù),按照隨機(jī)產(chǎn)生的順序,將這個(gè)9個(gè)數(shù)據(jù)進(jìn)行循環(huán)交換,生成一個(gè)隨 *機(jī)的數(shù)獨(dú)矩陣。 * */ public static void creatSudokuArray(int[][]seedArray,ArrayList<Integer> randomList) { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { for (int k = 0; k < 9; k++) { if(seedArray[i][j]==randomList.get(k)) { seedArray[i][j]=randomList.get((k+1)%9); break; } } } } System.out.println("處理后的數(shù)組"); Suduku.printArray(seedArray); } public static void creatSudokuQuestion(int [][] a) { Random rand=new Random(); for(int i=0;i<9;i++){ for(int j=0;j<4;j++){ a[i][(int)rand.nextInt(9)]=0; } } Suduku.printArray(a); } // public static void main(String[] args) { int seedArray[][]={ {9,7,8,3,1,2,6,4,5}, {3,1,2,6,4,5,9,7,8}, {6,4,5,9,7,8,3,1,2}, {7,8,9,1,2,3,4,5,6}, {1,2,3,4,5,6,7,8,9}, {4,5,6,7,8,9,1,2,3}, {8,9,7,2,3,1,5,6,4}, {2,3,1,5,6,4,8,9,7}, {5,6,4,8,9,7,2,3,1} }; System.out.println("原始的二維數(shù)組:"); Suduku.printArray(seedArray); ArrayList<Integer> randomList=Suduku.creatNineRondomArray(); Suduku.creatSudokuArray(seedArray, randomList); System.out.println("生成數(shù)獨(dú)問(wèn)題:"); Suduku.creatSudokuQuestion(seedArray); } }
輸出:
原始的二維數(shù)組: 9 7 8 3 1 2 6 4 5 3 1 2 6 4 5 9 7 8 6 4 5 9 7 8 3 1 2 7 8 9 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 8 9 7 2 3 1 5 6 4 2 3 1 5 6 4 8 9 7 5 6 4 8 9 7 2 3 1 生成的一位數(shù)組為: 2 3 9 1 6 8 7 5 4 處理后的數(shù)組 1 5 7 9 6 3 8 2 4 9 6 3 8 2 4 1 5 7 8 2 4 1 5 7 9 6 3 5 7 1 6 3 9 2 4 8 6 3 9 2 4 8 5 7 1 2 4 8 5 7 1 6 3 9 7 1 5 3 9 6 4 8 2 3 9 6 4 8 2 7 1 5 4 8 2 7 1 5 3 9 6 生成數(shù)獨(dú)問(wèn)題: 0 5 7 9 6 3 0 0 0 9 6 3 0 0 0 0 5 7 0 2 4 1 0 7 9 6 0 5 0 1 6 0 9 2 0 0 6 0 9 2 0 0 5 7 0 2 0 8 0 7 1 0 3 9 7 1 5 0 0 6 4 8 2 3 0 6 4 8 2 7 0 5 4 8 2 7 0 0 3 9 6
關(guān)于怎么在Java中利用二維數(shù)組實(shí)現(xiàn)一個(gè)數(shù)獨(dú)問(wèn)題就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。