Advertisement
El_GEMMY

ecpc(bfs)

Aug 21st, 2022
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.18 KB | None | 0 0
  1. // Those who cannot remember the past are
  2. // condemned to repeat it (use DP -_-)
  3. // - George Santayana
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8.  
  9. using namespace std;
  10. using namespace __gnu_pbds;
  11.  
  12. #define all(v) v.begin(), v.end()
  13. #define rall(v) v.rbegin(), v.rend()
  14. #define ll long long
  15. #define ull unsigned long long
  16. #define MOD 1000000007
  17. #define PI acos(-1)
  18. #define ceil(a, b) (((a) / (b)) + ((a) % (b) ? 1 : 0))
  19. #define imin INT_MIN
  20. #define imax INT_MAX
  21. #define llmax LLONG_MAX
  22. #define llmin LLONG_MIN
  23. #define inf 2000000000
  24. #define nl '\n'
  25. #define ppcnt __builtin_popcount
  26. #define ppcntll __builtin_popcountll
  27. #define clz __builtin_clz
  28. #define clzll __builtin_clzll
  29. #define ctz __builtin_ctz
  30. #define ctzll __builtin_ctzll
  31. #define modulo(a, b, mod) ((((a) % (mod)) + ((b) % (mod))) % (mod))
  32. #define cnte(v, x) count(all(v), (x))
  33. #define mine(v) min_element(all(v))
  34. #define maxe(v) max_element(all(v))
  35. #define updmin(a, b) a = min(a, b)
  36. #define updmax(a, b) a = max(a, b)
  37. #define findmod(x, m) x = ((x) % (m) + (m)) % m
  38. #define getmod(x, m) ((x) % (m) + (m)) % (m)
  39. #define debug(x) cout << "x: " << (x) << nl;
  40. #define debug2(x, y) cout << "x: " << (x) << " y: " << y << nl;
  41. #define ordered_set tree<pair<int, int>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>
  42. #define ordered_map tree<int, int, less<>, rb_tree_tag, tree_order_statistics_node_update>
  43.  
  44. //vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1}, dy = {1, -1, 0, 0, 1, -1, 1, -1};
  45. vector<int> dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
  46.  
  47. template<typename T = int>
  48. istream &operator>>(istream &in, vector<pair<int, int>> &v) {
  49.     for (auto &[x, y]: v) in >> x >> y;
  50.     return in;
  51. }
  52.  
  53. template<typename T = int>
  54. istream &operator>>(istream &in, vector<T> &v) {
  55.     for (T &i: v) in >> i;
  56.     return in;
  57. }
  58.  
  59. template<typename T = int>
  60. ostream &operator<<(ostream &out, const vector<T> &v) {
  61.     for (const T &x: v)
  62.         out << x << ' ';
  63.     return out;
  64. }
  65.  
  66. template<typename T = pair<int, int>>
  67. ostream &operator<<(ostream &out, const vector<pair<int, int>> &v) {
  68.     for (auto &[x, y]: v) {
  69.         out << x << ' ' << y << nl;
  70.     }
  71.     return out;
  72. }
  73.  
  74. void Start_Crushing() {
  75.     ios_base::sync_with_stdio(false);
  76.     cin.tie(nullptr);
  77.     cout.tie(nullptr);
  78. #ifndef ONLINE_JUDGE
  79.     freopen("input.txt", "r", stdin);
  80.     freopen("output.txt", "w", stdout);
  81. #endif
  82. }
  83.  
  84. char grid[1003][1003];
  85.  
  86. void solve(){
  87.     int n, m; cin >> n >> m;
  88.  
  89.     int t_x = -1, t_y = -1, c_x = -1, c_y = -1;
  90.  
  91.     for(int i = 0; i < n; i++){
  92.         for(int j = 0; j < m; j++){
  93.             cin >> grid[i][j];
  94.             if(grid[i][j] == 'C')
  95.                 c_x = i, c_y = j;
  96.             else if(grid[i][j] == 'T')
  97.                 t_x = i, t_y = j;
  98.         }
  99.     }
  100.  
  101.     auto valid = [&](int x, int y){
  102.         return x >= 0 and x < n and y >= 0 and y < m and grid[x][y] != '#';
  103.     };
  104.  
  105.     int nasr, alex; cin >> nasr >> alex;
  106.  
  107.     vector<vector<int>> ans_a, ans_b;
  108.     ans_a = ans_b = vector<vector<int>>(n, vector<int>(m, imax));
  109.     queue<pair<int, int>> a, b;
  110.  
  111.     a.push({0, 0}), b.push({n - 1, m - 1});
  112.     ans_a[0][0] = 0, ans_b[n - 1][m - 1] = 0;
  113.  
  114.  
  115.     while(not(a.empty())){
  116.         int size = (int)a.size();
  117.  
  118.         while(size--){
  119.             auto [x, y] = a.front();
  120.             a.pop();
  121.  
  122.             for(int d = 0; d < 4; d++){
  123.                 int new_x = x + dx[d], new_y = y + dy[d];
  124.                 if(valid(new_x, new_y)){
  125.                     if(ans_a[new_x][new_y] > ans_a[x][y] + 1){
  126.                         a.push({new_x, new_y});
  127.                         ans_a[new_x][new_y] = ans_a[x][y] + 1;
  128.                     }
  129.                 }
  130.             }
  131.         }
  132.     }
  133.  
  134.     while(not(b.empty())){
  135.         int size = (int)b.size();
  136.  
  137.         while(size--){
  138.             auto [x, y] = b.front();
  139.             b.pop();
  140.  
  141.             for(int d = 0; d < 4; d++){
  142.                 int new_x = x + dx[d], new_y = y + dy[d];
  143.                 if(valid(new_x, new_y)) {
  144.                     if(ans_b[new_x][new_y] > ans_b[x][y] + 1) {
  145.                         b.push({new_x, new_y});
  146.                         ans_b[new_x][new_y] = ans_b[x][y] + 1;
  147.                     }
  148.                 }
  149.             }
  150.         }
  151.     }
  152.  
  153.     if(c_x == -1)
  154.         return void(cout << "Tie");
  155.  
  156.     int x = min(ans_a[t_x][t_y], ans_a[c_x][c_y]);
  157.     int y = min(ans_b[t_x][t_y], ans_b[c_x][c_y]);
  158.  
  159.     if(min(x, y) == imax)
  160.         return void(cout << "Tie");
  161.  
  162.     x = ceil(x, nasr), y = ceil(y, alex);
  163.  
  164.     cout << (x <= y ? "Nasr" : "Alex");
  165. }
  166.  
  167. void set_file(string &file_name) {
  168.     freopen((file_name + ".in").c_str(), "r", stdin);
  169. //    freopen((file_name + ".out").c_str(), "w", stdout);
  170. }
  171.  
  172. int main() {
  173.     Start_Crushing();
  174.  
  175. //    string file_name = "conan";
  176. //    set_file(file_name);
  177.  
  178.     int t = 1;
  179. //    /*Multiple test cases?*/ cin >> t;
  180.     while (t--) {
  181.         solve();
  182.         if (!t)
  183.             break;
  184.         cout << nl;
  185.     }
  186.  
  187. //    for(int tc = 1; tc <= t; tc++){
  188. //        cout << "Case " << tc << ": ";
  189. //        solve();
  190. ////        if(tc != t)
  191. //            cout << nl;
  192. //    }
  193.  
  194.     return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement