#include<iostream> #include<cmath> #include<cstdio> #include<cstring> using namespace std; int x[9], a, b, cont = 0; bool place(int queen, int row) { for (int prev = 1; prev < queen; prev++) if (x[prev] == row or abs(x[prev] - row) == abs(prev - queen)) return false; return true; } void Nqueen(int queen) { if (queen == 9) { if (x[b] == a) { printf("%2d %d",++cont, x[1]); for (int i = 2; i <= 8; i++) { printf(" %d", x[i]); } puts(""); } } else { for (int row = 1; row <= 8; row++) { if (place(queen, row)) { x[queen] = row; Nqueen(queen + 1); } } } } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d %d", &a, &b); memset(x, 0, sizeof x); cont = 0; printf("SOLN COLUMN\n"); printf(" # 1 2 3 4 5 6 7 8\n\n"); Nqueen(1); if (t) puts(""); } return 0; }
jueves, 15 de diciembre de 2011
750 - 8 Queens Chess Problem , uva
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario