gpt4 book ai didi

java - 开始 Java SudokuSolver FileNotFoundException

转载 作者:行者123 更新时间:2023-11-30 08:16:37 25 4
gpt4 key购买 nike

当我运行我的程序时,我一直遇到这个问题:

Exception in thread "main" java.io.FileNotFoundException: samplesukodu8.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:611)
at prac7.readSudoku(prac7.java:31)
at prac7.main(prac7.java:16)

这是我的程序:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class prac7 {

public static void main(String[] args) throws FileNotFoundException {
// Begin keep when submit
// During development, you may comment out the next few lines
// and hardcode sudokuFile to be a particular input
if (args.length != 1) { usage(); System.exit(1); }

String sudokuFile = args[0]; //"samplesudoku8.txt";
System.out.println("Solving " + sudokuFile);

int[][] sudoku = readSudoku(sudokuFile);
simplify(sudoku);
int[][] answer = search(sudoku);
if (answer==null)
System.out.println("No answer found.");
else
printSudoku( answer );
}


// reads a sudoku board from a text file.
public static int[][]
readSudoku(String sudokuFile) throws FileNotFoundException
{
File f = new File(sudokuFile);
Scanner s = new Scanner(f);
int[][] sudoku = new int[9][9];

// Read in sudoku
for (int row = 0; row < 9; row++)
for (int column = 0; column < 9; column++)
if (s.hasNextInt())
sudoku[row][column] = s.nextInt();
s.close();
return sudoku;
}

// Simplification procedure used in assignment 7.
public static void simplify(int[][] sudoku) {
boolean hasMadeProgress;

do {

hasMadeProgress = false;

// enumerate row
for (int row = 0; row < 9; row++) {
for (int possibility = 1; possibility <= 9; possibility++) {
int count = 0;
int index = -1;

for (int col = 0; col < 9; col++) {
if (isPossible(sudoku, row, col, possibility)) {
count++;
index = col;
}
}

if (count == 1) {
sudoku[row][index] = possibility;
hasMadeProgress = true;
}
}
} // end of enumerate row
// enumerate col
for (int col = 0; col < 9; col++) {
for (int possibility = 1; possibility <= 9; possibility++) {

int count = 0;
int index = -1;

for (int row = 0; row < 9; row++) {
if (isPossible(sudoku, row, col, possibility)) {
count++;
index = row;
}
}

if (count == 1) {
sudoku[index][col] = possibility;
hasMadeProgress = true;
}
}
} // end of enumerate col

// end of enumerate 3x3
for (int row = 0; row < 9; row += 3) {
for (int col = 0; col < 9; col += 3) {
for (int possibility = 1; possibility <= 9; possibility++) {
int count = 0;
int index1 = -1, index2 = -1;

for (int r = row; r < row + 3; r++) {
for (int c = col; c < col + 3; c++) {
if (isPossible(sudoku, r, c, possibility)) {
count++;
index1 = r;
index2 = c;
}
}
}

if (count == 1) {
sudoku[index1][index2] = possibility;
hasMadeProgress = true;
}
} // end of possibility
} // end of col
} // end of row
} while (hasMadeProgress);

}

// This method returns true if it is possible for numToCheck to be instide
// vals[row][column]
public static boolean isPossible(int[][] sudoku, int row, int column,
int numToCheck)
{
// may not be necesary
if (sudoku[row][column] != 0)
{
return false;
}

// check same row
for (int j = 0; j < 9; j++)
{
if (sudoku[row][j] == numToCheck)
{
return false;
}
}

// check same column
for (int i = 0; i < 9; i++)
{
if (sudoku[i][column] == numToCheck)
{
return false;
}
}

// check same 3x3
int cornerRow = row / 3 * 3;
int cornerCol = column / 3 * 3;
for (int i = cornerRow; i < cornerRow + 3; i++)
{
for (int j = cornerCol; j < cornerCol + 3; j++)
{
if (sudoku[i][j] == numToCheck)
{
return false;
}
}
}

return true;

}


/* Recursively searches for a correctly filled in version of the sudoku
board provided as input. Returns null if there is no way of filling
in the given sudoku board.
Implementation note: Note that the array for the sudoku board
is copied when a recursive call is made. This is done because the
recursive call may make further changes to the board when it calls
simplify(), and it is too hard to predict where those changes will be
made. Instead, we just make a copy which we discard if it turns out to be
a dead end.
*/
public static int[][] search(int[][] sudoku) {

if (!isSafe(sudoku)) return null;

// look for a location to expand on.
int expand_row = -1;
int expand_col = -1;
for (int i=0; i<9; i++)
for (int j=0; j<9; j++) {
if (sudoku[i][j] == 0) {
expand_row = i;
expand_col = j;
}
}

// if all filled in, then we've found an answer.
if (expand_row==-1) return sudoku;

// a cell needs to be filled in, try to fill it in all possible ways
for (int fill=1; fill<=9; fill++) {
int[][] new_sudoku = copyBoard(sudoku); // copy the board.
new_sudoku[expand_row][expand_col] = fill; // fill the unknown cell
simplify(new_sudoku); // simplify
int[][] answer = search(new_sudoku); // recursively try to solve.
if (answer!=null) return answer; // found answer, return.
}

// no answer found from this board; return null;
return null;
}


// Creates a new array that is a copy of the given sudoku board.
public static int[][] copyBoard(int[][] sudoku) {
int[][] copy = new int[9][9];
for (int i=0; i<9; i++)
for (int j=0; j<9; j++)
copy[i][j] = sudoku[i][j];
return copy;
}


// Checks whether the give board is still safe, that is it does not
// have any duplicate entries in any row, column, or box.
public static boolean isSafe(int[][] sudoku) {
for (int fill=1; fill<9; fill++) {

// check rows for duplicate fill values
for (int i=0; i<9; i++) {
int count = 0;
for (int j=0; j<9; j++)
if (sudoku[i][j]==fill) count++;
if (count > 1) return false;
}

// check columns for duplicate fill values
for (int j=0; j<9; j++) {
int count = 0;
for (int i=0; i<9; i++)
if (sudoku[i][j]==fill) count++;
if (count > 1) return false;
}

// check boxes for duplicate fill values
for (int box=0; box<9; box++) {
int count = 0;
int startRow = 3 * (box / 3);
int startCol = 3 * (box % 3);
for (int i = startRow; i < startRow + 3; i++)
for (int j = startCol; j < startCol + 3; j++)
if (sudoku[i][j]==fill) count++;
if (count > 1) return false;
}
}
return true;
}


public static void printSudoku(int[][] sudoku) {
for (int row = 0; row < 9; row++) {
for (int column = 0; column < 9; column++) {
System.out.print(sudoku[row][column] + " ");
}
System.out.println();
}
}

public static void usage() {
System.out.println("java SodokuSolver <sudoku_input>");
}
}

最佳答案

拼写错误:samplesukodu 与 Samplesudoku。不客气。

关于java - 开始 Java SudokuSolver FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29572736/

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