我必须编写一个程序,将 8 个谜题作为数组,检查它是否可解(并捕获任何输入错误),然后使用 A* 搜索来解决谜题(如果可解),显示解决第 8 个谜题的步骤顺序(基本上是如何重新组织数组的,这一切都必须在一个 .java 文件中(我见过的大多数文件都在多个源文件中,我更希望这样做,但事实并非如此)目前正在接受的内容。我已经完成了大部分工作,除了使用文件作为输入来创建拼图数组(我已将其设置为接受键盘输入来创建它)、A* 搜索算法本身,以及我所做的部分几乎都是验证方法(检查以确保拼图是真正的 8 拼图,使用 if 语句和 try-catch block 的组合),检查可解性方法,拼图创建,我认为这是最重要的。
我遇到问题的部分是算法本身。我理解它,但不知道如何在一个文件中实现它,因为我不知道如何使用 State 对象创建 Node 对象(我认为 State 应该是一个对象,但我不确定)在与另一个类对象相同的文件(该类对象是拼图)。我已经做了很多工作,但是整个事情的方法(我知道如何将其实现为单独的 .java 文件,但不是这样,部分原因是 Java 不是我最强的数据结构语言(我的知识java 数据结构创建不如使用 C++)。无论如何,这是我到目前为止所掌握的内容,以便您可以了解我正在工作的上下文:
import java.io.File;
import java.io.IOException;
import java.util.Queue;
import java.util.Scanner;
public class Puzzle {
public static void main(String[] args) throws IOException
{
int collumnInput = 0;
int rowInput = 0;
if (args.length == 0)
{
System.out.println("Give 8-puzzle as 3x3 matrix (a space between digits, hit Enter after each line: " );
Scanner scan = new Scanner (System.in);
int[][] puzzleArray = new int[3][3];
for (collumnInput=0; collumnInput < 3; collumnInput++)
{
for (rowInput = 0; rowInput < 3; rowInput++)
{
try
{
puzzleArray[collumnInput][rowInput] = scan.nextInt();
if ((puzzleArray[collumnInput][rowInput] > 8) || (puzzleArray[collumnInput][rowInput] < 0))
{
System.out.println("Invalid 8-puzzle entered!");
System.exit(0);
}
}
catch (java.util.InputMismatchException exception)
{
System.out.println("Invalid 8-puzzle entered!");
System.exit(0);
}
}
}
scan.close();
for(int column = 0; column < 3; column++)
{
for(int row = 0; row < 3; row++)
{
System.out.print(puzzleArray[column][row] + " "); //Outputs the array in a 3x3 grid.
}
System.out.println();
}
boolean []check = new boolean[9];
for (int collumnCheck = 0; collumnCheck < collumnInput; collumnCheck++){
for (int rowCheck = 0; rowCheck < rowInput; rowCheck++)
{
if (check[puzzleArray[collumnCheck][rowCheck]]){
System.out.println("Invalid 8-puzzle entered!");
System.exit(0);
}else
{
check[ puzzleArray[collumnCheck][rowCheck]] = true;
}
}
}
int[] oneDArray = convertToOneD(puzzleArray);
boolean possible=isSolvable(oneDArray);
if (possible == true)
System.out.println("Is solvable");
else
System.out.println("Not solvable");
}
/*else if (args.length == 1)
{
}
else if (args.length == 2)
{
}
else
{
System.out.println("Invalid number of arguments, halting execution.");
}
*/
}
public static boolean isSolvable(int [] p)
{
int i, j, n, inversions = 0;
n = p.length;
for(i = 0; i < n - 1; i++)
for(j = i+1; j < n; j++)
if(p[i] > p[j]) {
//System.out.println("("+p[i]+", "+p[j]+")");
inversions++;
}
//System.out.println("Number of inversions are: "+inversions);
return !((inversions > 0) && (inversions % 2 == 0));
}
public static int[] convertToOneD(int[][] theArray)
{
int[] singleD = new int[9];
int k=0;
for (int i=0; i < 3; i++)
{
for (int j=0; j < 3; j++)
{
singleD[k] = theArray[i][j];
k++;
}
}
for (k = 0; k < 9; k++)
{
System.out.print(singleD[k]+" ");
}
return singleD;
}
/*public static void AStarSearch(int[]puzzleArray)
{
}*/
}
最佳答案
将其他类作为内部静态类放入 Puzzle
类中,如下所示:
public class Puzzle {
public static class Node {
public Node() {
//constructor
}
//other methods of Node
}
public static class State {
public State() {
//constructor
}
//other methods of State
}
public static void main(String[] args) {
Node n = new Node();
State s = new State();
//whatever
}
}
然后您就可以像在其他文件中定义它们一样使用它们。唯一的区别是,您不能从 Puzzle
类外部使用它们,除非它们具有足够的可见性(在本例中很好,因为我已将它们声明为 public
),除非您将它们称为 Puzzle.Node
或导入它们。
如果它们是私有(private)
,那么您可以在Puzzle
中使用它们,但不能在其他地方使用它们。
关于java - 如何创建一个程序,通过 A* 搜索来解决所有在一个 Java 源文件中的 8 个难题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26194945/