Official

C - Belt Conveyor Editorial by en_translator


In this problem, the condition that the answer is -1 is a bit difficult.
So let us first construct an algorithm to find the answer when it is not -1.
It can be solved by the following simulation:

  • First, let \((i, j) = (1, 1)\).
  • Repeat the following procedure.
    • Let \((i_n, j_n)\) be the square to which we can move from \((i, j)\). Replace \(i\) with \(i_n\) and \(j\) with \(j_n\). If, however, there is no square that can be movable from \((i, j)\), break the loop.
  • Print \((i, j)\) as the answer.

Now let us consider how can we determine if the answer is -1.
From now on, let us order the squares by calling initial square \((1, 1)\) “the first square,” the next square “the second square,“… and so on.
If the answer equals -1, you will indefinitely repeat moving. In such case, consider the sequence of “the first square”, “the second square”, \(\ldots\), “the \((HW+1)\)-th square”. Then, since there are a total of \(HW\) squares, there always exists at least one square occurring twice in this sequence. (Informally, “you visit some square at least twice.”)

Therefore, we have proved that

  • if “the answer is -1” then “you visit some square twice or more.”

We can also prove that, conversely,

  • if “you visit some square twice or more” then “the answer is -1.”

Suppose that you visit square \((i, j)\) twice or more, and you visit there as the \(t\)-th and \((t+d)\)-th square. Then, though we omit the details, we can show that you will periodically and indefinitely visit Square \((i, j)\) as the \((t+2d)\)-th, \((t+3d)\)-th, \((t+4d)\)-th, \(\dots\) square. (If it does not ring a bell, try preparing a physical grid and moving a physical piece on it.)

Therefore, due to the two facts above, we can say that

  • “the answer is -1” if and only if “you visit some square twice or more.”

(If you are not sure with this discussion, learn set and logic theories again.)

Therefore, this problem can be solved by managing “is there a square that you visited twice or more?” while the simulation. This can be basically solved by the algorithm above, with a little tweak:

  • First, let \((i, j) = (1, 1)\).
  • Also, prepare a \(H \times W\) two-dimensional array vis to manage whether we have visited each square. The elements of vis are initialized with false.
  • Repeat the following procedure.
    • First, update the flag vis[i][j].
      • If vis[i][j] is true, it measn that you visited the same square twice, so print -1 and exit the program.
      • If vis[i][j] is false, update as vis[i][j] = true.
    • Then, make a move. Let \((i_n, j_n)\) be the square to which we can move from \((i, j)\). Replace \(i\) with \(i_n\) and \(j\) with \(j_n\). If, however, there is no square that can be movable from \((i, j)\), break the loop.
  • Print \((i, j)\) as the answer.

The computational complexity is \(\mathrm{O}(HW)\), because the loop is repeated at most \((HW+1)\) times.

  • Sample code (C++)
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> g(H);
  for (auto& s : g) cin >> s;
  vector vis(H, vector<int>(W, false));

  int i = 0, j = 0;
  while (1) {
    if (vis[i][j] == true) {
      cout << -1 << endl;
      exit(0);
    }
    vis[i][j] = true;
    if (g[i][j] == 'U' and i != 0) {
      --i;
    } else if (g[i][j] == 'D' and i != H - 1) {
      ++i;
    } else if (g[i][j] == 'L' and j != 0) {
      --j;
    } else if (g[i][j] == 'R' and j != W - 1) {
      ++j;
    } else {
      break;
    }
  }
  cout << i + 1 << " " << j + 1 << endl;
}

posted:
last update: