Advertisement
Asif_Anwar

UVA-750

Jun 29th, 2021
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. //using namespace chrono;
  5.  
  6. typedef long long ll;
  7. typedef vector< int > vi;
  8. typedef vector< ll > V;
  9. typedef map<int, int > mp;
  10.  
  11. #define pb push_back
  12. #define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  13. #define F first
  14. #define S second
  15.  
  16. #define debug cout << -1 << endl;
  17. #define REP(i, a, b) for(int i=a; i<b; i++)
  18. #define f0r(i, n) for (int i = 0; i < n; ++i)
  19. #define f0r1(i, n) for (int i = 1; i <= n; ++i)
  20. #define r0f(i, n) for(int i=n-1; i>=0; i--)
  21. #define r0f1(i, n) for(int i=n; i>=1; i--)
  22. #define fore(a, x) for (auto& a : x)
  23. #define fori(i, a, b) for (int i = (a); i < (b); ++i)
  24.  
  25. #define MP make_pair
  26. #define UB upper_bound
  27. #define LB lower_bound
  28. #define nw cout << "\n"
  29.  
  30. #define issq(x) (((ll)(sqrt((x))))*((ll)(sqrt((x))))==(x))
  31. #define rev(v) reverse(v.begin(),v.end())
  32. #define asche cerr<<"Ekhane asche\n";
  33. #define rev(v) reverse(v.begin(),v.end())
  34. #define srt(v) sort(v.begin(),v.end())
  35. #define grtsrt(v) sort(v.begin(),v.end(),greater<ll>())
  36. #define all(v) v.begin(),v.end()
  37. #define mnv(v) *min_element(v.begin(),v.end())
  38. #define mxv(v) *max_element(v.begin(),v.end())
  39. #define valid(tx,ty) (tx>=0 && tx<n && ty>=0 && ty<m)
  40. #define one(x) __builtin_popcount(x)
  41. //#define pop pop_back
  42. #define setPrec(x) cout << fixed << setprecision(x)
  43. #define sz(a) (int)a.size()
  44. //#define fin cin
  45. //#define fout cout
  46. const int INF = 1e9;
  47. const ll MOD = (ll)1e9+7;
  48. const ll INFL = 1e18;
  49. const ll mnINF = -1e18;
  50. const double diff = 10e-6;
  51. const int maxn = 200005;
  52. const double PI = acos(-1);
  53. using namespace std;
  54.  
  55. // int dx[] = {-1, 0, 1};
  56. // int dy[] = {-1, 0, 1};
  57.  
  58. int dx[] = {-1, 0, 0, 1};
  59. int dy[] = {0, -1, 1, 0};
  60.  
  61.  
  62. int a, b;
  63. int n = 8;
  64.  
  65. vector< int > ans;
  66. int cc = 0;
  67. int tt = 0;
  68.  
  69. void print()
  70. {  
  71.     if(cc+1<10) cout << " " << ++cc << "     ";
  72.     else cout << ++cc << "     ";
  73.     for(auto xx: ans) {
  74.         cout << " " << xx;
  75.     }
  76.     nw;
  77. }
  78.  
  79. void nQueen(int col, int temp, vector< int > &leftRow, vector< int > &lowerDiagonal, vector< int > &upperDiagonal)
  80. {
  81.     if(col==n) {
  82.        // getAns();
  83.         print();
  84.        return;
  85.     }
  86.  
  87.     for(int row=0; row<8; row++){
  88.         if(col==a) {
  89.             if(row!=b) continue;
  90.         }
  91.  
  92.         if(leftRow[row]==0 && lowerDiagonal[col+row]==0 && upperDiagonal[7+col-row]==0) {
  93.             // safe place to place a queen
  94.  
  95.             leftRow[row] = 1;
  96.             lowerDiagonal[row+col] = 1;
  97.             upperDiagonal[7+col-row] = 1;
  98.  
  99.             ans.pb(row+1);
  100.  
  101.             nQueen(col+1, temp, leftRow, lowerDiagonal, upperDiagonal);
  102.  
  103.             ans.pop_back();
  104.  
  105.             leftRow[row] = 0;
  106.             lowerDiagonal[row+col] = 0;
  107.             upperDiagonal[7+col-row] = 0;
  108.         }
  109.     }
  110. }
  111.  
  112. void solve()
  113. {
  114.     cc = 0;
  115.     cin >> a >> b;
  116.     a--, b--;
  117.     swap(a, b);
  118.     vector< int > leftRow(n, 0), lowerDiagonal(2*n-1, 0), upperDiagonal(2*n-1, 0);
  119.  
  120.     if(tt++) cout << "\n";
  121.     cout << "SOLN       COLUMN\n";
  122.     cout << " #      1 2 3 4 5 6 7 8\n\n";
  123.  
  124.     nQueen(0, 0, leftRow, lowerDiagonal, upperDiagonal);
  125. }
  126.  
  127.  
  128.  
  129. void setIO(string name = "") { // name is nonempty for USACO file I/O
  130.  
  131.     ios_base::sync_with_stdio(0); cin.tie(0); // see Fast Input & Output
  132.  
  133.     // alternatively, cin.tie(0)->sync_with_stdio(0);
  134.  
  135.     if (name.size()) {
  136.  
  137.         freopen((name+".in").c_str(), "r", stdin); // see Input & Output
  138.  
  139.         freopen((name+".out").c_str(), "w", stdout);
  140.  
  141.     }
  142.  
  143. }
  144.  
  145. int main()
  146. {
  147.     //setIO("breedflip");
  148.     FastIO;
  149.     int t;
  150.     t = 1;
  151.     cin >> t;
  152.     f0r(i, t) {
  153.         // cin.ignore();
  154.         //cout << "Case " << i+1 << ": ";
  155.         solve();
  156.     }
  157.     return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement