結果
| 問題 |
No.367 ナイトの転身
|
| コンテスト | |
| ユーザー |
sekiya9311
|
| 提出日時 | 2016-08-26 16:34:29 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 24 ms / 2,000 ms |
| コード長 | 3,257 bytes |
| コンパイル時間 | 1,345 ms |
| コンパイル使用メモリ | 120,208 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-08 04:12:05 |
| 合計ジャッジ時間 | 2,161 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
コンパイルメッセージ
In constructor 'State::State(int, int, int, bool)',
inlined from 'int main()' at main.cpp:88:12:
main.cpp:69:16: warning: 'sW' may be used uninitialized [-Wmaybe-uninitialized]
69 | this->W=w;
| ~~~~~~~^~
main.cpp: In function 'int main()':
main.cpp:80:12: note: 'sW' was declared here
80 | int sH,sW;
| ^~
In constructor 'State::State(int, int, int, bool)',
inlined from 'int main()' at main.cpp:88:12:
main.cpp:68:16: warning: 'sH' may be used uninitialized [-Wmaybe-uninitialized]
68 | this->H=h;
| ~~~~~~~^~
main.cpp: In function 'int main()':
main.cpp:80:9: note: 'sH' was declared here
80 | int sH,sW;
| ^~
ソースコード
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>
#define REP(i,n) for(int (i)=0;(i)<(n);(i)++)
#define FOR(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define RREP(i,a) for(int (i)=(a)-1;(i)>=0;(i)--)
#define FORR(i,a,b) for(int (i)=(a)-1;(i)>=(b);(i)--)
#define PI acos(-1.0)
#define DEBUG(C) cout<<C<<endl;
#define VI vector<int>
#define VII vector<VI>
#define VL vector<LL>
#define VLL vector<VL>
#define VD vector<double>
#define VDD vector<VD>
#define PII pair<int,int>
#define PDD pair<double,double>
#define PLL pair<LL,LL>
#define VPII vector<PII>
#define ALL(a) (a).begin(),(a).end()
#define SORT(a) sort(ALL(a))
#define REVERSE(a) reverse(ALL(a))
#define MP make_pair
#define FORE(a,b) for(auto &&a:b)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF=1e9;
const int MOD=INF+7;
int nW[] = { -2, -2, -1, -1, 1, 1, 2, 2 };
int nH[] = { -1, 1, -2, 2, -2, 2, -1, 1 };
int bW[] = { -1, -1, 1, 1 };
int bH[] = { -1, 1, -1, 1 };
struct State{
int H;
int W;
int step;
bool knightFlag;
State(int h,int w,int step,bool f){
this->H=h;
this->W=w;
this->step=step;
this->knightFlag=f;
}
};
int main(void){
int H,W;
cin >> H >> W;
vector<string> S(H);
vector<vector<vector<bool>>> is(2,vector<vector<bool>>(H,vector<bool>(W,false)));
int sH,sW;
REP(i,H){
cin >> S[i];
REP(j,W){
if(S[i][j]=='S') sH=i,sW=j;
}
}
queue<State> q;
q.push(State(sH,sW,0,true));
while(!q.empty()){
State s=q.front();
q.pop();
bool f=s.knightFlag;
int nowStep=s.step+1;
if(f){
REP(i,8){
int h=s.H+nH[i],w=s.W+nW[i];
if(h<0 || h>=H) continue;
if(w<0 || w>=W) continue;
if(is[1][h][w]) continue;
is[1][h][w]=true;
if(S[h][w]=='G'){
cout << nowStep << endl;
return 0;
}
if(S[h][w]=='R'){
q.push(State(h,w,nowStep,!f));
}else{
q.push(State(h,w,nowStep,f));
}
}
}else{
REP(i,4){
int h=s.H+bH[i],w=s.W+bW[i];
if(h<0 || h>=H) continue;
if(w<0 || w>=W) continue;
if(is[0][h][w]) continue;
is[0][h][w]=true;
if(S[h][w]=='G'){
cout << nowStep << endl;
return 0;
}
if(S[h][w]=='R'){
q.push(State(h,w,nowStep,!f));
}else{
q.push(State(h,w,nowStep,f));
}
}
}
}
cout << -1 << endl;
}
sekiya9311