結果
問題 | No.1323 うしらずSwap |
ユーザー |
![]() |
提出日時 | 2020-12-20 23:23:32 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,081 bytes |
コンパイル時間 | 2,141 ms |
コンパイル使用メモリ | 187,788 KB |
実行使用メモリ | 41,472 KB |
最終ジャッジ日時 | 2024-09-21 12:23:57 |
合計ジャッジ時間 | 10,485 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 WA * 31 |
ソースコード
#include <bits/stdc++.h>using namespace std;typedef long long int ll;typedef pair<ll,ll> P;typedef vector<ll> VI;typedef vector<VI> VVI;#define REP(i,n) for(int i=0;i<n;i++)#define ALL(v) v.begin(),v.end()constexpr ll MOD=998244353;constexpr ll INF=1e18;struct xy {int x; int y; int d;bool operator<(const xy &another) const {return d > another.d;};};int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };int h, w;vector<string> s(1333);ll bfs(int sx, int sy, int gx, int gy){priority_queue<xy> q;q.push({sx,sy,0});VVI dis(h,VI(w,INF)); dis[sx][sy]=0;vector<vector<xy>> from(h,vector<xy>(w,xy{-1,-1}));vector<vector<bool>> two(h,vector<bool>(w,0));while (!q.empty()) {int x=q.top().x, y=q.top().y, d=q.top().d;q.pop();REP(k,4){int tox=x+dx[k], toy=y+dy[k];if(tox<0||tox>=h||toy<0||toy>=w)continue;if(s[tox][toy]=='#')continue;if(dis[tox][toy]>d+1){dis[tox][toy]=d+1;from[tox][toy]=xy{x,y};two[tox][toy]=two[x][y];q.push({tox,toy,d+1});}else if(dis[tox][toy]==d+1){two[tox][toy]=1;}}}if(dis[gx][gy]==INF)return -1;else if(two[gx][gy])return dis[gx][gy]*2;else{xy now={gx,gy};vector<vector<bool>> route(h,vector<bool>(w,0));while(now.x!=-1){route[now.x][now.y]=1;now=from[now.x][now.y];}REP(i,h)REP(j,w){if(route[i][j]){REP(k,4){int tox=i+dx[k], toy=j+dy[k];if(!route[tox][toy]&&s[tox][toy]=='.')return dis[gx][gy]*2+2;}}}return -1;}}int main(){int sx, sy, gx, gy;cin >> h >> w >> sx >> sy >> gx >> gy;sx--, sy--, gx--, gy--;REP(i,h) cin >> s[i];cout << bfs(sx, sy, gx, gy) << endl;return 0;}