結果
| 問題 |
No.367 ナイトの転身
|
| コンテスト | |
| ユーザー |
koyopro
|
| 提出日時 | 2020-03-28 00:40:22 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 72 ms / 2,000 ms |
| コード長 | 2,874 bytes |
| コンパイル時間 | 1,854 ms |
| コンパイル使用メモリ | 165,272 KB |
| 実行使用メモリ | 10,824 KB |
| 最終ジャッジ日時 | 2025-01-02 10:13:42 |
| 合計ジャッジ時間 | 3,375 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
ソースコード
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define REP1(i, n) for(int i=1; i<=(n); i++)
#define RREP1(i, n) for(int i=(n); i>=1; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
int dxy[] = {1,2, 2,1, -1,-2, -2,-1, 1,2};
int dxy2[] = {1,1,-1,-1,1,1};
void solve();
signed main()
{
#if DEBUG
std::ifstream in("input.txt");
std::cin.rdbuf(in.rdbuf());
#endif
cin.tie(0);
ios::sync_with_stdio(false);
solve();
return 0;
}
/*================================*/
#if DEBUG
#define SIZE 4
#else
#define SIZE 555
#endif
int H,W;
int MAP[SIZE][SIZE];
int START=1,GOAL=2,RED=3;
struct P {
int h,w,t;
P(int h, int w, int t):h(h),w(w),t(t){};
bool operator < (const P& p) const {
return p.t > t;
}
};
typedef pair<int, P> Q;
priority_queue<Q, vector<Q>, greater<Q>> que;
int DP[2][SIZE][SIZE];
int N=0,B=1;
P *goal;
void solve() {
cin>>H>>W;
string s;
REP(i,2)REP(h,H)REP(w,W)DP[i][h][w]=INT_MAX;
REP(h,H) {
cin>>s;
REP(w,W) {
if (s[w]=='S') {
MAP[h][w]=START;
DP[N][h][w]=0;
que.push(Q(0,P(h,w,N)));
}
if (s[w]=='G') {
MAP[h][w]=GOAL;
goal = new P(h,w,0);
}
if (s[w]=='R') MAP[h][w]=RED;
}
}
while(que.size()) {
auto item = que.top();
que.pop();
auto p = item.second;
if (DP[p.t][p.h][p.w] < item.first) continue;
int t = p.t;
if (MAP[p.h][p.w]==RED) t = !t;
if (t==N) {
REP(i,8) {
int nh = p.h + dxy[i];
int nw = p.w + dxy[i+2];
int nc = item.first+1;
if (nh<0||nw<0||H<=nh||W<=nw) continue;
if (DP[t][nh][nw] > nc) {
DP[t][nh][nw] = nc;
que.push(Q(nc, P(nh,nw,t)));
}
}
}
if (t==B) {
REP(i,4) {
int nh = p.h + dxy2[i];
int nw = p.w + dxy2[i+1];
int nc = item.first+1;
if (nh<0||nw<0||H<=nh||W<=nw) continue;
if (DP[t][nh][nw] > nc) {
DP[t][nh][nw] = nc;
que.push(Q(nc, P(nh,nw,t)));
}
}
}
}
int ans = min(DP[N][goal->h][goal->w], DP[B][goal->h][goal->w]);
if (ans == INT_MAX) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
}
koyopro