結果
| 問題 | 
                            No.1572 XI
                             | 
                    
| コンテスト | |
| ユーザー | 
                             umezo
                         | 
                    
| 提出日時 | 2021-06-27 15:58:13 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                            (最新)
                                AC
                                 
                             
                            (最初)
                            
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 2,431 bytes | 
| コンパイル時間 | 4,644 ms | 
| コンパイル使用メモリ | 208,024 KB | 
| 最終ジャッジ日時 | 2025-01-22 14:39:41 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 27 TLE * 18 | 
ソースコード
#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;
}
            
            
            
        
            
umezo