algorithm - N皇后问题算法方法的比较

标签 algorithm backtracking

我想评估解决N皇后问题的各种方法的性能比较与精确方法(如回溯)相比,主要是基于人工智能的元启发式算法,如模拟退火、禁忌搜索和遗传算法等。有学习的代码吗许多现实世界中的优化问题都考虑精确方法和元启发式方法之间的合作方案。

最佳答案

几年前,我在大学的一节课上不得不用java编写一个n皇后解算器。我不知道你在寻找哪种类型的源代码,但如果有什么帮助的话,这里是这个程序的代码它在任何方面都没有真正优化,只是一个非常简单的递归算法。我在大学的第一个学期写了这篇文章,所以请原谅初学者的错误:—)我把大部分的评论都删掉了,但希望你能理解我留下的那些评论。如果你想要一些专业的源代码比谷歌搜索我知道维基百科有一些像样的文章和伪代码希望这有帮助!

import java.util.Scanner;
public class QueensSolver 
{
public static void main(String args[])
{
    System.out.println("Please enter the size of the chessboard: ");
    Scanner stdin = new Scanner(System.in);
    int sizeOfBoard = stdin.nextInt();
    //Create the board to the given size.
    char[][] board = new char[sizeOfBoard][sizeOfBoard];

    //fill board with hyphens
    for(int row=0; row<sizeOfBoard; row++)
    {
        for(int col=0; col<sizeOfBoard; col++)
        {
            board[row][col] = '-';
        }
    }

    //Call the method which attempts to solve the problem
    if(solve(sizeOfBoard, board, 0))
    {   //if true, we know that the board array contains a solution
        printBoard(sizeOfBoard, board);
    }
    else
    {   //if false, we know that no solutions exist on this size of board
        System.out.println("No solutions are possible with this board size.");
    }
}

public static boolean solve(int sizeOfBoard, char[][] board, int row)
{
    //If all rows have been filled, we have a solution
    if(row>=sizeOfBoard)
    {
        return true;
    }
    //For each column(space) on the given row
    for(int col=0; col<sizeOfBoard; col++)
    {
        //If placing a queen in this column(space) does not cause a conflict
        if(!checkConflict(sizeOfBoard, board, row, col))
        {
            //place a queen here
            board[row][col]='Q';
            //then call this same function on the next row
            boolean success = solve(sizeOfBoard, board, row+1);
            //if every function in this recursive path returns true
            if(success)
            {
                //then we return true also
                return true;
            }
            //If there is no possible solution with this queen placement,
            //then we replace the queen with an empty and attempt
            //to place a queen in the next column(space).
            else
            {
                board[row][col]='-';
            }
        }
    }
    return false;
}

public static boolean checkConflict(int sizeOfBoard, char[][] board, int rowCheck, int colCheck)
{
    //Check for queens placed directly above this position
    for(int row = rowCheck-1; row>=0; row--)
    {
        if(board[row][colCheck]=='Q')
        {
            return true;
        }
    }

    //Check for queens placed on the diagonal 
    //above and to the right of this position
    int col = colCheck+1;
    for(int row = rowCheck-1; row>=0; row--)
    {

        if(col>=sizeOfBoard)
        {
            break;
        }
        if(board[row][col]=='Q')
        {
            return true;
        }
        col++;
    }

    //Check for queens placed on the diagonal 
    //above and to the left of this position
    col = colCheck-1;
    for(int row = rowCheck-1; row>=0; row--)
    {

        if(col<0)
        {
            break;
        }

        if(board[row][col]=='Q')
        {
            return true;
        }
        col--;
    }
    //We know that no conflicts are caused by this position, so return false
    return false;
}

public static void printBoard(int sizeOfBoard, char[][] board)
{
    for(int row=0; row<sizeOfBoard; row++)
    {
        for(int col=0; col<sizeOfBoard; col++)
        {
            System.out.print(board[row][col]);
        }
        System.out.print("\n");
    }
}
}

关于algorithm - N皇后问题算法方法的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1263391/

相关文章:

确定矩形的 (x,y) 坐标以便周围矩形的面积最小的算法?

c++ - 素数序列的回溯算法

algorithm - 4 个皇后和 1 个马攻击 8*8 棋盘上的所有方 block

c - N皇后使用回溯

c++ - 回溯——用硬币填充网格

c - 如何将此递归转换为循环?

arrays - 如何找到重新排序列表以获取另一个列表所需的步骤列表?

c++ - 为什么我尝试解决UVA 624的代码没有给出正确的输出?

algorithm - 有效地绘制星图

c++ - 在基数树/patricia trie 中进行前缀搜索