結果

問題 No.1572 XI
ユーザー umezo
提出日時 2021-06-27 16:10:54
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,366 bytes
コンパイル時間 8,685 ms
コンパイル使用メモリ 277,880 KB
実行使用メモリ 329,160 KB
最終ジャッジ日時 2025-02-21 04:28:34
合計ジャッジ時間 44,820 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 43 TLE * 2
権限があれば一括ダウンロードができます

ソースコード

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);
  
  int g1=h*w,g2=2*h*w,g3=3*h*w,g4=4*h*w,g5=5*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+g1,1));
        G[t+g1].push_back(Edge(t+1+g5,1));
        G[t+g2].push_back(Edge(t+1+g2,1));
        G[t+g3].push_back(Edge(t+1,1));
        G[t+g4].push_back(Edge(t+1+g4,1));
        G[t+g5].push_back(Edge(t+1+g3,1));
        
        G[t+1+g1].push_back(Edge(t,1));
        G[t+1+g5].push_back(Edge(t+g1,1));
        G[t+1+g2].push_back(Edge(t+g2,1));
        G[t+1].push_back(Edge(t+g3,1));
        G[t+1+g4].push_back(Edge(t+g4,1));
        G[t+1+g3].push_back(Edge(t+g5,1));
      }
      if(i!=h-1 && A[i+1][j]=='.'){
        G[t].push_back(Edge(t+w+g4,1));
        G[t+g1].push_back(Edge(t+w+g1,1));
        G[t+g2].push_back(Edge(t+w,1));
        G[t+g3].push_back(Edge(t+w+g3,1));
        G[t+g4].push_back(Edge(t+w+g5,1));
        G[t+g5].push_back(Edge(t+w+g2,1));
        
        G[t+w+g4].push_back(Edge(t,1));
        G[t+w+g1].push_back(Edge(t+g1,1));
        G[t+w].push_back(Edge(t+g2,1));
        G[t+w+g3].push_back(Edge(t+g3,1));
        G[t+w+g5].push_back(Edge(t+g4,1));
        G[t+w+g2].push_back(Edge(t+g5,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]) 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