考えたこと
- Sと同じ列、行のどこかに一つでも'#'があればそこに向かって永遠に動けばいいので-1
- これ以外のときは最初の位置から進み続けて落ちないぎりぎりの回数各方向に移動するのが上限になるはず
- この移動方法で途中で落ちるような部分列はありえない
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a<=x&&x<b)
#define MP make_pair
#define PB push_back
const int INF = (1LL<<30);
const ll LLINF = (1LL<<60);
const double PI = 3.14159265359;
const double EPS = 1e-12;
const int MOD = 1000000007;
template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
class RobotOnMoon {
public:
int longestSafeCommand(vector <string> board)
{
int h = board.size(), w = board[0].size();
int sx, sy;
REP(i, h) REP(j, w) if(board[i][j] == 'S') sx = j, sy = i;
REP(i, h) if(board[i][sx] == '#') return -1;
REP(i, w) if(board[sy][i] == '#') return -1;
return h+w-2;
}
};