結果
問題 | No.1572 XI |
ユーザー |
👑 ![]() |
提出日時 | 2021-06-27 13:45:04 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 252 ms / 2,000 ms |
コード長 | 2,158 bytes |
コンパイル時間 | 2,458 ms |
コンパイル使用メモリ | 210,472 KB |
最終ジャッジ日時 | 2025-01-22 14:06:24 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 45 |
ソースコード
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; using std::cout; using std::cin; using std::endl; using ll=long long; using ld=long double; ll I=1167167167167167167; ll Q=1e9+7; #define rep(i,a) for (ll i=0;i<a;i++) template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>; template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();} template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();} template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;} template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;} template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());} template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});} template<class T> void print_tate(vector<T> &v) {rep(i,v.size()) cout<<v[i]<<"\n";} void yneos(bool a){if(a) cout<<"Yes"<<"\n"; else cout<<"No"<<"\n";} //おちこんだりもしたけれど、私はげんきです。 int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int H,W; cin>>H>>W; int s1,s2,g1,g2; cin>>s1>>s2>>g1>>g2; s1--,s2--,g1--,g2--; int s=s1*W+s2,g=g1*W+g2; int p[H][W]; int dp[H][W][6]; rep(i,H) rep(j,W){ char a; cin>>a; if(a=='.') p[i][j]=1; else p[i][j]=0; rep(k,6) dp[i][j][k]=Q; } dp[s1][s2][0]=0; queue<tuple<int,int,int>> q; q.push({s1,s2,0}); vector<vector<tuple<int,int,int>>> diff={ {{1,0,1},{-1,0,3},{0,1,4},{0,-1,2}}, {{1,0,5},{-1,0,0},{0,1,1},{0,-1,1}}, {{1,0,2},{-1,0,2},{0,1,0},{0,-1,5}}, {{1,0,0},{-1,0,5},{0,1,3},{0,-1,3}}, {{1,0,4},{-1,0,4},{0,1,5},{0,-1,0}}, {{1,0,3},{-1,0,1},{0,1,2},{0,-1,4}} }; while (!q.empty()) { int x,y,t; tie(x,y,t)=q.front(); int L=dp[x][y][t]; L++; q.pop(); rep(i,4){ int a,b,c; tie(a,b,c)=diff[t][i]; int u=a+x,v=b+y; if(u==-1||u==H||v==-1||v==W||p[u][v]==0) continue; if(dp[u][v][c]==Q){ dp[u][v][c]=L; q.push({u,v,c}); } } } /*rep(i,H){ rep(j,W) cout<<dp[i][j][0]<<" "; cout<<endl; }*/ if(dp[g1][g2][0]==Q) cout<<"-1\n"; else cout<<dp[g1][g2][0]<<"\n"; }