Skip to content

Instantly share code, notes, and snippets.

@ssnau
Created April 6, 2014 17:22
Show Gist options
  • Save ssnau/10008966 to your computer and use it in GitHub Desktop.
Save ssnau/10008966 to your computer and use it in GitHub Desktop.
八皇后神级位运算解法
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