Untitled
raw download clone
C
views 27
,
size 1009 b
#include<stdio.h>

int q[15][15]={0},COL[50],XL[50],XR[50];//其實不用開二維q,因為沒要輸出圖形
int N,total=0;

int valid(int row, int col);
void queen(int row_now);

int main(void)
{
     scanf("%d",&N);
     queen(0);
     printf("%d\n",total);

     return 0;
}
int valid(row,col)
{
     if(COL[col]||XL[row-col+15]||XR[row+col])return 0;
     return 1;
}
void queen(row_now)
{
     if(row_now==N)
     {
          total++;
          //printf("%d\n",total);
     }
     else
     {
          for(int j=0;j<N;j++)
          {
               if(valid(row_now,j))
               {
                    q[row_now][j]=1;
                    COL[j]=1;
                    XL[row_now-j+15]=1;
                    XR[row_now+j]=1;
                    queen(row_now+1);
                    q[row_now][j]=0;
                    COL[j]=0;
                    XL[row_now-j+15]=0;
                    XR[row_now+j]=0;
               }
          }
     }

}
close fullscreen
Login or Register to edit or fork this paste. It's free.