Created
April 6, 2014 17:22
-
-
Save ssnau/10008966 to your computer and use it in GitHub Desktop.
八皇后神级位运算解法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int totalNQueens(int n) { | |
upperlim = (1<<n) - 1; | |
count = 0; | |
dfs(0,0,0); | |
return count; | |
} | |
public void dfs(int row, int leftX, int rightX) { | |
if (row == upperlim) { | |
count++; | |
return; | |
} | |
int availables = upperlim & ~(row | leftX | rightX); | |
int pos; | |
while (availables != 0) { | |
pos = availables & (-availables); | |
dfs(row|pos, (leftX|pos)<<1, (rightX|pos)>>1); | |
availables -= pos; | |
} | |
} | |
private int upperlim; | |
private int count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment