示例#1
0
文件: Form1.cs 项目: matmape/nqueen
        private void Solve()
        {
            try
            {
                pnlChess.Visible = false;
                pnlChess.Controls.Clear();
                if (txtN.Text == "")
                {
                    MessageBox.Show("Enter a valid number");
                    return;
                }
                var numberOfQueens = int.Parse(txtN.Text);
                if (numberOfQueens < 4)
                {
                    MessageBox.Show("Enter a number greater than or equals to 4");
                    return;
                }
                var board     = new AStarBoard(numberOfQueens);
                var stopWatch = new Stopwatch();

                stopWatch.Start();
                var result = AStarSearch(board);
                stopWatch.Stop();
                if (result != null)
                {
                    pnlChess.Visible = true;
                    result.ShowBoard(pnlChess);
                }
                else
                {
                    MessageBox.Show("No Result");
                }
            }
            catch
            {
                MessageBox.Show("Error occured while trying to solve the NQueen Problem.");
            }
        }
示例#2
0
文件: Form1.cs 项目: matmape/nqueen
 AStarBoard AStarSearch(AStarBoard board)
 {
     if (board.IsGoal())
     {
         return(board);
     }
     else
     {
         for (var i = 0; i < board.NumberOfQueens; i++)
         {
             if (board.IsSafe(i))
             {
                 board.Place(i);
                 var result = AStarSearch(board);
                 if (result != null)
                 {
                     return(result);
                 }
                 board.UnPlace();
             }
         }
     }
     return(null);
 }