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 | #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #define N 8 int board[N][N]; void put_chess(int); void show_result(); void mark(int, int, int); int main(){ memset(board, 0, sizeof(board)); put_chess(0); } void put_chess(int row){ if(row>=N){ show_result(); return; } for(int col=0;col<N;col++){ if(board[row][col]==0){ mark(row, col, 1); put_chess(row+1); mark(row, col, -1); } } } void show_result(){ for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ if(board[i][j]==0) printf("@"); else printf("."); } printf("\n"); } printf("\n"); system("pause"); } void mark(int row, int col, int flag){ for(int i=0;i<N;i++){ board[row][i]+=flag; board[i][col]+=flag; } for(int i=0;i<N;i++) for(int j=0;j<N;j++){ if( abs(i-row)==abs(j-col) ) board[i][j]+=flag; } board[row][col]=0; } |
Direct link: https://paste.plurk.com/show/103673