Naxocist

TurnInMatrix

Apr 27th, 2022
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using pi = pair<int, int>;
  5.  
  6. pi operator +(const pi &a, const pi &b){
  7.     return pi(a.first + b.first, a.second + b.second);
  8. }
  9.  
  10. enum Direction {
  11.     U, D, L, R, None
  12. };
  13.  
  14. using tup = tuple<int, Direction, pi>;
  15. queue<tup> q;
  16.  
  17. const int N = 2500, INF = 1e9;
  18.  
  19. char mp[N][N];
  20. int ar[N][N];
  21. pi diff[] = {{-1, 0}, {1, 0}, {0,-1}, {0, 1}};
  22.  
  23.  
  24. int main(){
  25.  
  26.     ios_base::sync_with_stdio(false); cin.tie(nullptr);
  27.  
  28.     int n; cin >> n;
  29.     pi st;
  30.     for(int i=0; i<n; ++i){
  31.         for(int j=0; j<n; ++j){
  32.             char &c = mp[i][j];
  33.             cin >> c;
  34.             if(c == 'K'){
  35.                 st = {i, j};
  36.             }
  37.             ar[i][j] = INF;
  38.         }
  39.     }
  40.  
  41.     q.emplace(-1, None, st);
  42.  
  43.     while(q.size()){
  44.         tup f = q.front(); q.pop();
  45.  
  46.         int turn = get<0>(f); Direction lastDir = get<1>(f); pi pos = get<2>(f);
  47.         int i = pos.first, j = pos.second;
  48.  
  49.         if(turn > ar[i][j]) continue;
  50.  
  51.         for(Direction newDir : {L, R, U, D}){
  52.             pi nPos = pos + diff[newDir];
  53.             int h = nPos.first, k = nPos.second, nTurn = turn + (lastDir != newDir);
  54.            
  55.             if(h < 0 || k < 0 || h >= n || k >= n || mp[h][k] != '.' || nTurn > ar[h][k]) continue;    
  56.  
  57.             ar[h][k] = nTurn;
  58.             q.emplace(nTurn, newDir, nPos);
  59.         }
  60.     }
  61.  
  62.     int mx = -1e9, cnt = 1;
  63.     for(int i=0; i<n; ++i){
  64.         for(int j=0; j<n; ++j){
  65.             if(mp[i][j] == 'K') {
  66.                 cout << "K ";
  67.                 continue;
  68.             }
  69.             if(mp[i][j] != '.' || ar[i][j] == INF) {
  70.                 cout << "# ";
  71.                 continue;
  72.             }
  73.             cout << ar[i][j] << ' ';
  74.             int k = ar[i][j];
  75.             if(k > mx){
  76.                 mx = k;
  77.                 cnt = 1;
  78.             }else if(k == mx) cnt++;
  79.         }
  80.         cout << '\n';
  81.     }
  82.     cout << mx << '\n' << cnt;
  83.     return 0;
  84. }
  85.  
  86. /*
  87. 8
  88. .....#..
  89. ...#....
  90. .#......
  91. ......#.
  92. #..#.K.#
  93. ..#.....
  94. ......#.
  95. .#...#..
  96. */
Advertisement
Add Comment
Please, Sign In to add comment