Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int N = 505;
- bool grid[N][N];
- ll visited[N][N][4], k;
- int n, m, xKord, yKord, s = 0, moment = 0, len;
- string g;
- void collision()
- {
- s++;
- s %= 4;
- moment++;
- }
- void move()
- {
- if (s == 0) {
- if (yKord == n - 1) {
- collision();
- return;
- }
- else if (grid[xKord][yKord + 1]) {
- collision();
- return;
- }
- else {
- yKord++;
- return;
- }
- }
- if (s == 1) {
- if (xKord == m - 1) {
- collision();
- return;
- }
- else if (grid[xKord + 1][yKord]) {
- collision();
- return;
- }
- else {
- xKord++;
- return;
- }
- }
- if (s == 2) {
- if (yKord == 0) {
- collision();
- return;
- }
- else if (grid[xKord][yKord - 1]) {
- collision();
- return;
- }
- else {
- yKord--;
- return;
- }
- }
- if (s == 3) {
- if (xKord == 0) {
- collision();
- return;
- }
- else if (grid[xKord - 1][yKord]) {
- collision();
- return;
- }
- else {
- xKord--;
- return;
- }
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- cerr.tie(nullptr);
- cin >> n >> m;
- cin >> yKord >> xKord;
- cin >> k;
- xKord--;
- yKord--;
- for (int i = 0; i < n; ++i) {
- cin >> g;
- for (int j = 0; j < m; ++j) {
- if (g[j] == 46) {
- grid[j][i] = false;
- visited[j][i][0] = -1;
- visited[j][i][1] = -1;
- visited[j][i][2] = -1;
- visited[j][i][3] = -1;
- }
- else {
- grid[j][i] = true;
- visited[j][i][0] = -2;
- visited[j][i][1] = -2;
- visited[j][i][2] = -2;
- visited[j][i][3] = -2;
- }
- }
- }
- bool ok = true;
- while (ok) {
- move();
- if (moment == k) {
- break;
- }
- if (visited[xKord][yKord][s] >= 0) {
- len = moment - visited[xKord][yKord][s];
- ok = false;
- }
- else {
- visited[xKord][yKord][s] = moment;
- }
- }
- if (ok) {
- xKord++;
- yKord++;
- cout << yKord << " " << xKord << endl;
- }
- else {
- int r = (k - moment) % len;
- moment = 0;
- while (moment < r) {
- move();
- }
- xKord++;
- yKord++;
- cout << yKord << " " << xKord << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement