結果

問題 No.1572 XI
ユーザー chocorusk
提出日時 2021-08-15 03:18:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 349 ms / 2,000 ms
コード長 1,815 bytes
コンパイル時間 3,932 ms
コンパイル使用メモリ 186,864 KB
最終ジャッジ日時 2025-01-23 22:22:27
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
using mint=modint998244353;
int h, w;
int sy, sx, gy, gx;
string s[1010];
int d[6][1010][1010];
int main()
{
    cin>>h>>w;
    cin>>sy>>sx;
    cin>>gy>>gx;
    sy--; sx--; gy--; gx--;
    for(int i=0; i<h; i++){
        cin>>s[i];
    }
    const int INF=1e9;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            for(int k=0; k<6; k++){
                d[k][i][j]=INF;
            }
        }
    }
    d[0][sy][sx]=0;
    using Pi=pair<P, int>;
    queue<Pi> que;
    que.push({{sy, sx}, 0});
    int dy[4]={1, -1, 0, 0}, dx[4]={0, 0, 1, -1};
    vector<vector<int>> v{{2,3,4,5},{3,2,5,4},{1,0,2,2},{0,1,3,3},{4,4,1,0},{5,5,0,1}};
    while(!que.empty()){
        auto p=que.front(); que.pop();
        int t=p.second;
        int y=p.first.first, x=p.first.second;
        for(int k=0; k<4; k++){
            int y1=y+dy[k], x1=x+dx[k];
            if(y1<0 || y1>=h || x1<0 || x1>=w || s[y1][x1]=='#') continue;
            int t1=v[t][k];
            if(d[t1][y1][x1]>d[t][y][x]+1){
                d[t1][y1][x1]=d[t][y][x]+1;
                que.push({{y1, x1}, t1});
            }
        }
    }
    if(d[0][gy][gx]==INF) cout<<-1<<endl;
    else cout<<d[0][gy][gx]<<endl;
    return 0;
}
0