Advertisement
Guest User

Untitled

a guest
Mar 8th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6.  
  7. const int N = 505;
  8.  
  9. bool grid[N][N];
  10. ll visited[N][N][4], k;
  11. int n, m, xKord, yKord, s = 0, moment = 0, len;
  12. string g;
  13.  
  14. void collision()
  15. {
  16.  
  17.     s++;
  18.     s %= 4;
  19.     moment++;
  20. }
  21.  
  22. void move()
  23. {
  24.     if (s == 0) {
  25.         if (yKord == n - 1) {
  26.             collision();
  27.             return;
  28.         }
  29.         else if (grid[xKord][yKord + 1]) {
  30.             collision();
  31.             return;
  32.         }
  33.         else {
  34.             yKord++;
  35.             return;
  36.         }
  37.     }
  38.     if (s == 1) {
  39.         if (xKord == m - 1) {
  40.             collision();
  41.             return;
  42.         }
  43.         else if (grid[xKord + 1][yKord]) {
  44.             collision();
  45.             return;
  46.         }
  47.         else {
  48.             xKord++;
  49.             return;
  50.         }
  51.     }
  52.     if (s == 2) {
  53.         if (yKord == 0) {
  54.             collision();
  55.             return;
  56.         }
  57.         else if (grid[xKord][yKord - 1]) {
  58.             collision();
  59.             return;
  60.         }
  61.         else {
  62.             yKord--;
  63.             return;
  64.         }
  65.     }
  66.     if (s == 3) {
  67.         if (xKord == 0) {
  68.             collision();
  69.             return;
  70.         }
  71.         else if (grid[xKord - 1][yKord]) {
  72.             collision();
  73.             return;
  74.         }
  75.         else {
  76.             xKord--;
  77.             return;
  78.         }
  79.     }
  80. }
  81.  
  82. int main()
  83. {
  84.     ios_base::sync_with_stdio(false);
  85.     cin.tie(nullptr);
  86.     cout.tie(nullptr);
  87.     cerr.tie(nullptr);
  88.     cin >> n >> m;
  89.     cin >> yKord >> xKord;
  90.     cin >> k;
  91.     xKord--;
  92.     yKord--;
  93.     for (int i = 0; i < n; ++i) {
  94.         cin >> g;
  95.         for (int j = 0; j < m; ++j) {
  96.  
  97.             if (g[j] == 46) {
  98.                 grid[j][i] = false;
  99.                 visited[j][i][0] = -1;
  100.                 visited[j][i][1] = -1;
  101.                 visited[j][i][2] = -1;
  102.                 visited[j][i][3] = -1;
  103.             }
  104.             else {
  105.                 grid[j][i] = true;
  106.                 visited[j][i][0] = -2;
  107.                 visited[j][i][1] = -2;
  108.                 visited[j][i][2] = -2;
  109.                 visited[j][i][3] = -2;
  110.             }
  111.         }
  112.     }
  113.  
  114.     bool ok = true;
  115.     while (ok) {
  116.  
  117.         move();
  118.         if (moment == k) {
  119.             break;
  120.         }
  121.         if (visited[xKord][yKord][s] >= 0) {
  122.             len = moment - visited[xKord][yKord][s];
  123.             ok = false;
  124.         }
  125.         else {
  126.             visited[xKord][yKord][s] = moment;
  127.         }
  128.     }
  129.  
  130.     if (ok) {
  131.         xKord++;
  132.         yKord++;
  133.         cout << yKord << " " << xKord << endl;
  134.     }
  135.     else {
  136.         int r = (k - moment) % len;
  137.         moment = 0;
  138.         while (moment < r) {
  139.             move();
  140.         }
  141.         xKord++;
  142.         yKord++;
  143.          cout << yKord << " " << xKord << endl;
  144.     }
  145.  
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement