C# 骑士巡游寻径算法

C# 骑士巡游寻径算法,c#,algorithm,path-finding,C#,Algorithm,Path Finding,我必须从[0,0]开始,参观所有广场,然后返回/坐在[0,0] 来自我的董事会的图像 这是我尝试的代码: public class chess_F { // save my current position struct position { public int row; public int col; }; const int NOT_VISITED = 0; // my board private

我必须从[0,0]开始,参观所有广场,然后返回/坐在[0,0]

来自我的董事会的图像

这是我尝试的代码:

public class chess_F
{
    // save my current position
    struct position
    {
        public int row;
        public int col;
    };

    const int NOT_VISITED = 0;

    // my board
    private int[,] cols = new int[8, 8];


    // main function 
    public void Do()
    {
        position start;
        start.row = 0;
        start.col = 0;

        initializeBoard(ref cols);

        move(start, cols);
    }

    private void initializeBoard(ref int[,] board)
    {
        for (int i = 0; i < 8; i++)
            for (int j = 0; j < 8; j++)
                board[i, j] = NOT_VISITED;
    }

    private bool visited(int[,] board, position square)
    {
        return board[square.row, square.col] != NOT_VISITED;
    }


    /*  |---|---|-0-|---|-1-|---|---|---|
     *  |---|-7-|---|---|---|-2-|---|---|
     *  |---|---|---|-X-|---|---|---|---|
     *  |---|-6-|---|---|---|-3-|---|---|
     *  |---|---|-5-|---|-4-|---|---|---|
     *  |---|---|---|---|---|---|---|---|
     *  |---|---|---|---|---|---|---|---|
     *  |---|---|---|---|---|---|---|---|
     */

    // find all cols for my knight - can sit on
    private position[] findPath(position present, int[,] cols)
    {
        position[] temp = new position[8];
        position mytemp;

        for (int i = 0; i < 8; i++)
        {
            try
            {
                //path 0
                switch (i)
                {
                    case 0:
                        {
                            if (cols[present.row - 1, present.col - 2] == 0)
                            {
                                mytemp.row = present.row - 1;
                                mytemp.col = present.col - 2;
                                temp[i] = mytemp;
                            }
                            break;
                        }

                    //path 1
                    case 1:
                        {
                            if (cols[present.row + 1, present.col - 2] == 0)
                            {
                                mytemp.row = present.row + 1;
                                mytemp.col = present.col - 2;
                                temp[i] = mytemp;
                            } break;
                        }

                    //path 2
                    case 2:
                        {
                            if (cols[present.row + 2, present.col - 1] == 0)
                            {
                                mytemp.row = present.row + 2;
                                mytemp.col = present.col - 1;
                                temp[i] = mytemp;
                            } break;
                        }
                    //path 3
                    case 3:
                        {
                            if (cols[present.row + 2, present.col + 1] == 0)
                            {
                                mytemp.row = present.row + 2;
                                mytemp.col = present.col + 1;
                                temp[i] = mytemp;
                            } break;
                        }

                    //path 4
                    case 4:
                        {
                            if (cols[present.row + 1, present.col + 2] == 0)
                            {
                                mytemp.row = present.row + 1;
                                mytemp.col = present.col + 2;
                                temp[i] = mytemp;
                            } break;
                        }

                    //path 5
                    case 5:
                        {
                            if (cols[present.row - 1, present.col + 2] == 0)
                            {
                                mytemp.row = present.row - 1;
                                mytemp.col = present.col + 2;
                                temp[i] = mytemp;
                            } break;
                        }

                    //path 6
                    case 6:
                        {
                            if (cols[present.row - 2, present.col - 1] == 0)
                            {
                                mytemp.row = present.row - 2;
                                mytemp.col = present.col - 1;
                                temp[i] = mytemp;
                            } break;
                        }

                    //path 7
                    case 7:
                        {
                            if (cols[present.row - 2, present.col - 1] == 0)
                            {
                                mytemp.row = present.row - 2;
                                mytemp.col = present.col - 1;
                                temp[i] = mytemp;
                            } break;
                        }
                }
            }
            catch
            {
                mytemp.row = -1;
                mytemp.col = -1;
                temp[i] = mytemp;
            }
        }
        return temp;
    }

    // check all cols and row to check ...
    private bool allVisited(int[,] cols)
    {
        for (int i = 0; i < 8; i++)
            for (int j = 0; j < 8; j++)
                if (cols[i, j] != 1)
                    return false;
        return true;

    }


    // save true path
    int[,] truepath;
    private void move(position present, int[,] cols)
    {
        int[,] tempCols = cols;
        tempCols[present.row, present.col] = 1;
        position[] avaliable = findPath(present, tempCols);

        if (avaliable.Count() < 1)
            return;

        for (int i = 0; i < avaliable.Count(); i++)
        {
            if (allVisited(tempCols))
            {
                truepath = tempCols;
            }
            else
            {
                if (avaliable[i].row != -1 && avaliable[i].row != 0)
                    move(avaliable[i], tempCols);
            }
        }
    }
}
公共类国际象棋
{
//保存我的当前位置
结构位置
{
公共int row;
公共int col;
};
const int NOT_visted=0;
//我的董事会
私有整数[,]cols=新整数[8,8];
//主要功能
公营部门
{
位置启动;
start.row=0;
start.col=0;
初始化板(参考cols);
移动(开始,cols);
}
专用无效初始化板(参考int[,]板)
{
对于(int i=0;i<8;i++)
对于(int j=0;j<8;j++)
董事会[i,j]=未到访;
}
参观私人住宅(int[,]板,位置广场)
{
返回板[square.row,square.col]!=未访问;
}
/*  |---|---|-0-|---|-1-|---|---|---|
*  |---|-7-|---|---|---|-2-|---|---|
*|--|--|--|--X-|---|---|---|---|
*  |---|-6-|---|---|---|-3-|---|---|
*  |---|---|-5-|---|-4-|---|---|---|
*  |---|---|---|---|---|---|---|---|
*  |---|---|---|---|---|---|---|---|
*  |---|---|---|---|---|---|---|---|
*/
//为我的骑士找到所有的颜色-可以坐在上面
私有位置[]查找路径(位置存在,整数[,]列)
{
位置[]temp=新位置[8];
位置mytemp;
对于(int i=0;i<8;i++)
{
尝试
{
//路径0
开关(一)
{
案例0:
{
if(cols[present.row-1,present.col-2]==0)
{
mytemp.row=present.row-1;
mytemp.col=present.col-2;
温度[i]=我的温度;
}
打破
}
//路径1
案例1:
{
if(cols[present.row+1,present.col-2]==0)
{
mytemp.row=当前.row+1;
mytemp.col=present.col-2;
温度[i]=我的温度;
}中断;
}
//路径2
案例2:
{
if(cols[present.row+2,present.col-1]==0)
{
mytemp.row=present.row+2;
mytemp.col=present.col-1;
温度[i]=我的温度;
}中断;
}
//路径3
案例3:
{
if(cols[当前.row+2,present.col+1]==0)
{
mytemp.row=present.row+2;
mytemp.col=present.col+1;
温度[i]=我的温度;
}中断;
}
//路径4
案例4:
{
如果(列[present.row+1,present.col+2]==0)
{
mytemp.row=present.row+1;
mytemp.col=present.col+2;
温度[i]=我的温度;
}中断;
}
//路径5
案例5:
{
if(cols[present.row-1,present.col+2]==0)
{
mytemp.row=present.row-1;
mytemp.col=present.col+2;
温度[i]=我的温度;
}中断;
}
//路径6
案例6:
{
if(cols[present.row-2,present.col-1]==0)
{
mytemp.row=present.row-2;
mytemp.col=present.col-1;
温度[i]=我的温度;
}中断;
}
//路径7
案例7:
{
if(cols[present.row-2,present.col-1]==0)
{
mytemp.row=present.row-2;
mytemp.col=present.col-1;
温度[i]=我的温度;
}中断;
}
}
}
抓住
{
mytemp.row=-1;
mytemp.col=-1;
温度[i]=我的温度;
}
}
返回温度;
}
//检查所有列和行以检查。。。
私人布尔访问(整数[,]列)
{
对于(int i=0;i<8;i++)
对于(int j=0;j<8;j++)
if(cols[i,j]!=1)
返回false;
返回true;
}
//保存真实路径
int[,]truepath;
私有无效移动(位置存在,整数[,]列)
{
int[,]tempCols=cols;
tempCols[present.row,present.col]=1;
位置[]可用=查找路径(存在,临时);
if(available.Count()<1)
返回;
for(int i=0;i public static PointF PointOnCircle(float radius, float angleInDegrees, PointF origin)
{
    // Convert from degrees to radians via multiplication by PI/180        
    float x = (float)(radius * Math.Cos(angleInDegrees * Math.PI / 180F)) + origin.X;
    float y = (float)(radius * Math.Sin(angleInDegrees * Math.PI / 180F)) + origin.Y;

    return new PointF(x, y);
}  
import java.awt.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class KnightTour extends JComponent
{
    private int maxRows;    // number of rows (and cols!) on board
    private int move=0;     // how many successful moves we've made
    private int board[][];  // contains move number
    private static final int CELLSIZE = 30;  // graphical size of each cell of board

    public KnightTour(int maxRows)
    {
        JFrame frame = new JFrame("Knight Tour");
        this.maxRows = maxRows;
        board = new int[maxRows][maxRows];

        // clear the board
        for  (int i=0; i<maxRows; ++i)
         for (int j=0; j<maxRows; ++j)
           board[i][j] = 0;

        setPreferredSize(new Dimension(maxRows*CELLSIZE, maxRows*CELLSIZE));
        frame.getContentPane().add(this);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private void showBoard()
    {
        repaint();
        try {
            Thread.sleep(0);
        } catch (InterruptedException e)
        { /* ignore, shouldn't happen */ }
    }

    @Override
    public void paintComponent(Graphics g)
    {
        for (int i=0; i<=maxRows; ++i) { // draw grid
             g.drawLine(0,i*CELLSIZE, CELLSIZE*maxRows,i*CELLSIZE);
             for (int j=0; j<=maxRows; ++j)
                g.drawLine(j*CELLSIZE, 0, j*CELLSIZE, CELLSIZE*maxRows);
        }
        for (int i=0; i<maxRows; ++i) // draw moves
            for (int j=0; j<maxRows; ++j)
                if (board[i][j] != 0)
                    g.drawString(""+board[i][j], CELLSIZE/4+i*CELLSIZE, CELLSIZE*3/4+j*CELLSIZE);
    }

    public boolean solve(int row, int column)
    {
        if (row < 0 || column < 0 || row >= maxRows || column >= maxRows || board[row][column]!=0)
            return false; // out of range or already been to this cell
        board[row][column] = ++move;
        showBoard();

        if (move == maxRows*maxRows)
            return true; // filled the board!

        // try all possible moves from here
        if (   solve(row-2, column-1)
            || solve(row-2, column+1)
            || solve(row+2, column-1)
            || solve(row+2, column+1)
            || solve(row-1, column-2)
            || solve(row+1, column-2)
            || solve(row-1, column+2)
            || solve(row+1, column+2)) {
           repaint(); // in case not showing each move separately
           return true; // success!
        }

        board[row][column] = 0; // failed - remove current move
        --move;
        showBoard();
        return false;
    }

    public static void main(String args[])
    {
        KnightTour board = new KnightTour(6);
        if (board.solve(0,0))  // try solution starting at upper left
            System.out.println("Solution found.");
        else
            System.out.println("No solution found.");
    }
}
public boolean solve(int row, int column)
    {
        if (row < 0 || column < 0 || row >= maxRows || column >= maxRows || board[row][column]!=0)
            return false; // out of range or already been to this cell
        board[row][column] = ++move;
        showBoard();

        if (move == maxRows*maxRows)
            return true; // filled the board!

        // try all possible moves from here
        if (   solve(row-2, column-1)
            || solve(row-2, column+1)
            || solve(row+2, column-1)
            || solve(row+2, column+1)
            || solve(row-1, column-2)
            || solve(row+1, column-2)
            || solve(row-1, column+2)
            || solve(row+1, column+2)) {
           repaint(); // in case not showing each move separately
           return true; // success!
        }

        board[row][column] = 0; // failed - remove current move
        --move;
        showBoard();
        return false;
    }
if (avaliable[i].row != -1 && avaliable[i].row != 0)
                    move(avaliable[i], tempCols);
if (avaliable[i].row != -1 && avaliable[i].col!= -1)
                    move(avaliable[i], tempCols);