C#算法巧解八皇后问题浅析

开发 后端 算法
C#算法巧解八皇后问题是如何实现的呢?那么本文就像你介绍关于八皇后问题的C#算法的解决。

八皇后问题问题描述:

八皇后问题是一个古老而著名的问题,是回溯算法的典型例题。该问题是十九世纪著名的数学家高斯1850年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。

C#算法的解决方法:

  1. public class Queen  
  2.  {  
  3.   public Queen()  
  4.   {  
  5.    //  
  6.    // TODO: 在此处添加构造函数逻辑  
  7.    //  
  8.   }  
  9.   private static bool[] columflag=new bool[8]{true,  
  10.  true,true,true,true,true,true,true};//列占用标记 true为可用  
  11.   private static bool[] leftflag=new bool[15]{true,true,true,  
  12.  true,true,true,true,true,true,true,true,true,true,true,true};//左行对角线占用标记  
  13.   private static bool[] rightflag=new bool[15]{true,true,  
  14.  true,true,true,true,true,true,true,true,true,true,true,true,true};//右行对角线占用标记  
  15.   private static int[,] position=new int[,]{{0,0,0,0,0,0,0,0},  
  16.    {0,0,0,0,0,0,0,0},  
  17.    {0,0,0,0,0,0,0,0},  
  18.    {0,0,0,0,0,0,0,0},  
  19.    {0,0,0,0,0,0,0,0},  
  20.    {0,0,0,0,0,0,0,0},  
  21.    {0,0,0,0,0,0,0,0},  
  22.    {0,0,0,0,0,0,0,0}  
  23.              };//皇后位置坐标  
  24.   private static int sum=0;  
  25.     
  26.   public static void TryStep(int i)//i取值1至8  
  27.   {  
  28.    for (int j=1;j<=8;j++)   
  29.    {  
  30.     if (columflag[j-1]&&leftflag[i+j-2]&&rightflag[i-j+7])//如果当前位置可以放置  
  31.     {  
  32.      columflag[j-1]=false;  
  33.      leftflag[i+j-2]=false;  
  34.      rightflag[i-j+7]=false;//占用  
  35.  
  36.      position[i-1,j-1]=1;//加入皇后位置  
  37.      if (i<8)  
  38.      {  
  39.       TryStep(i+1);//进入下一行  
  40.      }  
  41.      else 
  42.      {  
  43.       sum++;//解法数统计  
  44.       Console.WriteLine("");  
  45.       Console.WriteLine("第 {0} 种解法:",sum);  
  46.       Console.WriteLine("");  
  47.  
  48.       for (int m=0;m<8;m++) //打印解法  
  49.       {  
  50.        for (int n=0;n<8;n++)  
  51.        {  
  52.         Console.Write("{0} ",position[m,n]);  
  53.        }  
  54.        Console.Write("\n");  
  55.         
  56.       }  
  57.  
  58.      }  
  59.  
  60.      columflag[j-1]=true;//如果不能放置时,取消占座及移走皇后  
  61.      leftflag[i+j-2]=true;  
  62.      rightflag[i-j+7]=true;  
  63.      position[i-1,j-1]=0;  
  64.        
  65.     }  
  66.  
  67.    }  
  68.   }  
  69.  } 

C#算法巧解八皇后问题就向你介绍到这里,希望通过这个例子使你对C#算法有一点认识。

【编辑推荐】

  1. 简单介绍C#预处理
  2. 学习C#无词尾符号经验谈
  3. C#调用QuickTest自动化
  4. 详解C#正规表达式
  5. C#字符串插入html标签
责任编辑:仲衡 来源: 博客园
相关推荐

2021-04-23 21:03:10

MySQL数据语法

2010-10-08 15:53:42

2009-08-11 13:54:54

约瑟夫环算法C#算法

2009-08-11 09:19:52

C#选择排序C#算法

2010-12-24 15:38:53

C#单例模式

2009-09-04 18:16:19

C# Main参数C# Main

2010-09-26 15:53:25

JVM内存溢出

2010-09-17 13:45:40

JVM termina

2009-08-21 14:03:04

C#网络编程

2009-08-17 18:34:50

C# ChangeCo

2009-08-14 17:45:52

C# ArrayLis

2009-08-11 10:26:49

C#算法C#字符串反转

2009-08-07 17:25:37

C# SortedLi

2009-08-25 17:59:49

C#入门

2009-08-24 10:07:57

C#泛型处理

2009-08-11 14:51:11

C#数据结构与算法

2009-08-11 14:43:42

C#数据结构与算法

2009-08-11 14:59:57

一道面试题C#算法

2009-08-18 10:30:30

C#枚举

2009-08-11 10:42:14

C#算法
点赞
收藏

51CTO技术栈公众号