VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之八皇后问题的Python实现和C#实现(2)

 C#:

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
// EightQueens.cs
namespace EightQueens
{
    class EightQueens
    {
        private bool checkConflict(List<int> queenList, int nextY)
        {
            for (int positionY = 0; positionY < nextY; positionY++)
            {
                if (Math.Abs(queenList[positionY] - queenList[nextY]) == Math.Abs(positionY - nextY) || queenList[positionY] == queenList[nextY])
                {
                    return true;
                }
            }
            return false;
        }
 
        long count = 0;
        public void putQueen(int queenCount, List<int> queenList, int nextY)
        {
            for (queenList[nextY] = 0; queenList[nextY] < queenCount; queenList[nextY]++)
            {
                if (checkConflict(queenList, nextY) == false)
                {
                    nextY++;
                    if (nextY < queenCount)
                    {
                        putQueen(queenCount, queenList, nextY);
                    }
                    else
                    {
                        count++;
                        Console.WriteLine(count.ToString() + ": " string.Join(", ", queenList));
                    }
                    nextY--;
                }
            }
        }
    }
}

相关教程