mr_dot_convict

11553_grid_game-UVa-mr.convict

Jun 6th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  2.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  3.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  4.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  5.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  6. When I wrote this, only God and I understood what I was doing
  7.  ** * * * * * * * Now, only God knows * * * * * * */
  8. #include         <bits/stdc++.h>
  9. using namespace std;
  10. #pragma GCC      optimize ("Ofast")
  11. #pragma GCC      optimize ("unroll-loops")
  12. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  13.  
  14. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  15. #define PREC     cout.precision (10); cout << fixed
  16. #define bg(x)    " [ " << #x << " : " << (x) << " ]"
  17. #define x        first
  18. #define y        second
  19.  
  20. #define debug(args...) { \
  21.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  22.    stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
  23. }
  24. void err(istream_iterator<string> it) { it->empty();
  25.    cerr << " (Line : " << __LINE__ << ")" << '\n';
  26. }
  27. template<typename T, typename... Args>
  28. void err(istream_iterator<string> it, T a, Args... args) {
  29.     cerr << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  30.     err(++it, args...);
  31. }
  32.  
  33.  
  34. const int N = 8;
  35. int mat[N][N];
  36. int rows, cols, n;
  37.  
  38. void read() {
  39.    cin >> n;
  40.    rows = cols = n;
  41.  
  42.    for (int r = 0; r < rows; ++r) {
  43.       for (int c = 0; c < cols; ++c) {
  44.          cin >> mat[r][c];
  45.       }
  46.    }
  47.    vector <int> perm;
  48.    for (int i = 0; i < n; ++i)
  49.       perm.push_back(i);
  50.  
  51.    int mn = INT_MAX;
  52.    do {
  53.       int val = 0;
  54.       for (int r = 0; r < rows; ++r) {
  55.          val += mat[r][perm[r]];
  56.       }
  57.       mn = min(val, mn);
  58.    } while (next_permutation(perm.begin(), perm.begin() + n));
  59.    cout << mn << '\n';
  60. }
  61.  
  62. signed main() {
  63.    IOS; PREC;
  64.    int tc;
  65.    cin >> tc;
  66.    while (tc--) {
  67.       read();
  68.    }
  69.    return EXIT_SUCCESS;
  70. }
Add Comment
Please, Sign In to add comment