c夏普 8皇后_某年某月_新浪博客

本文介绍了一个使用C#实现的八皇后问题解决方案。通过递归和判断函数确定每个皇后的位置,确保任意两个皇后不会在同一行、同一列或同一对角线上。程序能够找到所有可能的摆放方案,并展示每种方案的具体布局。
    public partial class Form1 : Form
    {
        public int[] hs = new int[8]; //皇后數組
        public int sum1 = 0;          //解個數
        public string s_wt = "";      //輸出結果

        public Form1()
        {
            InitializeComponent();
        }

        void js(int num)
        {
            for (int i = 1; i < 9; i++)
            {
                if (num == 8)             //安置完8個皇后輸出
                {
                    sum1++;
                    show1();
                    break;
                }
                if (bj1(num, i))         //判斷安置位置是否安全
                {
                    hs[num] = i;         //如果安全,安置皇后,跳下一行
                    js(num + 1);
                }                        //如果失敗,退回原循環繼續
            }
        }

        bool bj1(int row, int p)
        {
            if (row == 0)                //棋盤上沒有皇后,安全
            {
                return true;
            }
            else
            {
                for (int i = 0; i < row; i++)
                {
                    if (!bj2(hs[i], row - i, p))
                    {
                        return false;
                    }
                }
                return true;
            }
        }
        ///
        /// 
        ///
        /// 上一個安全的皇后
        /// 列數差
        /// 當前安置的皇后
        ///
        bool bj2(int i, int row_c, int j)
        {
            if (i == j || (i - j) == row_c || (j - i) == row_c)   //i上一個皇后不等於j當前安置的皇后,且兩皇后不在一條斜線上
            {
                return false;
            }
            return true;
        }

        
        public void show1()
        {
            //输出皇后的个数排序
            s_wt += string.Format("{0}[" + getst(hs) + "]" + (sum1 % 4 == 0 ? System.Environment.NewLine : " "), sum1.ToString("00"));
            //for (int i = 0; i < 8; i++)
            //{
            //    for (int j = 1; j < 9; j++)
            //    {
            //        if (j == hs[i])
            //        {
            //            s_wt += "■";
            //        }
            //        else
            //        {
            //            s_wt += "□";
            //        }
            //    }
            //    //换行
            //    s_wt += System.Environment.NewLine;
            //}
        }

        string getst(int[] i2)
        {
            string s = "";
            foreach (var item in i2)
            {
                s += item.ToString() + " ";
            }
            return s;
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            js(0);
            txt1.Text = s_w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 6 3 7 2 4 ] 50[5 2 4 6 8 3 1 7 ] 51[5 2 4 7 3 8 6 1 ] 52[5 2 6 1 7 4 8 3 ]
53[5 2 8 1 4 7 3 6 ] 54[5 3 1 6 8 2 4 7 ] 55[5 3 1 7 2 8 6 4 ] 56[5 3 8 4 7 1 6 2 ]
57[5 7 1 3 8 6 4 2 ] 58[5 7 1 4 2 8 6 3 ] 59[5 7 2 4 8 1 3 6 ] 60[5 7 2 6 3 1 4 8 ]
61[5 7 2 6 3 1 8 4 ] 62[5 7 4 1 3 8 6 2 ] 63[5 8 4 1 3 6 2 7 ] 64[5 8 4 1 7 2 6 3 ]
65[6 1 5 2 8 3 7 4 ] 66[6 2 7 1 3 5 8 4 ] 67[6 2 7 1 4 8 5 3 ] 68[6 3 1 7 5 8 2 4 ]
69[6 3 1 8 4 2 7 5 ] 70[6 3 1 8 5 2 4 7 ] 71[6 3 5 7 1 4 2 8 ] 72[6 3 5 8 1 4 2 7 ]
73[6 3 7 2 4 8 1 5 ] 74[6 3 7 2 8 5 1 4 ] 75[6 3 7 4 1 8 2 5 ] 76[6 4 1 5 8 2 7 3 ]
77[6 4 2 8 5 7 1 3 ] 78[6 4 7 1 3 5 2 8 ] 79[6 4 7 1 8 2 5 3 ] 80[6 8 2 4 1 7 5 3 ]
81[7 1 3 8 6 4 2 5 ] 82[7 2 4 1 8 5 3 6 ] 83[7 2 6 3 1 4 8 5 ] 84[7 3 1 6 8 5 2 4 ]
85[7 3 8 2 5 1 6 4 ] 86[7 4 2 5 8 1 3 6 ] 87[7 4 2 8 6 1 3 5 ] 88[7 5 3 1 6 8 2 4 ]
89[8 2 4 1 7 5 3 6 ] 90[8 2 5 3 1 7 4 6 ] 91[8 3 1 6 2 5 7 4 ] 92[8 4 1 3 6 2 7 5 ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值