結果

問題 No.367 ナイトの転身
ユーザー maimai
提出日時 2017-08-22 22:55:42
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 2,563 bytes
コンパイル時間 1,922 ms
コンパイル使用メモリ 195,348 KB
実行使用メモリ 6,144 KB
最終ジャッジ日時 2024-04-23 04:49:30
合計ジャッジ時間 2,869 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 14 ms
6,144 KB
testcase_11 AC 18 ms
6,144 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 9 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 8 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:57:18: warning: 'sy' may be used uninitialized [-Wmaybe-uninitialized]
   57 |     dp[sx][sy][0]=1;
      |     ~~~~~~~~~~~~~^~
main.cpp:36:15: note: 'sy' was declared here
   36 |     int sx=-1,sy;
      |               ^~

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define debugv(v) printf("L%d %s => ",__LINE__,#v);for(auto e:v){cout<<e<<" ";}cout<<endl;
#define debugm(m) printf("L%d %s is..\n",__LINE__,#m);for(auto v:m){for(auto e:v){cout<<e<<" ";}cout<<endl;}
#define debuga(m,w) printf("L%d %s is => ",__LINE__,#m);for(int x=0;x<(w);x++){cout<<(m)[x]<<" ";}cout<<endl;
#define debugaa(m,w,h) printf("L%d %s is..\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){cout<<(m)[x][y]<<" ";}cout<<endl;}
#define ALL(v) (v).begin(),(v).end()
#define BIGINT 0x7FFFFFFF
#define E107 1000000007

template<typename T1,typename T2>
ostream& operator <<(ostream &o,const pair<T1,T2> p){o<<"("<<p.first<<":"<<p.second<<")";return o;}

#define qmax 10000
int _qu[qmax][4];
int _quf=0,_qub=0;

int w,h;
char maze[505][505];
int dp[505][505][2];

int movePatternX[2][9]={{-2,-2,-1,-1, 1, 1, 2, 2,0},{-1,-1, 1, 1, 0, 0, 0, 0,0}};
int movePatternY[2][9]={{-1, 1,-2, 2,-2, 2,-1, 1,0},{-1, 1,-1, 1, 0, 0, 0, 0,0}};

inline bool inmap(int x,int y){
    return 0<=x && x<w && 0<=y && y<h;
}

int main(){
    int i,j,k,l;
    int m,n;
    
    int sx=-1,sy;
    
    cin>>h>>w;cin.ignore();
    
    for (i=0;i<h;i++){
        scanf("%s",maze[i]);
        if (sx<0)
            for (j=0;j<w;j++){
                if (maze[i][j]=='S'){
                    sx=j;sy=i;
                    break;
                }
            }
    }
    
    //queue<vector<int>> q;
    //q.push(vector<int>{0,sx,sy,0});
    _qu[_quf][1]=sx;
    _qu[_quf][2]=sy;
    _quf++;
    
    dp[sx][sy][0]=1;
    
    int x,y,cnt,job,tx,ty;int ddd=0;
    while (_qub!=_quf){
        //const vector<int> &qfront = q.front();
        //cnt=qfront[0]+1;x=qfront[1];y=qfront[2];job=qfront[3];
        //q.pop();
        cnt=_qu[_qub][0]+1;x=_qu[_qub][1];y=_qu[_qub][2];job=_qu[_qub][3];
        _qub=(++_qub)>=qmax ? 0 : _qub;
        
        if (maze[y][x]=='G'){
            cout<<(cnt-1)<<endl;
            return 0;
        }
        if (maze[y][x]=='R') job=!job;
        
        for (i=0;movePatternX[job][i]!=0;i++){
            tx=x+movePatternX[job][i];
            ty=y+movePatternY[job][i];
            if (inmap(tx,ty) && !dp[tx][ty][job]){
                dp[tx][ty][job]=1;
                //q.push(vector<int>{cnt,tx,ty,job});
                _qu[_quf][0]=cnt;
                _qu[_quf][1]=tx;
                _qu[_quf][2]=ty;
                _qu[_quf][3]=job;
                _quf=(++_quf)>=qmax ? 0 : _quf;
            }
        }
    }

    cout<<-1<<endl;

    return 0;
}
0