在main方法中,已经定义了一个数组,我想通过使用scanner类来从文件中获取该数组,用户将输入该文件的名称。
这是我想要做的我试图将文件的数据传递到一个字符串,然后我试图将字符串传递给整数数组
package theSuDoKuSolver;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class BackTracking
{
public static boolean isSafe(int[][] board, int row, int col, int num)
{
for (int d = 0; d < board.length; d++)
{
// if the number we are trying to
// place is already present in
// that row, return false;
if (board[row][d] == num)
{
return false;
}
}
// column has the unique numbers (column-clash)
for (int r = 0; r < board.length; r++)
{
// if the number we are trying to
// place is already present in
// that column, return false;
if (board[r][col] == num)
{
return false;
}
}
// corresponding square has
// unique number (box-clash)
int sqrt = (int) Math.sqrt(board.length);
int boxRowStart = row - row % sqrt;
int boxColStart = col - col % sqrt;
for (int r = boxRowStart;
r < boxRowStart + sqrt; r++)
{
for (int d = boxColStart;
d < boxColStart + sqrt; d++)
{
if (board[r][d] == num)
{
return false;
}
}
}
// if there is no clash, it's safe
return true;
}
public static boolean solveSudoku(int[][] board, int n)
{
int row = -1;
int col = -1;
boolean isEmpty = true;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (board[i][j] == 0)
{
row = i;
col = j;
// we still have some remaining
// missing values in Sudoku
isEmpty = false;
break;
}
}
if (!isEmpty)
{
break;
}
}
// no empty space left
if (isEmpty)
{
return true;
}
// else for each-row backtrack
for (int num = 1; num <= n; num++)
{
if (isSafe(board, row, col, num))
{
board[row][col] = num;
if (solveSudoku(board, n))
{
// print(board, n);
return true;
}
else
{
board[row][col] = 0; // replace it
}
}
}
return false;
}
public static void print(int[][] board, int N)
{
// we got the answer, just print it
for (int r = 0; r < N; r++)
{
for (int d = 0; d < N; d++)
{
System.out.print(board[r][d]);
System.out.print(" ");
}
System.out.print("\n");
if ((r + 1) % (int) Math.sqrt(N) == 0)
{
System.out.print("");
}
}
}
// Driver Code
@SuppressWarnings("resource")
public static void main(String args[])
{
File file = new File("Evil532");
String content = null;
try {
content = new Scanner(file).useDelimiter("\\Z").next();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int[][] board = new int[9][9];
Scanner sc = new Scanner(content);
int i=0; int r=0;
while(sc.hasNext()) {
String st = sc.next();
for(i=0;i<st.length();i++)
board[r][i]=st.charAt(i);
r++;
}
// System.out.println(content);
// System.out.println(board);
// int[][] board = new int[][]
// {
// {3, 0, 6, 5, 0, 8, 4, 0, 0},
// {5, 2, 0, 0, 0, 0, 0, 0, 0},
// {0, 8, 7, 0, 0, 0, 0, 3, 1},
// {0, 0, 3, 0, 1, 0, 0, 8, 0},
// {9, 0, 0, 8, 6, 3, 0, 0, 5},
// {0, 5, 0, 0, 9, 0, 6, 0, 0},
// {1, 3, 0, 0, 0, 0, 2, 5, 0},
// {0, 0, 0, 0, 0, 0, 0, 7, 4},
// {0, 0, 5, 2, 0, 6, 3, 0, 0}
// };
int N = board.length;
if (solveSudoku(board, N))
{
print(board, N); // print solution
}
else
{
System.out.println("No solution");
}
}
}
// The file has some data like
005007900
700050148
000000000
009304000
140000300
000010002
003070020
006023500
020600007
这是我想要做的,但答案是错误的
预期结果
3 1 6 5 7 8 4 9 2
5 2 9 1 3 4 7 6 8
4 8 7 6 2 9 5 3 1
2 6 3 4 1 5 9 8 7
9 7 4 8 6 3 1 2 5
8 5 1 7 9 2 6 4 3
1 3 8 9 4 7 2 5 6
6 9 2 3 5 1 8 7 4
7 4 5 2 8 6 3 1 9
这是预期的结果,表明阵列是通过适当的解决方案解决的。
实际结果
48 48 53 48 48 55 57 48 48
55 48 48 48 53 48 49 52 56
48 48 48 48 48 48 48 48 48
48 48 57 51 48 52 48 48 48
49 52 48 48 48 48 51 48 48
48 48 48 48 49 48 48 48 50
48 48 51 48 55 48 48 50 48
48 48 54 48 50 51 53 48 48
48 50 48 54 48 48 48 48 55
答案显示错误的结果
问题出在这里:board[r][i] = st.charAt(i);
您正在将char
值分配给int
。所以i
是48
的'0'
值。您应该将char
值转换为正确的int
。例如使用这个:
board[r][i] = st.charAt(i) - '0';
要获得ascii数字的完整视图,您可以在这里查看:https://www.ascii-code.com/
或者你可以使用这个:
while (sc.hasNext()) {
String[] numbers = sc.next().split("");
for (int i = 0; i < numbers.length; i++)
board[r][i] = Integer.parseInt(numbers[i]);
r++;
}
除了你的代码读取文件不是很清楚,我建议使用这样的东西:
int[][] board = new int[9][9];
BufferedReader br = new BufferedReader(new FileReader(new File("/path/to/file")));
String line;
int lineNumber = 0;
while ((line = br.readLine()) != null) {
String[] numbers = line.split("");
for (int columnNumber = 0; columnNumber < numbers.length; columnNumber++)
board[lineNumber][columnNumber] = Integer.parseInt(numbers[columnNumber]);
lineNumber++;
}
br.close();