結果

問題 No.1572 XI
ユーザー umezoumezo
提出日時 2021-06-27 15:58:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,431 bytes
コンパイル時間 2,324 ms
コンパイル使用メモリ 214,904 KB
実行使用メモリ 329,196 KB
最終ジャッジ日時 2023-09-07 18:00:52
合計ジャッジ時間 40,063 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 335 ms
70,464 KB
testcase_05 AC 802 ms
160,244 KB
testcase_06 AC 962 ms
192,644 KB
testcase_07 AC 19 ms
8,076 KB
testcase_08 AC 823 ms
165,468 KB
testcase_09 AC 356 ms
79,380 KB
testcase_10 AC 615 ms
129,124 KB
testcase_11 AC 39 ms
13,084 KB
testcase_12 AC 254 ms
57,488 KB
testcase_13 AC 52 ms
15,584 KB
testcase_14 AC 264 ms
60,040 KB
testcase_15 AC 52 ms
15,144 KB
testcase_16 AC 204 ms
48,892 KB
testcase_17 AC 15 ms
6,772 KB
testcase_18 AC 315 ms
71,468 KB
testcase_19 AC 844 ms
172,520 KB
testcase_20 AC 528 ms
111,204 KB
testcase_21 AC 1,169 ms
200,240 KB
testcase_22 AC 778 ms
155,856 KB
testcase_23 AC 12 ms
6,080 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1,851 ms
298,996 KB
testcase_35 AC 1,780 ms
295,716 KB
testcase_36 AC 1,803 ms
303,360 KB
testcase_37 AC 1,948 ms
315,008 KB
testcase_38 AC 1,853 ms
309,688 KB
testcase_39 AC 1,927 ms
312,360 KB
testcase_40 AC 1,759 ms
287,884 KB
testcase_41 AC 1,860 ms
302,312 KB
testcase_42 AC 1,852 ms
307,268 KB
testcase_43 AC 1,818 ms
302,240 KB
testcase_44 TLE -
testcase_45 TLE -
testcase_46 AC 1,974 ms
328,868 KB
testcase_47 AC 994 ms
328,556 KB
testcase_48 AC 1,948 ms
329,060 KB
権限があれば一括ダウンロードができます

ソースコード

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 int INF=1e9+7;

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

using Graph=vector<vector<Edge>>;
using pli=pair<int,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<int> 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;
    int d=que.top().first;
    que.pop();
    
    if(d>dist[v] || d>6*h*w) 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