1. 程式人生 > >八皇后問題藉助位運算秒解

八皇后問題藉助位運算秒解

上課手寫的
這裡寫圖片描述

實測:

#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; }

結果:
這裡寫圖片描述