package homeWork;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JApplet;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class KnightTravel extends JApplet {//骑士旅行 7.22
private static final long serialVersionUID = 1L;
int horizontal[] = {2,1,-1,-2,-2,-1,1,2};//表示移动的方向和步数 向上和向左为负
int vertical[] = {-1,-2,-2,-1,1,2,2,1};
int moveNumber;//移动的方式 0--7
int board[][] = new int[8][8];//棋盘
int Xboard[][] = {{2,3,4,4,4,4,3,2},{3,4,6,6,6,6,4,3},{4,6,8,8,8,8,6,4},{4,6,8,8,8,8,6,4},{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},{3,4,6,6,6,6,4,3},{2,3,4,4,4,4,3,2}};//记录方格访问难易程度,数值越小越难访问
int moveRecord;//记录当前的选择
int footSteps;//记录骑士移动的步数
int currentRow = 0;
int currentCol = 0;
int s = 1;//记录成功次数
int Record = 0;//记录骑士当前移动的最远步数
int row,col;//记录当前应移动的行和列
int XchangeRow,XchangeCol;//记录访问数减少的行和列
boolean R;//记录移动是否成功
boolean moveJudge;//判断是否可以移动
JTextArea outpuTextArea;
String outputString = "";
JScrollPane scrollPane;
public void init(){
Container container = getContentPane();
container.setLayout(new FlowLayout());
outpuTextArea = new JTextArea(17,20);
scrollPane = new JScrollPane(outpuTextArea);
container.add(scrollPane);
for(currentRow = 0;currentRow < 8;currentRow ++)
for(currentCol = 0;currentCol < 8;currentCol ++){
R = move(board,currentRow,currentCol);//移动骑士
Print(board,R);//打印结果
}
}
// 骑士移动过程
public boolean move(int array[][],int currentRow,int currentCol){
// int currentRow = 0;//当前行
// int currentCol = 0;//当前列
// 初始化操作
for(int row = 0;row < 8;row++)
for(int col = 0;col < 8;col ++)
board[row][col] = 0;
int Xboard[][] = {{2,3,4,4,4,4,3,2},{3,4,6,6,6,6,4,3},{4,6,8,8,8,8,6,4},{4,6,8,8,8,8,6,4},{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},{3,4,6,6,6,6,4,3},{2,3,4,4,4,4,3,2}};
Record = 0;
footSteps = 1;
int tempCR,tempCC;//保存当前行当前列
board[currentRow][currentCol] = footSteps++;
while(footSteps<=64){
moveNumber = 0;
moveRecord = 9;
moveJudge = false;
while(moveNumber < 8){//找出当前最小访问数
// if(moveNumber > 7)
// return false;
tempCR = currentRow;
tempCC = currentCol;
currentRow += vertical[moveNumber];
currentCol += horizontal[moveNumber];
if(currentRow <8 && currentRow >= 0 && currentCol >= 0 && currentCol < 8 //是否越过棋盘
&& board[currentRow][currentCol] == 0){//此位置是否已经经过
moveJudge = true;
if(Xboard[currentRow][currentCol] < moveRecord){
moveRecord = Xboard[currentRow][currentCol];
row = currentRow;
col = currentCol;
}
}
// board[row][col] = footSteps;
// Record++;
// break;
currentRow = tempCR;//返回原来位置
currentCol = tempCC;
moveNumber ++;
}
if(moveJudge){
currentRow = row;//保存当前位置
currentCol = col;
board[row][col] = footSteps;
Record++;
footSteps++;
moveNumber = 0;
XchangeRow = row;
XchangeCol = col;
while(moveNumber < 8){
tempCR = XchangeRow;
tempCC = XchangeCol;
XchangeRow += vertical[moveNumber];
XchangeCol += horizontal[moveNumber];
if(XchangeRow <8 && XchangeRow >= 0 && XchangeCol >= 0 && XchangeCol < 8//是否越过棋盘
&& board[XchangeRow][XchangeCol] == 0)//此位置是否已经经过
Xboard[XchangeRow][XchangeCol]--;
XchangeRow = tempCR;
XchangeCol = tempCC;
moveNumber ++;
}
}
else
return false;
}
return true;
}
// 打印移动结果
public void Print(int Parray[][],boolean result){
if(result){
for(int row = 0;row < 8;row++){
for(int col = 0;col < 8;col ++)
outputString += board[row][col]+" ";
outputString += "/n";
}
outputString += "success! "+ s + "/n";
s++;
}
else {
for(int row = 0;row < 8;row++){
for(int col = 0;col < 8;col ++)
outputString += board[row][col]+" ";
outputString += "/n";
}
outputString +=" Move False!"+" "+Record +"/n";
}
outpuTextArea.setText(outputString);
}
}