#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int solution(vector<string> board) {
queue <pair<int, int>> tmp;
int visit[100][100];
fill(&visit[0][0], &visit[100][0], -1);
for(int i=0; i<board.size(); i++) {
for(int j=0; j<board[0].size(); j++) {
if(board[i][j] == 'R') {
tmp.push({i, j});
visit[i][j] = 0;
}
}
}
while(!tmp.empty()) {
int x = tmp.front().first;
int y = tmp.front().second;
tmp.pop();
for(int i=0; i<4; i++) {
int nx = x + dir[i][0];
int ny = y + dir[i][1];
while(1) {
if(nx < 0 || nx >= board.size() || ny < 0 || ny >= board[0].size()) break;
if(board[nx][ny] == 'D') break;
nx += dir[i][0];
ny += dir[i][1];
}
nx -= dir[i][0];
ny -= dir[i][1];
if(board[nx][ny] == 'G') return visit[x][y] + 1;
if(visit[nx][ny] > -1) continue;
visit[nx][ny] = visit[x][y] + 1;
tmp.push({nx, ny});
}
}
return -1;
}