`
xiyanqiu
  • 浏览: 3398 次
  • 性别: Icon_minigender_2
  • 来自: 南京
最近访客 更多访客>>
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
八皇后(Main)
 static void Main(string[] args)
        {
            EightQueen eq = new EightQueen();
            int[] queenList = new int[8];
            for (int j = 0; j < 8; j++)
            {
                Console.WriteLine("-----------------"+j+"---------------------");
                queenList[0] = j;
                bool res = eq.PlaceQueue(queenList, 1);

                if (res)
                {
                    Console.Write("   ");       
                    for (int i = 0; i < 8; i++)
                    {
                        Console.Write(" " + i.ToString() + " ");       
                    }
                    Console.WriteLine("");
                    for (int i = 0; i < 8; i++)
                    {
                        Console.Write(" "+i.ToString()+" ");                       
                        for (int a = 0; a < 8; a++)
                        {                           
                            if (i == queenList[a])
                            {
                                Console.Write(" q ");
                            }
                            else
                            {
                                Console.Write(" * ");
                            }
                        }
                        Console.WriteLine("");
                                
                    }
                  
                    Console.WriteLine("---------------------------------------");
                }
                else
                {
                    Console.WriteLine("不能完成棋局,棋局失败!");
                }
            }
            Console.Read();
        }
八皇后(PlaceQueue)
/// <summary>
        /// 在第col列寻找安全的row值
        /// </summary>
        /// <param name="queenList"></param>
        /// <param name="col"></param>
        /// <returns></returns>
        public bool PlaceQueue(int[] queenList, int col)
        {
            int row = 0;
            bool foundSafePos = false;
            if (col == 8) //结束标志
            {
                //当处理完第8列的完成
                foundSafePos = true;
            }
            else
            {
                while (row < 8 && !foundSafePos)
                {
                    if (IsSafe(col, row, queenList))
                    {
                        //找到安全位置
                        queenList[col] = row;
                        //找下一列的安全位置
                        foundSafePos = PlaceQueue(queenList, col + 1);
                        if (!foundSafePos)
                        {
                            row++;
                        }
                    }
                    else
                    {
                        row++;
                    }
                }
            }
            return foundSafePos;
        }
八皇后(IsSafe)
bool IsSafe(int col,int row,int[] queenList)
        {
            //只检查前面的列
            for (int tempCol = 0; tempCol < col; tempCol++)
            {
                int tempRow = queenList[tempCol];
                if (tempRow == row)
                {
                    //同一行
                    return false;
                }
                if (tempCol == col)
                {
                    //同一列
                    return false;
                }
                if (tempRow - tempCol == row - col || tempRow + tempCol == row + col)
                {
                    return false;
                }
            }
            return true;
        }
Global site tag (gtag.js) - Google Analytics