询问数独游戏中java中使用的数组 [英] asking about array used in java in a Sudoku game

查看:75
本文介绍了询问数独游戏中java中使用的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解这个funtcion /方法,但我很困惑为什么它使用2 ??下面给出了

//////////////////////////////////////// /////////////////

将空闲单元格位置存储到freeCellList中

int [] [] freeCellList = new int [numberOfFreeCells ] [2];

用于(LINE No 9)

//////////////////// ///////////////////////////////////////////





< pre lang =cs> / **获取拼图中的免费单元格列表* /

public static int [] [] getFreeCellList(int [] [] grid){

//确定空闲单元格的数量

int numberOfFreeCells = 0;

for(int i = 0; i& lt; 9; i ++)

for(int j = 0; j& lt; 9; j ++)

if(grid [i] [j] == 0)

numberOfFreeCells ++;



//将免费单元格位置存储到freeCellList

int [] [] freeCellList = new int [numberOfFreeCells] [2];

int count = 0;

for(int i = 0; i& lt; 9; i ++)

for(int j = 0; j& lt; 9; j ++)

if(grid [i] [j] == 0){

freeCellList [count] [0] = i;

freeCellList [count ++] [1] = j;

}



返回freeCellList;

}



整个代码在这里

公共类Sudoku {

public static void main(String [] args){

//读一个数独谜题

int [] [] grid = readAPuzzle();



if(! isValid(grid))

System.out.println(无效输入);

else if(search(grid)){

System.out.println(找到解决办法:);

printGrid(网格);

}

其他

System.out.println(无解决方案);

}



/ **从中读取数独谜题键盘* /

public static int [] [] readAPuzzle(){

//创建扫描仪

扫描仪输入=新扫描仪(系统.in);



System.out.println(输入数独谜题:);

int [] [] grid = new int [9] [9];

for(int i = 0;我< 9; i ++)

for(int j = 0; j< 9; j ++)

grid [i] [j] = input.nextInt();



返回网格;

}



/ **从中获取免费单元格列表谜题* /

public static int [] [] getFreeCellList(int [] [] grid){

//确定空闲单元格数

int numberOfFreeCells = 0;

for(int i = 0; i< 9; i ++)

for(int j = 0; j< 9; j ++)

if(grid [i] [j] == 0)

numberOfFreeCells ++;



//将免费单元格位置存储到freeCellList

int [] [] freeCellList = new int [numberOfFreeCells] [2];

int count = 0;

for(int i = 0 ; i< 9; i ++)

for(int j = 0; j< 9; j ++)

if(grid [i] [j] == 0 ){

freeCellList [count] [0] = i;

freeCellList [count ++] [1] = j;

}



返回freeCellList;

}



/ **打印网格中的值* /

public static void printGrid(int [] [] grid){

for(int i = 0; i< 9; i ++){

for(int j = 0; j< 9; j ++)

System.out.print(grid [i] [j] +);

System.out.println();

}

}



/ **搜索解决方案* /

public static boolean search(int [] [] grid){

int [] [] freeCellList = getFreeCellList(grid); //免费单元格

if(freeCellList.length == 0)

返回true; //没有免费的单元格);



int k = 0; //从第一个免费单元格开始

while(true){

int i = freeCellList [k] [0];

int j = freeCellList [k] [1];

if(grid [i] [j] == 0)

grid [i] [j] = 1; //用1号填充免费单元格



if(isValid(i,j,grid)){

if(k + 1) == freeCellList.length){//没有更多的免费单元格

返回true; //找到解决方案

}

else {//移动到下一个免费单元格

k ++;

}

}

else if(grid [i] [j]< 9){

//填充免费单元格下一个可能的值

grid [i] [j] = grid [i] [j] + 1;

}

else {//自由格网格[i] [j]是9,回溯

而(grid [i] [j ] == 9){

如果(k == 0){

返回false; //没有可能的价值

}

grid [i] [j] = 0; //重置为免费单元格

k--; //回溯到前面的免费单元格

i = freeCellList [k] [0];

j = freeCellList [k] [1];

}



//用下一个可能的值填充空闲单元格,

//从这个空闲单元格继续搜索k

grid [i] [j] = grid [i] [j] + 1;

}

}

}



/ **检查网格[ i] [j]在网格中有效* /

public static boolean isValid(int i,int j,int [] [] grid){

//检查grid [i] [j]是否在i行有效

for(int column = 0; column< 9; column ++)

if(column!= j && grid [i] [column] == grid [i] [j])

返回false;



//检查grid [i] [j]在j的列是否有效

for(int row = 0; row< 9; row ++)

if(row!= i&& grid [row] [j] == grid [i] [j])

返回false;



/ /检查grid [i] [j]在3×3框中是否有效

for(int row =(i / 3)* 3; row<(i / 3)* 3 + 3;行++)

for(int col =(j / 3)* 3; col<(j / 3)* 3 + 3; col ++)

if( row!= i&& col!= j&& grid [row] [col] == grid [i] [j])

返回false;



返回true; //网格[i] [j]的当前值有效

}



/ **检查固定单元格是否有效在网格中* /

public static boolean isValid(int [] [] grid){

for(int i = 0; i< 9; i ++)

for(int j = 0; j< 9; j ++)

if(grid [i] [j]< 0 || grid [i] [j]> ; 9 ||

(grid [i] [j]!= 0&&!isValid(i,j,grid)))

返回false;



返回true; //固定单元格有效

}

}





输入一个谜题:0 6 0 1 0 4 0 5 0

0 0 8 3 0 5 6 0 0

2 0 0 0 0 0 0 0 1

8 0 0 4 0 7 0 0 6

0 0 6 0 0 0 3 0 0

7 0 0 9 0 1 0 0 4

5 0 0 0 0 0 0 0 2

0 0 7 2 0 6 9 0 0

0 4 0 5 0 8 0 7 0



找到解决方案:

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

< end output =>

i understand this funtcion/method but i am confuse why its use 2??. Given below
/////////////////////////////////////////////////////////
Store free cell positions into freeCellList
int[][] freeCellList = new int[numberOfFreeCells][2];
This is used in (LINE No 9)
///////////////////////////////////////////////////////////////


<pre lang="cs">/** Obtain a list of free cells from the puzzle */
public static int[][] getFreeCellList(int[][] grid) {
// Determine the number of free cells
int numberOfFreeCells = 0;
for (int i = 0; i &lt; 9; i++)
for (int j = 0; j &lt; 9; j++)
if (grid[i][j] == 0)
numberOfFreeCells++;

// Store free cell positions into freeCellList
int[][] freeCellList = new int[numberOfFreeCells][2];
int count = 0;
for (int i = 0; i &lt; 9; i++)
for (int j = 0; j &lt; 9; j++)
if (grid[i][j] == 0) {
freeCellList[count][0] = i;
freeCellList[count++][1] = j;
}

return freeCellList;
}

Whole code is here
public class Sudoku {
public static void main(String[] args) {
// Read a Sudoku puzzle
int[][] grid = readAPuzzle();

if (!isValid(grid))
System.out.println("Invalid input");
else if (search(grid)) {
System.out.println("The solution is found:");
printGrid(grid);
}
else
System.out.println("No solution");
}

/** Read a Sudoku puzzle from the keyboard */
public static int[][] readAPuzzle() {
// Create a Scanner
Scanner input = new Scanner(System.in);

System.out.println("Enter a Sudoku puzzle:");
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;
}

/** Obtain a list of free cells from the puzzle */
public static int[][] getFreeCellList(int[][] grid) {
// Determine the number of free cells
int numberOfFreeCells = 0;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (grid[i][j] == 0)
numberOfFreeCells++;

// Store free cell positions into freeCellList
int[][] freeCellList = new int[numberOfFreeCells][2];
int count = 0;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (grid[i][j] == 0) {
freeCellList[count][0] = i;
freeCellList[count++][1] = j;
}

return freeCellList;
}

/** Print the values in the grid */
public static void printGrid(int[][] grid) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++)
System.out.print(grid[i][j] + " ");
System.out.println();
}
}

/** Search for a solution */
public static boolean search(int[][] grid) {
int[][] freeCellList = getFreeCellList(grid); // Free cells
if (freeCellList.length == 0)
return true; // "No free cells");

int k = 0; // Start from the first free cell
while (true) {
int i = freeCellList[k][0];
int j = freeCellList[k][1];
if (grid[i][j] == 0)
grid[i][j] = 1; // Fill the free cell with number 1

if (isValid(i, j, grid)) {
if (k + 1 == freeCellList.length) { // No more free cells
return true; // A solution is found
}
else { // Move to the next free cell
k++;
}
}
else if (grid[i][j] < 9) {
// Fill the free cell with the next possible value
grid[i][j] = grid[i][j] + 1;
}
else { // free cell grid[i][j] is 9, backtrack
while (grid[i][j] == 9) {
if (k == 0) {
return false; // No possible value
}
grid[i][j] = 0; // Reset to free cell
k--; // Backtrack to the preceding free cell
i = freeCellList[k][0];
j = freeCellList[k][1];
}

// Fill the free cell with the next possible value,
// search continues from this free cell at k
grid[i][j] = grid[i][j] + 1;
}
}
}

/** Check whether grid[i][j] is valid in the grid */
public static boolean isValid(int i, int j, int[][] grid) {
// Check whether grid[i][j] is valid at the i's row
for (int column = 0; column < 9; column++)
if (column != j && grid[i][column] == grid[i][j])
return false;

// Check whether grid[i][j] is valid at the j's column
for (int row = 0; row < 9; row++)
if (row != i && grid[row][j] == grid[i][j])
return false;

// Check whether grid[i][j] is valid in the 3 by 3 box
for (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
}

/** Check whether the fixed cells are valid in the grid */
public static boolean isValid(int[][] grid) {
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (grid[i][j] < 0 || grid[i][j] > 9 ||
(grid[i][j] != 0 && !isValid(i, j, grid)))
return false;

return true; // The fixed cells are valid
}
}


Enter a puzzle: 0 6 0 1 0 4 0 5 0
0 0 8 3 0 5 6 0 0
2 0 0 0 0 0 0 0 1
8 0 0 4 0 7 0 0 6
0 0 6 0 0 0 3 0 0
7 0 0 9 0 1 0 0 4
5 0 0 0 0 0 0 0 2
0 0 7 2 0 6 9 0 0
0 4 0 5 0 8 0 7 0

The solution is found:
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
<end output="">

推荐答案

因为如果你不这样做,你没有任何地方可以返回免费单元格列表...

你的代码的第一部分会通过并找出有多少单元格是免费的:

Because if you didn't do that, you wouldn't have anywhere to return the list of free cells...
The first part of your code goes through and finds out how many cells are free:
int numberOfFreeCells = 0;
for (int i = 0; i &lt; 9; i++)
  for (int j = 0; j &lt; 9; j++)
    if (grid[i][j] == 0)
      numberOfFreeCells++;



第二位创建一个正确大小的数组来保存空闲单元格坐标,这样你就可以返回它:


The second bit creates an array the right size to hold the free cell coordinates so you can return it:

int[][] freeCellList = new int[numberOfFreeCells][2];



第三个块用数组填充数组坐标:


The third chunk fills the array with the coordinates:

int count = 0;
for (int i = 0; i &lt; 9; i++)
  for (int j = 0; j &lt; 9; j++)
    if (grid[i][j] == 0) {
      freeCellList[count][0] = i;
      freeCellList[count++][1] = j;
    }



最后你返回数组:


And finally you return the array:

return freeCellList;


这篇关于询问数独游戏中java中使用的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆