You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
1.7 KiB
83 lines
1.7 KiB
package g08.problem.npuzzle;
|
|
|
|
import core.problem.Action;
|
|
import core.problem.Problem;
|
|
import core.problem.State;
|
|
import core.solver.Node;
|
|
|
|
import java.util.Deque;
|
|
|
|
/**
|
|
* ClassName : Npuzzle //类名
|
|
* Description : //描述
|
|
* Author : WLS //作者
|
|
* Date: 2021-04-15 11:01 //时间
|
|
*/
|
|
public class Npuzzle extends Problem {
|
|
|
|
public Npuzzle(State initialState, State goal)
|
|
{
|
|
super(initialState, goal);
|
|
}
|
|
|
|
public Npuzzle(State initialState, State goal, int size)
|
|
{
|
|
super(initialState, goal, size);
|
|
}
|
|
|
|
|
|
@Override
|
|
public boolean solvable() {
|
|
int cnt = 0;
|
|
char[] init = initialState.toString().toCharArray();
|
|
char[] go = goal.toString().toCharArray();
|
|
for(int i = 0;i<size*size;i++)
|
|
{
|
|
for(int j=0;j<size*size;j++)
|
|
{
|
|
if(init[i]=='0'||init[j]=='0')
|
|
continue;
|
|
if(init[i]>init[j])
|
|
{
|
|
cnt++;
|
|
}
|
|
}
|
|
}
|
|
if(cnt%2==1)
|
|
{
|
|
System.out.println("unsolvable");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public int stepCost(State state, Action action) {
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
protected boolean applicable(State state, Action action) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public void showSolution(Deque<Node> path) {
|
|
System.out.println("showSolution");
|
|
}
|
|
|
|
@Override
|
|
public void draw() {
|
|
initialState.draw();
|
|
System.out.println();
|
|
goal.draw();
|
|
System.out.println("=======================");
|
|
}
|
|
|
|
@Override
|
|
public void printPath(Deque<Node> path) {
|
|
System.out.println("printPath");
|
|
}
|
|
}
|