1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
   | #include<iostream> #include<cstdio> #include<cmath> using namespace std; bool board[14][14],col[14],line[14],NE_SW[26],NW_SE[26]; int n,ans,solution[3][15]; bool flag(int, int); void dfs(int, int); int main() {     cin>>n;     for(int i=1;i<=n;i++) {col[i]=true; line[i]=true; NE_SW[i]=true; NW_SE[i]=true; NE_SW[i+n]=true; NW_SE[i+n]=true;}     dfs(1,1);     for(int i=0;i<3;i++){         for(int j=1;j<=n;j++){             if(solution[i][j]==0) solution[i][j]=solution[i-1][j];             cout<<solution[i][j]<<" ";
          }         cout<<endl;     }     cout<<ans; } bool flag(int x,int y) {     if(col[x]&&line[y]){if(NE_SW[x+y-1]){         if( y<=x && NW_SE[n-int(abs(x-y))] ) return true;         else if( y>x && NW_SE[n+int(abs(x-y))] ) return true;              }     }          return false; } void dfs(int x,int y) {          if(flag(x,y)){                  if(ans<=3) solution[ans][x]=y;
                   col[x]=false;         line[y]=false;         if(y<=7-x) NE_SW[x+y-1]=false;              else NE_SW[x+y-1]=false;          if(y<=x) NW_SE[n-int(abs(x-y))]=false;              else NW_SE[n+int(abs(x-y))]=false; 
          if(x==n) {ans++;}          else dfs(x+1,1);
          col[x]=true;         line[y]=true;         if(y<=7-x) NE_SW[x+y-1]=true;              else NE_SW[x+y-1]=true;          if(y<=x) NW_SE[n-int(abs(x-y))]=true;              else NW_SE[n+int(abs(x-y))]=true;          
      }
      if(y<n) dfs(x,y+1); }
 
   |