#include <stdlib.h> #include <stdio.h>
#define SIZE 8
void printqueen(int **a, int col, int row); int queen(int **arr, int col, int row); void lost(int y);
static int a[8][8] = { {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0} }; static int *p = &a[0][0]; static int **pp = &p;
int main(void){ printqueen(pp, 8, 8); lost(0); printqueen(pp, 8, 8); return 0; }
void printqueen(int **arr, int col, int row){ int a, b; for(a = 0;a < col;a++) { for(b = 0; b < row; b++) { if(*(*arr + SIZE*a + b) != 0) printf("* "); else printf(". "); } putchar('\n'); } puts("-----------------\n"); }
//if can be falldown, return 1, else return 0
int queen(int **arr, int col, int row){ int x, y;// x = row, y = col
for(x = 0; x < SIZE; x++) { if(x == row) continue; if(*(*arr + row*SIZE + x) == 1) return 0; }
for(y = 0; y < SIZE; y++) { if(y == col) continue; if(*(*arr + y*SIZE + col) == 1) return 0; }
for(x = 0; x < SIZE; x++) for(y = 0; y < SIZE; y++) { if(x == row && y == col) continue; if( ((x + y) == (row + col)) || ((y - x) == (col - row)) ){ if(*(*arr + x*SIZE + y) == 1) return 0; } } return 1; }
void lost(int y){ int x; if(y > SIZE) return; for(x = 0; x < SIZE; x++) { if(queen(pp, x, y)){ *(*pp + y*SIZE + x) = 1; lost(y+1); } } }
|