結果

問題 No.367 ナイトの転身
ユーザー fumofumofunifumofumofuni
提出日時 2021-09-18 15:13:10
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 2,515 bytes
コンパイル時間 2,333 ms
コンパイル使用メモリ 204,416 KB
最終ジャッジ日時 2025-01-24 15:45:28
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /usr/include/c++/13/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:51,
                 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:
/usr/include/c++/13/bits/stl_algobase.h:238:15: warning: ‘gx’ may be used uninitialized [-Wmaybe-uninitialized]
  238 |       if (__b < __a)
      |           ~~~~^~~~~
main.cpp: In function ‘int main()’:
main.cpp:51:14: note: ‘gx’ was declared here
   51 |     ll sx,sy,gx,gy;
      |              ^~
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 function ‘constexpr const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = long long int]’,
    inlined from ‘int main()’ at main.cpp:89:15:
/usr/include/c++/13/bits/stl_algobase.h:238:15: warning: ‘gy’ may be used uninitialized [-Wmaybe-uninitialized]
  238 |       if (__b < __a)
      |           ~~~~^~~~~
main.cpp: In function ‘int main()’:
main.cpp:51:17: note: ‘gy’ 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