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

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

八皇后问题问题描述:

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

C#算法的解决方法:

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

2009-08-11 13:54:54

约瑟夫环算法C#算法

2010-10-08 15:53:42

2009-08-11 09:19:52

C#选择排序C#算法

2009-09-04 18:16:19

C# Main参数C# Main

2010-12-24 15:38:53

C#单例模式

2009-08-21 14:03:04

C#网络编程

2009-08-17 18:34:50

C# ChangeCo

2009-08-14 17:45:52

C# ArrayLis

2010-09-26 15:53:25

JVM内存溢出

2010-09-17 13:45:40

JVM termina

2009-08-07 17:25:37

C# SortedLi

2009-08-11 10:26:49

C#算法C#字符串反转

2009-08-25 17:59:49

C#入门

2009-08-24 10:07:57

C#泛型处理

2009-08-11 10:42:14

C#算法

2009-08-11 14:51:11

C#数据结构与算法

2009-08-11 14:43:42

C#数据结构与算法

2009-08-11 14:59:57

一道面试题C#算法

2009-08-10 14:43:03

C#函数Convert
点赞
收藏

51CTO技术栈公众号