結果

問題 No.1323 うしらずSwap
ユーザー 👑 kmjpkmjp
提出日時 2020-12-20 00:28:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,450 bytes
コンパイル時間 4,620 ms
コンパイル使用メモリ 214,340 KB
実行使用メモリ 65,664 KB
最終ジャッジ日時 2023-10-21 09:51:24
合計ジャッジ時間 10,418 ms
ジャッジサーバーID
(参考情報)
judge9 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,800 KB
testcase_01 AC 3 ms
7,800 KB
testcase_02 AC 3 ms
7,804 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 3 ms
7,804 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 3 ms
7,800 KB
testcase_14 AC 3 ms
7,804 KB
testcase_15 WA -
testcase_16 AC 95 ms
41,228 KB
testcase_17 AC 135 ms
41,216 KB
testcase_18 AC 94 ms
40,884 KB
testcase_19 AC 141 ms
41,252 KB
testcase_20 AC 94 ms
41,208 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 142 ms
52,772 KB
testcase_27 AC 212 ms
65,664 KB
testcase_28 AC 94 ms
41,116 KB
testcase_29 AC 75 ms
40,824 KB
testcase_30 AC 104 ms
41,000 KB
testcase_31 AC 75 ms
40,816 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 258 ms
41,064 KB
testcase_37 AC 256 ms
41,040 KB
testcase_38 AC 243 ms
41,032 KB
testcase_39 AC 258 ms
41,064 KB
testcase_40 AC 247 ms
41,028 KB
testcase_41 AC 247 ms
41,048 KB
testcase_42 AC 240 ms
41,048 KB
testcase_43 AC 52 ms
41,112 KB
testcase_44 AC 65 ms
41,096 KB
testcase_45 AC 72 ms
41,156 KB
testcase_46 AC 77 ms
41,116 KB
testcase_47 AC 56 ms
41,096 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int H,W;
int SR,SC,GR,GC;
string S[2020];

int D[3][2020][2020];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>H>>W>>SR>>SC>>GR>>GC;
	SR--,SC--,GR--,GC--;
	FOR(y,H) cin>>S[y];
	FOR(y,H) FOR(x,W) D[0][y][x]=D[1][y][x]=D[2][y][x]=1<<30;
	D[0][SR][SC]=D[1][GR][GC]=0;
	queue<int> Q;
	Q.push(SR*10000+SC);
	Q.push(100000000+GR*10000+GC);
	int dd[4]={1,0,-1,0};
	while(Q.size()) {
		int id=Q.front()/100000000;
		int cy=Q.front()/10000%10000;
		int cx=Q.front()%10000;
		Q.pop();
		FOR(i,4) {
			int ty=cy+dd[i];
			int tx=cx+dd[i^1];
			if(ty<0||ty>=H||tx<0||tx>=W) continue;
			if(S[ty][tx]=='#') continue;
			if(chmin(D[id][ty][tx],D[id][cy][cx]+1)) {
				Q.push(id*100000000+ty*10000+tx);
			}
		}
	}
	int d=D[0][GR][GC];
	int ma=1<<30;
	map<int,int> M;
	FOR(y,H) FOR(x,W) if(S[y][x]=='.') {
		
		int num=0;
		FOR(i,4) {
			int ty=y+dd[i];
			int tx=x+dd[i^1];
			if(ty<0||ty>=H||tx<0||tx>=W) continue;
			if(S[ty][tx]=='.') num++;
		}
		if(D[0][y][x]+D[1][y][x]==d) {
			M[D[0][y][x]]++;
			if(M[D[0][y][x]]==2) ma=min(ma,d*2);
			if(num>=3) ma=min(ma,(D[0][y][x]+D[1][y][x]+1)*2);
		}
		if(num>=3) ma=min(ma,(D[0][y][x]+D[1][y][x]+2)*2);
	}
	FOR(y,H) FOR(x,W) if(D[0][y][x]+D[1][y][x]==d && D[0][y][x]&&D[1][y][x]) S[y][x]='#';
	D[2][SR][SC]=0;
	Q.push(SR*10000+SC);
	while(Q.size()) {
		int cy=Q.front()/10000%10000;
		int cx=Q.front()%10000;
		Q.pop();
		FOR(i,4) {
			int ty=cy+dd[i];
			int tx=cx+dd[i^1];
			if(ty<0||ty>=H||tx<0||tx>=W) continue;
			if(S[ty][tx]=='#') continue;
			if(chmin(D[2][ty][tx],D[2][cy][cx]+1)) {
				Q.push(ty*10000+tx);
			}
		}
	}
	ma=min(ma,d+D[2][GR][GC]);
	
	if(ma==1<<30) ma=-1;
	cout<<ma<<endl;
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0