gpt4 book ai didi

java - 如何从文件中获取数据并替换数独游戏中的预定义数组

转载 作者:太空宇宙 更新时间:2023-11-04 09:43:44 24 4
gpt4 key购买 nike

在 main 方法中已经定义了一个数组,我想使用扫描器类从文件中获取该数组,并且用户将输入文件名。

这就是我想要做的,我尝试将文件的数据传递到字符串中,然后尝试将字符串传递到整数数组中

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。因此,'0' 值的 i48。您应该将 char 值转换为正确的 int。例如使用这个:

board[r][i] = st.charAt(i) - '0';

要获得 ASCII 数字的完整 View ,您可以查看此处: 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();

关于java - 如何从文件中获取数据并替换数独游戏中的预定义数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55668408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com