#include <string>
#include <vector>
#include <algorithm>
using namespace std;
#define UP 0
#define RIGHT 1
#define DOWN 2
#define LEFT 3
int g_cache[4][500][500][4] = {};
int g_visited[4][500][500] = {};
int g_ans[4] = {};
int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};
int W, H;
int rotate(int dir, char cmd) {
if (cmd == 'L') {
if (dir == UP) dir = LEFT;
else --dir;
} else if (cmd == 'R') {
if (dir == LEFT) dir = UP;
else ++dir;
}
return dir;
}
bool move(int &x, int &y, int &dir, char cmd, int idx) {
dir = rotate(dir, cmd);
if (g_cache[idx][y][x][dir]) return false;
g_cache[idx][y][x][dir] = 1;
x += dx[dir]; y += dy[dir];
if (x == -1) x = W - 1;
else if (x == W) x = 0;
else if (y == -1) y = H - 1;
else if (y == H) y = 0;
return true;
}
vector<int> solution(vector<string> grid) {
vector<int> answer;
int i, x, y, ans, cnt, dir, j;
W = grid[0].size(); H = grid.size();
for (i = 0; i < 4; ++i) {
// init
x = y = ans = 0;
cnt = 1;
dir = i;
g_visited[i][y][x] = 1;
// logic
while (move(x, y, dir, grid[y][x], i)) {
if (!g_visited[i][y][x]) {
++cnt;
g_visited[i][y][x] = 1;
}
++ans;
}
// exception
if (x != 0 || y != 0 || cnt != W * H || dir != rotate(i, grid[0][0]) || ans == 0) continue;
for (j = 0; j < i; ++j) {
if (g_cache[j][0][0][dir] && ans == g_ans[j]) break;
}
if (j != i) continue;
// answer
g_ans[i] = ans;
answer.push_back(ans);
}
sort(answer.begin(), answer.end());
return answer;
}
다 뒤질래 진짜
게이야 4차원배열은 너무한거아니노