結果

問題 No.1323 うしらずSwap
ユーザー 👑 Nachia
提出日時 2020-12-20 18:44:45
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,600 bytes
コンパイル時間 2,110 ms
コンパイル使用メモリ 199,236 KB
最終ジャッジ日時 2025-01-17 04:53:55
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30 WA * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using LL=long long;
using ULL=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

int H,W;
int Ay,Ax,By,Bx;
bool block[3<<9][3<<9];
int D[3<<9][3<<9][2];
int P[3<<9][3<<9][2];
int dx[4][2]={{-1,0},{0,-1},{1,0},{0,1}};

struct QueueNode{ int x,y,t; };

int main(){
	cin>>H>>W>>Ay>>Ax>>By>>Bx; Ay--; Ax--; By--; Bx--;
	rep(y,H) rep(x,W){ char c; cin>>c; block[x][y]=(c=='#'); }

	int ans=1000000000;
	
	rep(y,H) rep(x,W) rep(t,2){ D[x][y][t]=-1; P[x][y][t]=-1; }
	queue<QueueNode> Q;
	D[Ax][Ay][0]=0;
	Q.push({Ax,Ay,0});
	while(Q.size()){
		auto q = Q.front(); Q.pop();
		rep(i,4){
			auto nx=q; nx.x+=dx[i][0]; nx.y+=dx[i][1];
			if(block[nx.x][nx.y]) continue;
			while(nx.t<2) if(D[nx.x][nx.y][nx.t]!=-1) nx.t++; else break;
			if(nx.t==2) continue;
			if(P[q.x][q.y][q.t]==(i^2)) continue;
			D[nx.x][nx.y][nx.t]=D[q.x][q.y][q.t]+1;
			P[nx.x][nx.y][nx.t]=i;
			Q.push(nx);
		}
	}
	if(D[Bx][By][1]==-1){ cout<<-1<<endl; return 0; }
	ans=min(ans,D[Bx][By][1]*2);

	rep(y,H) rep(x,W) rep(t,2){ D[x][y][t]=-1; P[x][y][t]=-1; }
	D[Ax][Ay][0]=0;
	Q.push({Ax,Ay,0});
	while(Q.size()){
		auto q = Q.front(); Q.pop();
		if(q.x!=Bx || q.y!=By) rep(i,4){
			auto nx=q; nx.x+=dx[i][0]; nx.y+=dx[i][1];
			if(block[nx.x][nx.y]) continue;
			while(nx.t<2) if(D[nx.x][nx.y][nx.t]!=-1) nx.t++; else break;
			if(nx.t==2) continue;
			if(P[q.x][q.y][q.t]==(i^2)) continue;
			D[nx.x][nx.y][nx.t]=D[q.x][q.y][q.t]+1;
			P[nx.x][nx.y][nx.t]=i;
			Q.push(nx);
		}
	}

	if(D[Bx][By][1]!=-1) ans=min(ans,D[Bx][By][0]+D[Bx][By][1]);
	cout<<ans<<endl;
	return 0;
}
0