题意:
八皇后;
*的位置不能放;
思路:
直接回溯会超时,
所以用位运算;
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 20;
int INF;
int n, sum;
char g[N][N];
int s[N];
int dfs(int cur, int d, int lu, int ru) {
int res = 0;
if(cur == n) {
return 1;
}
int p = ~(s[cur] | d | lu | ru);
int sta = p & (-p) & INF;
while(sta) {
res += dfs(cur + 1, d|sta , (sta|lu) << 1 ,(sta|ru) >> 1);
p ^= sta;
sta = p & (-p) & INF;
}
return res;
}
int main() {
int cas = 1;
while(scanf("%d",&n) && n) {
INF = (1 << n) - 1;
memset(s, 0, sizeof(s));
for(int i = 0; i < n; i++) {
scanf("%s",g[i]);
for(int j = 0; j < n; j++) {
if(g[i][j] == '*')
s[i] |= (1 << j);
}
}
printf("Case %d: %d\n",cas++, dfs(0, 0, 0, 0));
}
}