Advertisement
Asif_Anwar

UVA-167

Jun 29th, 2021
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 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. int arr[8][8];
  62.  
  63. int n = 8;
  64. int sum = 0;
  65.  
  66. int digit(int num)
  67. {
  68.     int cnt = 0;
  69.     while(num) {
  70.         cnt++;
  71.         num/=10;
  72.     }
  73.     return cnt;
  74. }
  75.  
  76. void nQueen(int col, int temp, vector< int > &leftRow, vector< int > &lowerDiagonal, vector< int > &upperDiagonal)
  77. {
  78.     if(col==n) {
  79.        sum = max(sum, temp);
  80.        return;
  81.     }
  82.  
  83.     for(int row=0; row<8; row++){
  84.         if(leftRow[row]==0 && lowerDiagonal[col+row]==0 && upperDiagonal[7+col-row]==0) {
  85.             // safe place to place a queen
  86.  
  87.             leftRow[row] = 1;
  88.             lowerDiagonal[row+col] = 1;
  89.             upperDiagonal[7+col-row] = 1;
  90.  
  91.             temp += arr[row][col];
  92.  
  93.             nQueen(col+1, temp, leftRow, lowerDiagonal, upperDiagonal);
  94.  
  95.             temp -= arr[row][col];
  96.  
  97.             leftRow[row] = 0;
  98.             lowerDiagonal[row+col] = 0;
  99.             upperDiagonal[7+col-row] = 0;
  100.         }
  101.     }
  102. }
  103.  
  104. void solve()
  105. {
  106.     sum = 0;
  107.     f0r(i, 8) f0r(j, 8) cin >> arr[i][j];
  108.  
  109.     vector< int > leftRow(n, 0), lowerDiagonal(2*n-1, 0), upperDiagonal(2*n-1, 0);
  110.  
  111.     // memset(board, '.', sizeof(board, leftRow, lowerDiagonal, upperDiagonal));
  112.  
  113.     nQueen(0, 0, leftRow, lowerDiagonal, upperDiagonal);
  114.  
  115.     int d = digit(sum);
  116.     for(int i=1; i<=5-d; i++) cout << " ";
  117.     cout << sum << "\n";
  118. }
  119.  
  120.  
  121.  
  122. void setIO(string name = "") { // name is nonempty for USACO file I/O
  123.  
  124.     ios_base::sync_with_stdio(0); cin.tie(0); // see Fast Input & Output
  125.  
  126.     // alternatively, cin.tie(0)->sync_with_stdio(0);
  127.  
  128.     if (name.size()) {
  129.  
  130.         freopen((name+".in").c_str(), "r", stdin); // see Input & Output
  131.  
  132.         freopen((name+".out").c_str(), "w", stdout);
  133.  
  134.     }
  135.  
  136. }
  137.  
  138. int main()
  139. {
  140.     //setIO("breedflip");
  141.     FastIO;
  142.     int t;
  143.     t = 1;
  144.     cin >> t;
  145.     f0r(i, t) {
  146.         // cin.ignore();
  147.         //cout << "Case " << i+1 << ": ";
  148.         solve();
  149.     }
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement