-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathNQueensII.cs
executable file
·58 lines (55 loc) · 3.12 KB
/
NQueensII.cs
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
// Source : https://leetcode.com/problems/n-queens-ii/
// Author : codeyu
// Date : Friday, November 11, 2016 11:55:18 PM
/**********************************************************************************
*
* Follow up for N-Queens problem.
*
* Now, instead outputting board configurations, return the total number of distinct solutions.
*
*
*
**********************************************************************************/
using System;
using System.Collections.Generic;
using Algorithms.Utils;
using System.Text;
using System.Linq;
namespace Algorithms
{
public class Solution052
{
public static int TotalNQueens(int n)
{
IList<int> res = new List<int>();
res.Add(0);
helper(n,0,new int[n], res);
return res[0];
}
private static void helper(int n, int row, int[] columnForRow, IList<int> res)
{
if(row == n)
{
res[0] = res[0] + 1;
return;
}
for(int i=0;i<n;i++)
{
columnForRow[row] = i;
if(check(row,columnForRow))
{
helper(n,row+1,columnForRow,res);
}
}
}
private static bool check(int row, int[] columnForRow)
{
for(int i=0;i<row;i++)
{
if(columnForRow[row]==columnForRow[i] || Math.Abs(columnForRow[row]-columnForRow[i])==row-i)
return false;
}
return true;
}
}
}