結果

問題 No.1323 うしらずSwap
ユーザー 沙耶花沙耶花
提出日時 2020-12-20 00:31:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,872 bytes
コンパイル時間 2,324 ms
コンパイル使用メモリ 218,384 KB
実行使用メモリ 102,616 KB
最終ジャッジ日時 2023-10-21 09:52:52
合計ジャッジ時間 9,939 ms
ジャッジサーバーID
(参考情報)
judge13 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
testcase_13 AC 2 ms
4,348 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 91 ms
19,976 KB
testcase_17 AC 103 ms
19,712 KB
testcase_18 AC 80 ms
19,976 KB
testcase_19 AC 108 ms
19,976 KB
testcase_20 AC 89 ms
19,976 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 155 ms
102,616 KB
testcase_27 AC 151 ms
102,604 KB
testcase_28 AC 96 ms
19,976 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 196 ms
19,712 KB
testcase_34 AC 175 ms
19,712 KB
testcase_35 AC 168 ms
19,712 KB
testcase_36 AC 161 ms
19,976 KB
testcase_37 AC 159 ms
20,180 KB
testcase_38 AC 147 ms
19,712 KB
testcase_39 AC 157 ms
19,712 KB
testcase_40 AC 161 ms
19,712 KB
testcase_41 AC 169 ms
19,976 KB
testcase_42 AC 144 ms
19,712 KB
testcase_43 AC 71 ms
19,976 KB
testcase_44 AC 84 ms
19,712 KB
testcase_45 AC 87 ms
19,976 KB
testcase_46 AC 77 ms
19,712 KB
testcase_47 AC 73 ms
19,976 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 <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++;
			}
		}
		
		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){
							cout<<2*D+2<<endl;
							return 0;
						}
					}
					
				}
			}
		}
		
		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);
				}
			}
		}
		cout<<-1<<endl;
		/*
		auto ans = G.flow(2*(r0*W+c0)+1,2*(r1*W+c1),2);
		if(ans.first<2)cout<<-1<<endl;
		else cout<<ans.second<<endl;
		*/
		
	}
	
    return 0;
}
0