Внутри основного метода массив уже определен, я хочу извлечь этот массив из файла, используя класс сканера, и пользователь введет имя файла.
Это то, что я пытаюсь сделать, я пытаюсь передать данные файла в строку, а затем я пытаюсь передать строку в массив целых чисел
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
Ответ показывает результат