結果

問題 No.1323 うしらずSwap
ユーザー 👑 kmjpkmjp
提出日時 2020-12-20 00:16:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,985 bytes
コンパイル時間 2,588 ms
コンパイル使用メモリ 215,236 KB
実行使用メモリ 53,404 KB
最終ジャッジ日時 2023-10-21 09:47:24
合計ジャッジ時間 8,947 ms
ジャッジサーバーID
(参考情報)
judge11 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,756 KB
testcase_01 AC 3 ms
5,756 KB
testcase_02 AC 2 ms
5,760 KB
testcase_03 AC 3 ms
5,756 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 3 ms
5,760 KB
testcase_11 AC 2 ms
5,764 KB
testcase_12 WA -
testcase_13 AC 2 ms
5,756 KB
testcase_14 AC 2 ms
5,760 KB
testcase_15 AC 2 ms
5,756 KB
testcase_16 AC 67 ms
28,944 KB
testcase_17 AC 102 ms
28,932 KB
testcase_18 AC 66 ms
28,600 KB
testcase_19 AC 104 ms
28,968 KB
testcase_20 AC 66 ms
28,924 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 124 ms
40,488 KB
testcase_27 AC 199 ms
53,404 KB
testcase_28 AC 84 ms
28,832 KB
testcase_29 AC 66 ms
28,540 KB
testcase_30 AC 96 ms
28,716 KB
testcase_31 AC 65 ms
28,532 KB
testcase_32 AC 95 ms
28,612 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 187 ms
28,780 KB
testcase_37 AC 182 ms
28,752 KB
testcase_38 AC 174 ms
28,748 KB
testcase_39 AC 194 ms
28,776 KB
testcase_40 AC 181 ms
28,740 KB
testcase_41 AC 179 ms
28,756 KB
testcase_42 AC 174 ms
28,764 KB
testcase_43 AC 44 ms
28,828 KB
testcase_44 AC 58 ms
28,812 KB
testcase_45 AC 64 ms
28,872 KB
testcase_46 AC 70 ms
28,832 KB
testcase_47 AC 49 ms
28,812 KB
testcase_48 AC 45 ms
28,532 KB
testcase_49 AC 65 ms
28,724 KB
testcase_50 AC 39 ms
28,728 KB
testcase_51 AC 19 ms
28,724 KB
testcase_52 AC 28 ms
28,616 KB
testcase_53 AC 63 ms
28,732 KB
testcase_54 AC 28 ms
28,536 KB
testcase_55 AC 66 ms
28,736 KB
testcase_56 AC 24 ms
28,536 KB
testcase_57 AC 79 ms
28,744 KB
testcase_58 AC 3 ms
5,780 KB
testcase_59 AC 8 ms
26,564 KB
testcase_60 AC 3 ms
5,780 KB
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[2][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]=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);
	}
	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