結果
問題 | No.367 ナイトの転身 |
ユーザー |
![]() |
提出日時 | 2016-04-29 23:21:48 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 20 ms / 2,000 ms |
コード長 | 2,599 bytes |
コンパイル時間 | 1,089 ms |
コンパイル使用メモリ | 123,780 KB |
実行使用メモリ | 5,888 KB |
最終ジャッジ日時 | 2024-10-11 00:17:36 |
合計ジャッジ時間 | 1,974 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 27 |
ソースコード
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <complex> #include <queue> #include <deque> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <iomanip> #include <assert.h> #include <array> #include <cstdio> #include <cstring> #include <random> #include <functional> #include <numeric> #include <bitset> using namespace std; struct before_main{before_main(){cin.tie(0); ios::sync_with_stdio(false);}} before_main; #define REP(i,a,b) for(int i=a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) #define all(c) (c).begin(), (c).end() #define zero(a) memset(a, 0, sizeof a) #define minus(a) memset(a, -1, sizeof a) template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); } template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); } template<class T> constexpr bool in_range(T y, T x, T H, T W) { return 0<=y&&y<H&&0<=x&&x<W; } typedef long long ll; int const inf = 1<<29; int dxb[4] = {-1,+1,-1,+1}; int dyb[4] = {-1,-1,+1,+1}; int dxk[8] = {-2,-1,+1,+2,+2,+1,-1,-2}; int dyk[8] = {-1,-2,-2,-1,+1,+2,+2,+1}; int main() { int H, W; cin >> H >> W; vector<string> G(H); rep(i, H) { cin >> G[i]; } int sy, sx; rep(i, H) rep(j, W) { if(G[i][j] == 'S') { sy = i, sx = j; } } queue<tuple<int, int, bool>> q; static int dist[505][505][2]; minus(dist); dist[sy][sx][0] = 0; q.emplace(sy, sx, 0); while(!q.empty()) { int y, x; bool mb; tie(y, x, mb) = q.front(); q.pop(); if(G[y][x] == 'G') { cout << dist[y][x][mb] << endl; return 0; } if(mb) { rep(i, 4) { int ny = y + dyb[i], nx = x + dxb[i]; if(!in_range(ny, nx, H, W)) { continue; } if(G[ny][nx] == 'R') { if(dist[ny][nx][0] >= 0) { continue; } dist[ny][nx][0] = dist[y][x][mb] + 1; q.emplace(ny, nx, 0); } else { if(dist[ny][nx][1] >= 0) { continue; } dist[ny][nx][1] = dist[y][x][mb] + 1; q.emplace(ny, nx, 1); } } } else { rep(i, 8) { int ny = y + dyk[i], nx = x + dxk[i]; if(!in_range(ny, nx, H, W)) { continue; } if(G[ny][nx] == 'R') { if(dist[ny][nx][1] >= 0) { continue; } dist[ny][nx][1] = dist[y][x][mb] + 1; q.emplace(ny, nx, 1); } else { if(dist[ny][nx][0] >= 0) { continue; } dist[ny][nx][0] = dist[y][x][mb] + 1; q.emplace(ny, nx, 0); } } } } cout << -1 << endl; return 0; }