/ Published in: C
Expand |
Embed | Plain Text
#include "stdio.h" #define SIZE 8 void printrow(unsigned int q) { for (int i=0; i < SIZE; i++) { char c = (q >> i) & 1 ? '*' : '.'; putchar(c); } putchar('n'); } extern void placeQ(int x, int y) { static unsigned int Q[SIZE]; static unsigned int nSolutions; unsigned int col0, col1, col2,q; col0 = 1 << (SIZE - 1 - x); for (int row = 0; row < y; row++) { col1 = col0 << (y - row); col2 = col0 >> (y - row); q = Q[row]; if (q == col0 || q == col1 || q == col2) goto nextcolumn; } Q[y] = col0; if (y == SIZE - 1) // finished { for (int i = 0; i < SIZE; i++) printrow(Q[i]); } else { // continue to try next row placeQ(0, y + 1); } Q[y] = 0; nextcolumn: if (x < SIZE - 1) placeQ(x + 1, y); } int main (int argc, char const *argv[]) { placeQ(0, 0); return 0; }
You need to login to post a comment.
