zoukankan      html  css  js  c++  java
  • 递归 八皇后问题

    原文发布时间为:2009-03-04 —— 来源于本人的百度文章 [由搬家工具导入]

    using System;
    //using System.Collections.Generic;
    //using System.Text;

    namespace digui1
    {
        public class Class4//八皇后
        {
            public static char[,] queen = new char[8, 8];
            public static int[] a = new int[8];
            public static int[] b = new int[15];
            public static int[] c = new int[15];
            public static int num = 0;

            public static void Main()
            {
                for (int i = 0; i < 8; i++)
                    for (int j = 0; j < 8; j++)
                        queen[i, j] = '*';
                fun(0);
                Console.ReadLine();
            }

            public static void fun(int i)
            {
                int iColum;
                for (iColum = 0; iColum < 8; iColum++)
                {
                    if (a[iColum] == 0 && b[i - iColum + 7] == 0 && c[i + iColum] == 0)
                    {
                        queen[i, iColum] = ;
                        a[iColum] = 1;
                        b[i - iColum + 7] = 1;
                        c[i + iColum] = 1;
                        if (i < 7)
                            fun(i + 1);
                        else
                        {
                            Console.WriteLine("第{0}种状态为:", ++num);
                            for (int j = 0; j < 8; j++)
                            {
                                for (int k = 0; k < 8; k++)
                                    Console.Write("{0}", queen[j, k]);
                                Console.WriteLine();
                            }
                        }
                        queen[i, iColum] = '*';
                        a[iColum] = 0;
                        b[i - iColum + 7] = 0;
                        c[i + iColum] = 0;
                    }
                }
            }
        }
    }

  • 相关阅读:
    $on , $emit , $broadcast , $apply
    angularJS 从后台获取所有分类,并默认选中值(select)
    angularJS 发送get、post请求
    微信小程序点击不同的按钮,展示不同的信息内容
    小程序---模板的引用与使用
    项目最全的小程序源码网址
    小程序多个商品数量之间的增减与总价
    leetcode——两数之和(暴力,一遍hash,两遍hash,双指针)
    java在线程和内部类中的使用final关键字
    java线程——多线程访问成员变量与局部变量
  • 原文地址:https://www.cnblogs.com/handboy/p/7153336.html
Copyright © 2011-2022 走看看