您的位置:首页 > 产品设计 > UI/UE

N-Queens II

2014-09-16 15:12 127 查看
class Solution {
//always pay attention to do not change the parameters in current level by forwarding
public:
void solveNQueensUtil(int n, int curRow, int upc, int ld, int rd, int& totalCnt)
{
if(curRow == n)
{
totalCnt++;
return;
}

for(int j = 0; j < n; ++j)
{
int curNum = 1<<j;
if(curNum & (ld | rd | upc)) continue;
solveNQueensUtil(n, curRow+1, upc|curNum, (ld|curNum)>>1, (rd|curNum)<<1, totalCnt);
}
}
int totalNQueens(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int totalCnt = 0;
solveNQueensUtil(n, 0, 0, 0, 0, totalCnt);
return totalCnt;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: