結果

問題 No.1323 うしらずSwap
ユーザー 沙耶花沙耶花
提出日時 2020-12-20 00:33:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,894 bytes
コンパイル時間 4,330 ms
コンパイル使用メモリ 235,076 KB
実行使用メモリ 322,860 KB
最終ジャッジ日時 2023-10-21 09:55:26
合計ジャッジ時間 19,585 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 2 ms
4,348 KB
testcase_11 WA -
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 439 ms
322,836 KB
testcase_17 AC 853 ms
322,276 KB
testcase_18 AC 428 ms
322,860 KB
testcase_19 AC 824 ms
322,304 KB
testcase_20 AC 424 ms
322,572 KB
testcase_21 AC 426 ms
322,396 KB
testcase_22 AC 828 ms
322,572 KB
testcase_23 AC 439 ms
322,296 KB
testcase_24 AC 835 ms
322,404 KB
testcase_25 AC 426 ms
322,296 KB
testcase_26 AC 398 ms
322,596 KB
testcase_27 AC 409 ms
322,848 KB
testcase_28 AC 503 ms
322,584 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 193 ms
19,720 KB
testcase_34 AC 182 ms
19,720 KB
testcase_35 AC 176 ms
19,720 KB
testcase_36 AC 169 ms
19,984 KB
testcase_37 AC 168 ms
20,188 KB
testcase_38 AC 155 ms
19,720 KB
testcase_39 AC 158 ms
19,720 KB
testcase_40 AC 165 ms
19,720 KB
testcase_41 AC 180 ms
19,984 KB
testcase_42 AC 145 ms
19,720 KB
testcase_43 AC 217 ms
166,044 KB
testcase_44 AC 257 ms
192,312 KB
testcase_45 AC 381 ms
236,004 KB
testcase_46 AC 371 ms
192,312 KB
testcase_47 AC 282 ms
192,616 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 AC 3 ms
4,420 KB
testcase_60 AC 3 ms
4,348 KB
testcase_61 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/mincostflow>
using namespace atcoder;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000

int H,W;
vector<string> S;
vector<int> dy = {0,0,1,-1},dx = {1,-1,0,0};
vector<vector<int>> get(int r,int c){
	queue<pair<int,int>> Q;
	vector<vector<int>> dis(H,vector<int>(W,Inf));
	dis[r][c] = 0;
	Q.emplace(r,c);
	
	
	while(Q.size()>0){
		int y = Q.front().first,x = Q.front().second;
		Q.pop();
		
		rep(i,4){
			int yy = y+dy[i],xx = x+dx[i];
			if(yy<0||yy>=H||xx<0||xx>=W)continue;
			if(S[yy][xx]=='#')continue;
			if(dis[yy][xx]!=Inf)continue;
			dis[yy][xx] = dis[y][x]+1;
			Q.emplace(yy,xx);
		}
	}
	
	return dis;
}

int main(){
	
	cin>>H>>W;
	S.resize(H);
	int r0,c0,r1,c1;
	cin>>r0>>c0>>r1>>c1;
	r0--,c0--,r1--,c1--;
	rep(i,H){
		cin>>S[i];
	}
	
	auto d0 = get(r0,c0),d1 = get(r1,c1);
	
	if(d0[r1][c1]==Inf)cout<<-1<<endl;
	else{
		int D = d0[r1][c1];
		int cnt = 0;
		rep(i,H){
			rep(j,W){
				if(d0[i][j] + d1[i][j]==D)cnt++;
			}
		}
		int ans = Inf;
		if(cnt>D+1){
			cout<<2*D<<endl;
			return 0;
		}
		else{
			rep(i,H){
				rep(j,W){
					if(d0[i][j]==Inf)continue;
					if(d0[i][j]+d1[i][j]==D)continue;
					rep(k,4){
						int ii = i+dy[k],jj = j+dx[k];
						if(d0[ii][jj]+d1[ii][jj]==D && d0[ii][jj]!=0 && d1[ii][jj]!=0){
							ans = 2*D+2;//cout<<2*D+2<<endl;
						}
					}
					
				}
			}
		}
		
		mcf_graph<int,int> G(2*H*W);
		rep(i,H){
			rep(j,W){
				if(S[i][j]=='#')continue;
				G.add_edge(2*(i*W+j),2*(i*W+j)+1,1,0);
				rep(k,4){
					int ii = i,jj = j;
					ii += dy[k],jj += dx[k];
					if(S[ii][jj]=='#')continue;
					
					G.add_edge(2*(i*W+j)+1,2*(ii*W+jj),1,1);
				}
			}
		}

		auto ret = G.flow(2*(r0*W+c0)+1,2*(r1*W+c1),2);
		if(ret.first>=2)ans = min(ans,ret.second);
		
		if(ans!=Inf)cout<<ans<<endl;
		else cout<<-1<<endl;
		
	}
	
    return 0;
}
0