LeetCode Online Judge 题目C# 练习 - N-Queens

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.
For example,
There exist two distinct solutions to the 4-queens puzzle:
[
 [".Q..", // Solution 1
 "...Q",
 "Q...",
 "..Q."],

 ["..Q.", // Solution 2
 "Q...",
 "...Q",
 ".Q.."]
]

 1         public static List<List<string>> NQueens(int n)

 2         {

 3             //solution is the List of map

 4             List<List<int>> solution = new List<List<int>>();

 5             //map is for Q index on each row, for example ["..Q."] map[row] = 2;

 6             List<int> map = new List<int>();

 7             //initial map for later

 8             for (int i = 0; i < n; i++)

 9                 map.Add(0);

10             NQueensHelper(solution, map, 0, n);

11 

12             List<List<string>> ret = new List<List<string>>();

13 

14             //Construct the ret with solution

15             for (int i = 0; i < solution.Count; i++)

16             {

17                 List<string> board = new List<string>();

18                 string temp = "";

19                 for (int j = 0; j < n; j++)

20                 {

21                     for(int k = 0; k < n; k++)

22                     {

23                         if (k == solution[i][j])

24                             temp += "Q";

25                         else

26                             temp += ".";

27                     }

28                     board.Add(temp);

29                     temp = "";

30                 }

31                 ret.Add(board);

32             }

33 

34             return ret;

35         }

36 

37         public static void NQueensHelper(List<List<int>> solutions, List<int> map, int row, int n)

38         {

39             if(n == 1)

40             {

41                 map[0] = 0;

42                 solutions.Add(map);

43                 return;

44             }

45             

46             if (row >= n)

47             {

48                 solutions.Add(new List<int>(map));

49                 return;

50             }

51 

52             for (int i = 0; i < n; i++)

53             {

54                 if (row == 0)

55                 {

56                     map[row] = i;

57                     NQueensHelper(solutions, map, row + 1, n);

58                 }

59                 else

60                 {

61                     bool valid = true;

62                     for (int j = row - 1; j >= 0; j--)

63                     {

64                         //check if Q can be placed on this position

65                         //Critical Algorithm here...

66                         if (i == map[j] || Math.Abs(i - map[j]) == row - j)

67                         {

68                             valid = false;

69                             break;

70                         }

71                     }

72 

73                     if (valid)

74                     {

75                         map[row] = i;

76                         NQueensHelper(solutions, map, row + 1, n);

77                     }

78                 }

79             }

80        

代码分析:

  N皇后,经典的递归题。第一次写不出来,,第二次复习的时候居然跌跌撞撞的写出来了。O(n3)。

  这种要求所有可能的解的题目,递归比DP好,很多时候DP未必能做出来。

下面把我贴到LEETCODE上跑的C++的代码也贴一下,以后好复习C++跟C#的区别

 1 vector<vector<string> > solveNQueens(int n) {

 2         // Start typing your C/C++ solution below

 3         // DO NOT write int main() function

 4             vector<vector<int> > solution;// = new List<List<int>>();

 5             vector<int> map;// = new List<int>();

 6             for (int i = 0; i < n; i++)

 7                 map.push_back(0);

 8             NQueensHelper(solution, map, 0, n);

 9 

10             vector<vector<string> > ret;// = new List<List<string>>();

11 

12             for (int i = 0; i < solution.size(); i++)

13             {

14                 vector<string> board;// = new List<string>();

15                 string temp = "";

16                 for (int j = 0; j < n; j++)

17                 {

18                     for(int k = 0; k < n; k++)

19                     {

20                         if (k == solution[i][j])

21                             temp += "Q";

22                         else

23                             temp += ".";

24                     }

25                     board.push_back(temp);

26                     temp = "";

27                 }

28                 ret.push_back(board);

29             }

30 

31             return ret;

32         }

33 

34         void NQueensHelper(vector<vector<int> > &solutions, vector<int> &map, int row, int n)

35         {

36             if(n == 1)

37             {

38                 map[0] = 0;

39                 solutions.push_back(map);

40                 return;

41             }

42             

43             if (row >= n)

44             {

45                 solutions.push_back(vector<int>(map));

46                 return;

47             }

48 

49             for (int i = 0; i < n; i++)

50             {

51                 if (row == 0)

52                 {

53                     map[row] = i;

54                     NQueensHelper(solutions, map, row + 1, n);

55                 }

56                 else

57                 {

58                     bool valid = true;

59                     for (int j = row - 1; j >= 0; j--)

60                     {

61                         if (i == map[j] || abs(i - map[j]) == row - j)

62                         {

63                             valid = false;

64                             break;

65                         }

66                     }

67 

68                     if (valid)

69                     {

70                         map[row] = i;

71                         NQueensHelper(solutions, map, row + 1, n);

72                     }

73                 }

74             }

75         }

 

  

你可能感兴趣的:(LeetCode)