Advertisement
juanjo12x

UVA_750_8_Queens_Problem

Aug 10th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <string>
  6. #include <cctype>
  7. #include <stack>
  8. #include <queue>
  9. #include <list>
  10. #include <vector>
  11. #include <map>
  12. #include <set>
  13. #include <sstream>
  14. #include <stdlib.h>
  15. #include <cmath>
  16. #define LL unsigned long long
  17. using namespace std;
  18. int row[8],TC,a,b,lineCounter;
  19. bool place(int r,int c){
  20.     for(int prev=0;prev<c;prev++){
  21.         if(row[prev]==r || (abs(row[prev]-r)== abs(prev-c)))
  22.         return false;
  23.     }
  24. return true;
  25. }
  26. void backtrack(int c){
  27.     if(c==8 && row[b]==a){
  28.         printf("%2d      %d",++lineCounter,row[0]+1);
  29.         for(int j=1;j<8;j++) printf(" %d",row[j]+1);
  30.         printf("\n");
  31.     }
  32.     for(int r=0;r<8;r++){
  33.         if(place(r,c)){
  34.             row[c]=r;
  35.             backtrack(c+1);
  36.         }
  37.     }
  38. }
  39.  
  40. int main() {
  41.     scanf("%d",&TC);
  42.     while(TC--){
  43.         scanf("%d %d",&a,&b);a--;b--;
  44.         memset(row,0,sizeof row);lineCounter=0;
  45.         printf("SOLN       COLUMN\n");
  46.         printf(" #      1 2 3 4 5 6 7 8\n\n");
  47.         backtrack(0);
  48.         if(TC) printf("\n");
  49.     }
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement