結果

問題 No.367 ナイトの転身
ユーザー sekiya9311sekiya9311
提出日時 2016-08-26 16:34:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 3,257 bytes
コンパイル時間 1,307 ms
コンパイル使用メモリ 119,088 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-25 17:02:12
合計ジャッジ時間 2,064 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 18 ms
6,944 KB
testcase_11 AC 23 ms
6,944 KB
testcase_12 AC 5 ms
6,940 KB
testcase_13 AC 6 ms
6,940 KB
testcase_14 AC 10 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 10 ms
6,940 KB
testcase_17 AC 3 ms
6,940 KB
testcase_18 AC 3 ms
6,940 KB
testcase_19 AC 4 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 4 ms
6,944 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
      |         ^~
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;
      |            ^~

ソースコード

diff #

#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;
}
0