骑士旅行问题

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Knight extends JApplet implements ActionListener
{
	final int MAX=9;
	int chessBoard[][];
	int chessBoardPossibility[][];
	
	final int horizontal[]={ 2, 1, -1, -2, -2, -1, 1, 2 },vertical[]={ -1, -2, -2, -1, 1, 2, 2, 1 };
	int currentRow=1,currentColumn=1;
	int stepsCounter=1;
	int steps=(int)Math.pow(2, MAX);
	
	JTextArea outputArea;
	
	JButton startButton;
	
	public void init()
	{
		chessBoard=new int[MAX][MAX];
		chessBoardPossibility=new int[MAX][MAX];
		for(int row=0; row<chessBoard.length; row++)
		{
			for(int column=0; column<chessBoard[row].length; column++)
			{
				chessBoard[row][column]=0;
				chessBoardPossibility[row][column]=possibilities(row, column);
			}
		}
		chessBoard[currentRow][currentColumn]=1;
		
		Container container=getContentPane();
		container.setLayout(new FlowLayout());
		
		outputArea=new JTextArea(25, 30);
		outputArea.setEditable(false);
		outputArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
		container.add(outputArea);
		
		startButton=new JButton("开始");
		startButton.addActionListener(this);
		container.add(startButton);
		
		updateBoard();
	}
	
	public void actionPerformed(ActionEvent actionEvent)
	{
		if(actionEvent.getSource()==startButton)
		{
			for(int counter=0; counter<1; counter++)
			{
				knightStepNext();
				updateBoard();
			}
		}
		
		
	}
	
	public int possibilities(int row, int column)
	{
		int i=0;
		for(int n=0; n<8; n++)
		{
			int nextRow=row+vertical[n],nextColumn=column+horizontal[n];
			if(nextRow>=0&&nextRow<MAX&&nextColumn>=0&&nextColumn<MAX&&chessBoard[nextRow][nextColumn]==0)
			{
				i++;
			}
		}
		return i;
	}
	
	public void knightStepNext()
	{
		int possibilities[]=new int[8];
		for(int counter=0; counter<8; counter++)
		{
			possibilities[counter]=0;
		}
		
		int min=0,minn=9;
		for(int n=0; n<8; n++)
		{
			int nextRow=currentRow+vertical[n],nextColumn=currentColumn+horizontal[n];
			if(nextRow>=0&&nextRow<MAX&&nextColumn>=0&&nextColumn<MAX&&chessBoard[nextRow][nextColumn]==0)
			{
				possibilities[n]=chessBoardPossibility[nextRow][nextColumn];
				if(possibilities[n]<minn)
				{
					minn=possibilities[n];
					min=n;
				}
			}
		}
		
		if(minn<9)
		{
			int nextRow=currentRow+vertical[min],nextColumn=currentColumn+horizontal[min];
			if(nextRow>=0&&nextRow<MAX&&nextColumn>=0&&nextColumn<MAX&&chessBoard[nextRow][nextColumn]==0)
			{
				chessBoard[nextRow][nextColumn]=1;
				currentRow=nextRow;
				currentColumn=nextColumn;
				
				for(int counter=0; counter<8; counter++)
				{
					int nextRowx=currentRow+vertical[counter],nextColumnx=currentColumn+horizontal[counter];
					if(nextRowx>=0&&nextRowx<MAX&&nextColumnx>=0&&nextColumnx<MAX&&chessBoard[nextRowx][nextColumnx]==0)
					{
						chessBoardPossibility[nextRowx][nextColumnx]-=1;
					}
				}
			}
			
		}
	}
	
	public void updateBoard()
	{
		String newOutput="     ";
		for(int i=0; i<MAX; i++)
		{
			newOutput+=i + "  ";
		}
		newOutput+="\n";
		for(int row=0; row<chessBoard.length; row++)
		{
			newOutput+="  "+row;
			for(int column=0; column<chessBoard[row].length; column++)
			{                      
				if(chessBoard[row][column]==0)
				{
					newOutput+=" □";
				}
				else
				if(chessBoard[row][column]==1)
				{
					newOutput+=" ■";
				}
			}
			newOutput+="\n";
		}
		
		newOutput+="\n\n\n" + "     ";
		for(int i=0; i<MAX; i++)
		{
			newOutput+=i + "  ";
		}
		newOutput+="\n";
		
		for(int row=0; row<chessBoard.length; row++)
		{
			newOutput+="  "+row;
			for(int column=0; column<chessBoard[row].length; column++)
			{         
				newOutput+="  " + chessBoardPossibility[row][column];
			}
			newOutput+="\n";
		}
		
		outputArea.setText(newOutput);
	}
}


    原文作者:骑士周游问题
    原文地址: https://blog.csdn.net/zhuxiangfeicool/article/details/6871759
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞