結果

問題 No.367 ナイトの転身
ユーザー fumofumofunifumofumofuni
提出日時 2021-09-18 15:13:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 2,515 bytes
コンパイル時間 2,308 ms
コンパイル使用メモリ 211,668 KB
実行使用メモリ 7,956 KB
最終ジャッジ日時 2024-06-30 12:39:18
合計ジャッジ時間 3,890 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 40 ms
7,956 KB
testcase_11 AC 58 ms
7,948 KB
testcase_12 AC 36 ms
6,940 KB
testcase_13 AC 25 ms
6,944 KB
testcase_14 AC 32 ms
6,940 KB
testcase_15 AC 3 ms
6,940 KB
testcase_16 AC 30 ms
6,940 KB
testcase_17 AC 5 ms
6,940 KB
testcase_18 AC 7 ms
6,940 KB
testcase_19 AC 10 ms
6,940 KB
testcase_20 AC 5 ms
6,940 KB
testcase_21 AC 11 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:62:20: warning: 'sx' may be used uninitialized [-Wmaybe-uninitialized]
   62 |     dist[sx][sy][0]=0;
      |     ~~~~~~~~~~~~~~~^~
main.cpp:51:8: note: 'sx' was declared here
   51 |     ll sx,sy,gx,gy;
      |        ^~
main.cpp:62:20: warning: 'sy' may be used uninitialized [-Wmaybe-uninitialized]
   62 |     dist[sx][sy][0]=0;
      |     ~~~~~~~~~~~~~~~^~
main.cpp:51:11: note: 'sy' was declared here
   51 |     ll sx,sy,gx,gy;
      |           ^~
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/specfun.h:45,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/cmath:1935,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/x86_64-pc-linux-gnu/bits/stdc++.h:41,
                 from main.cpp:1:
In function 'constexpr const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = long long int]',
    inlined from 'int main()' at main.cpp:89:15:
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/stl_algobase.h:235:15: warning: 'gy' may be used uninitialized [-Wmaybe-uninitialized]
  235 |       if (__b < __a)
      |           ~~~~^~~~~
main.cpp: In function 'int main()':
main.cpp:51:17: note: 'gy' was declared here
   51 |     ll sx,sy,gx,gy;
      |                 ^~
In function 'constexpr const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = long long int]',
    inlined from 'int main()' at main.cpp:89:15:
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/stl_algobase.h:235:15: warning: 'gx' may be used uninitialized [-Wmaybe-uninitialized]
  235 |       if (__b < __a)
      |           ~~~~^~~~~
main.cpp: In function 'int main()':
main.cpp:51:14: note: 'gx' was declared here
   51 |     ll sx,sy,gx,gy;
      |              ^~

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
//const ll dy[9]={0,1,-1,0,1,1,-1,-1,0};
//const ll dx[9]={1,0,0,-1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
} 

int main(){
    ll h,w;cin >> h >> w;
    vector<string> g(h);
    rep(i,h)cin >> g[i];
    vl dx={1,2,2,1,-1,-2,-2,-1};
    vl dy={2,1,-1,-2,-2,-1,1,2};
    vl ddx={1,1,-1,-1};
    vl ddy={1,-1,-1,1};
    ll sx,sy,gx,gy;
    rep(i,h)rep(j,w){
        if(g[i][j]=='S'){
            sx=i,sy=j;g[i][j]='.';
        }
        if(g[i][j]=='G'){
            gx=i,gy=j;g[i][j]='.';
        }
    }
    ll dist[h][w][2];
    rep(i,h)rep(j,w)rep(k,2)dist[i][j][k]=INF;
    dist[sx][sy][0]=0;
    queue<ll> que;que.push(sx*w*2+sy*2);
    while(!que.empty()){
        ll v=que.front();que.pop();
        ll p=v%2;v/=2;
        if(p==0){
            rep(i,8){
                ll nx=v/w+dx[i],ny=v%w+dy[i];
                if(nx<0||ny<0||nx>=h||ny>=w)continue;
                ll to=p;if(g[nx][ny]=='R')to^=1;
                if(dist[nx][ny][to]!=INF)continue;
                dist[nx][ny][to]=dist[v/w][v%w][p]+1;
                que.push(nx*w*2+ny*2+to);
            }
        }
        else{
            rep(i,4){
                ll nx=v/w+ddx[i],ny=v%w+ddy[i];
                if(nx<0||ny<0||nx>=h||ny>=w)continue;
                ll to=p;if(g[nx][ny]=='R')to^=1;
                if(dist[nx][ny][to]!=INF)continue;
                dist[nx][ny][to]=dist[v/w][v%w][p]+1;
                que.push(nx*w*2+ny*2+to);
            }
        }
    }

    ll ans=min(dist[gx][gy][0],dist[gx][gy][1]);
    if(ans==INF)cout << -1 << endl;
    else cout << ans << endl;
}   
0