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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
#include <iostream> #include <fstream> #include <iomanip> #include <ctime> #include <cstring>
using namespace std;
const int MAXNUM = 12 ;
int NUM ;
const char *qq="/**/"; const char *pp="/**/"; const char *rr="/**/""//";
void printBoard(const char [][MAXNUM]);
void place(char [][MAXNUM],int,int);
void rotatematrix90(int rrow[MAXNUM],int rrow1[MAXNUM]);
bool canfound(int rrow[MAXNUM], int rowcol[][MAXNUM],int solution);
int rowcol[14200][MAXNUM]; int solution=0; int diffrowcol[3574][MAXNUM]; int diffsolution=0;
char c='\"'; const char* p1 = "sdjksd\"\\d//fj/*kdhjk\"dsfjl*/dks";
int main() {
cout << "//The number of queens (4 -- 12) :// " ; cin >> NUM;
long startTime=clock();
char board[MAXNUM][MAXNUM];
for (char y=0;y<NUM;y++) { memset(board,0,MAXNUM*MAXNUM); place(board,0,y); }
long endTime=clock();
ofstream outFile1("full.txt"); for ( int i=0;i<solution;i++ ) { outFile1 << setw(3) << i+1 << ":" ; for (int j=0;j<NUM;j++) outFile1 << "/*(" << rowcol[i][j]/100 << ',' << rowcol[i][j]%100 << "*/"; outFile1 << endl; }
ofstream outFile2("unique.txt"); for (int i=0;i<diffsolution;i++ ) { outFile2 << setw(3) << i+1 << ":" ; for (int j=0;j<NUM;j++) outFile2 << "/*(" << rowcol[i][j]/100 << ',' << rowcol[i][j]%100 << "*/)"; outFile2 << endl; }
cout << endl << "//Total number of solutions after eliminating solutions \n" "//that can be represented by rotating chessboard is " << diffsolution << "// ." << endl;
cout << endl << "Time Elasped : " << 1000*(double)(endTime-startTime) / CLOCKS_PER_SEC << " microseconds." << endl;
{char ch; cout<<"press any key to exit" <<endl; cin.get(ch);cin.get(ch);} return 0; }
void printBoard(const char board[][MAXNUM]) { int rrow[MAXNUM],rrow1[MAXNUM],rrow2[MAXNUM],rrow3[MAXNUM]; for (int row=0; row<NUM; row++) for (int col=0; col<NUM; col++) if (board[row][col]>0) rrow[row]=row*100+col;
{ cout << "Solution " << setw(5) << solution+1 << ": "; for (int j=0;j<NUM;j++) { rowcol[solution][j]=rrow[j]; cout << '(' << rrow[j]/100 <<','<<rrow[j]%100 << ')'; } solution++; cout << endl;
rotatematrix90(rrow,rrow1); if (canfound(rrow1,diffrowcol,diffsolution)) return; rotatematrix90(rrow1,rrow2); if (canfound(rrow2,diffrowcol,diffsolution)) return; rotatematrix90(rrow2,rrow3); if (canfound(rrow3,diffrowcol,diffsolution)) return;
for (int j=0;j<NUM;j++) diffrowcol[diffsolution][j]=rrow[j]; diffsolution++; } }
void place(char board[][MAXNUM], int r, int c) { if (r+1==NUM) { board[r][c] = r+1; printBoard(board); return; }
for (int idx=0; idx<NUM; idx++) { board[r][idx] = -9; board[idx][c] = -9; int t=c+r-idx; if ( t>=0 && t<NUM ) board[idx][t]=-9; t = c-r+idx; if ( t>=0 && t<NUM ) board[idx][t]=-9; } board[r][c] = r+1;
for (int col=0; col<NUM; col++) if (!board[r+1][col]) { char bakboard[MAXNUM][MAXNUM]; memcpy(bakboard,board,MAXNUM*MAXNUM*sizeof(char)); place(bakboard,r+1,col); } }
void rotatematrix90(int rrow[MAXNUM],int rrow1[MAXNUM]) { for (int i=0;i<NUM;i++) rrow1[i]=rrow[i]/100+100*(NUM-1-rrow[i]%100); for (int i=0;i<NUM;i++) { int min = rrow1[i]; int minidx=i; for (int j=i+1;j<NUM;j++) if (rrow1[j]<min) { min=rrow1[j]; minidx=j; } rrow1[minidx]=rrow1[i]; rrow1[i]=min; } }
bool canfound(int rrow[MAXNUM],int rowcol[][MAXNUM],int solution) { bool found = false; for (int i=0; i<solution && !found; i++)
found = !memcmp(rrow,rowcol[i],NUM*sizeof(int)); return found; }
a
|