八皇后問題藉助位運算秒解
阿新 • • 發佈:2019-01-08
上課手寫的
實測:
#include <stdio.h>
#include <time.h>
int count = 0;
int search(unsigned char A, unsigned char B, unsigned char C)
{
if(B!=255)
{
unsigned char D = ~(A|B|C);
while(D)
{
unsigned char bit = D & (-D);
D ^= bit;
search((A|bit)<<1 , B|bit, (C|bit)>>1);
}
}
else
count ++;
return 0;
}
int main(){
time_t tm;
tm =time(0);
search(0, 0, 0);
printf("count:%d\ntime:%ld\n", count,time(0)-tm);
return 0;
}
結果: