Official
C - Grid Walk Editorial by en_translator
While managing Takahashi’s current cell, simulate his action according to the rules.
To determine if there is an adjacent cell in some direction, one can either check whether the current cell is on the edge, or whether the cell that you move into is within the range of the grid, as in the sample code below.
Sample code
#include <bits/stdc++.h>
using namespace std;
int main() {
int h, w;
cin >> h >> w;
int si, sj;
cin >> si >> sj;
si--; sj--;
vector<vector<char>> c(h, vector<char>(w));
for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> c[i][j];
string x;
cin >> x;
for (char o : x) {
int ni = si, nj = sj;
if (o == 'L') nj--;
else if (o == 'R') nj++;
else if (o == 'U') ni--;
else ni++;
if (0 <= ni && ni < h && 0 <= nj && nj < w && c[ni][nj] == '.') {
si = ni, sj = nj;
}
}
cout << si + 1 << ' ' << sj + 1 << '\n';
}
posted:
last update: