結果

問題 No.1572 XI
ユーザー umezoumezo
提出日時 2021-06-27 14:24:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,416 bytes
コンパイル時間 2,321 ms
コンパイル使用メモリ 215,308 KB
実行使用メモリ 445,284 KB
最終ジャッジ日時 2023-09-07 17:50:28
合計ジャッジ時間 46,503 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 388 ms
94,424 KB
testcase_05 AC 928 ms
216,064 KB
testcase_06 AC 1,110 ms
260,152 KB
testcase_07 AC 24 ms
9,656 KB
testcase_08 AC 942 ms
223,576 KB
testcase_09 AC 412 ms
106,488 KB
testcase_10 AC 727 ms
174,028 KB
testcase_11 AC 53 ms
16,440 KB
testcase_12 AC 300 ms
76,784 KB
testcase_13 AC 65 ms
20,392 KB
testcase_14 AC 310 ms
80,232 KB
testcase_15 AC 65 ms
19,628 KB
testcase_16 AC 245 ms
65,440 KB
testcase_17 AC 18 ms
8,096 KB
testcase_18 AC 366 ms
95,656 KB
testcase_19 AC 984 ms
233,148 KB
testcase_20 AC 609 ms
149,816 KB
testcase_21 AC 1,370 ms
270,864 KB
testcase_22 AC 900 ms
210,604 KB
testcase_23 AC 15 ms
7,076 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,384 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
testcase_37 TLE -
testcase_38 TLE -
testcase_39 TLE -
testcase_40 TLE -
testcase_41 TLE -
testcase_42 TLE -
testcase_43 TLE -
testcase_44 TLE -
testcase_45 TLE -
testcase_46 TLE -
testcase_47 AC 1,259 ms
444,228 KB
testcase_48 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

const ll INF=1LL<<60;

struct Edge{
  int to;
  ll w;
  Edge(int to,ll w) : to(to),w(w) {}
};

using Graph=vector<vector<Edge>>;
using pli=pair<ll,int>;

template<class T> bool chmin(T& a,T b){
  if(a>b){
    a=b;
    return true;
  }
  return false;
}

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int h,w,sy,sx,gy,gx;
  cin>>h>>w>>sy>>sx>>gy>>gx;
  sy--,sx--,gy--,gx--;
  vector<string> A(h);
  rep(i,h) cin>>A[i];
  
  Graph G(6*h*w);
  
  rep(i,h){
    rep(j,w){
      if(A[i][j]=='#') continue;
      int t=i*w+j;
      if(j!=w-1 && A[i][j+1]=='.'){
        G[t].push_back(Edge(t+1+h*w,1));
        G[t+h*w].push_back(Edge(t+1+5*h*w,1));
        G[t+2*h*w].push_back(Edge(t+1+2*h*w,1));
        G[t+3*h*w].push_back(Edge(t+1,1));
        G[t+4*h*w].push_back(Edge(t+1+4*h*w,1));
        G[t+5*h*w].push_back(Edge(t+1+3*h*w,1));
        
        G[t+1+h*w].push_back(Edge(t,1));
        G[t+1+5*h*w].push_back(Edge(t+h*w,1));
        G[t+1+2*h*w].push_back(Edge(t+2*h*w,1));
        G[t+1].push_back(Edge(t+3*h*w,1));
        G[t+1+4*h*w].push_back(Edge(t+4*h*w,1));
        G[t+1+3*h*w].push_back(Edge(t+5*h*w,1));
      }
      if(i!=h-1 && A[i+1][j]=='.'){
        G[t].push_back(Edge(t+w+4*h*w,1));
        G[t+h*w].push_back(Edge(t+w+h*w,1));
        G[t+2*h*w].push_back(Edge(t+w,1));
        G[t+3*h*w].push_back(Edge(t+w+3*h*w,1));
        G[t+4*h*w].push_back(Edge(t+w+5*h*w,1));
        G[t+5*h*w].push_back(Edge(t+w+2*h*w,1));
        
        G[t+w+4*h*w].push_back(Edge(t,1));
        G[t+w+h*w].push_back(Edge(t+h*w,1));
        G[t+w].push_back(Edge(t+2*h*w,1));
        G[t+w+3*h*w].push_back(Edge(t+3*h*w,1));
        G[t+w+5*h*w].push_back(Edge(t+4*h*w,1));
        G[t+w+2*h*w].push_back(Edge(t+5*h*w,1));
      }
    }
  }

  vector<ll> dist(6*h*w,INF);
  int s=sy*w+sx;
  dist[s]=0;
  
  priority_queue<pli,vector<pli>,greater<pli>> que;
  que.push({dist[s],s});
  
  while(!que.empty()){
    int v=que.top().second;
    ll d=que.top().first;
    que.pop();
    
    if(d>dist[v]) continue;
    
    for(auto e:G[v]){
      if(chmin(dist[e.to],dist[v]+e.w)){
        que.push({dist[e.to],e.to});
      }
    }
  }
  
  if(dist[gy*w+gx]==INF) cout<<-1<<endl;
  else cout<<dist[gy*w+gx]<<endl;
  
  return 0;
}
0