結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
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 86 ms
19,948 KB
testcase_17 AC 115 ms
19,684 KB
testcase_18 AC 85 ms
19,948 KB
testcase_19 AC 113 ms
19,948 KB
testcase_20 AC 87 ms
19,948 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 104 ms
19,948 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 203 ms
19,684 KB
testcase_34 AC 184 ms
19,684 KB
testcase_35 AC 178 ms
19,684 KB
testcase_36 AC 173 ms
19,948 KB
testcase_37 AC 173 ms
20,160 KB
testcase_38 AC 162 ms
19,684 KB
testcase_39 AC 160 ms
19,684 KB
testcase_40 AC 173 ms
19,684 KB
testcase_41 AC 181 ms
19,948 KB
testcase_42 AC 155 ms
19,684 KB
testcase_43 AC 73 ms
19,948 KB
testcase_44 AC 91 ms
19,684 KB
testcase_45 AC 89 ms
19,948 KB
testcase_46 AC 94 ms
19,684 KB
testcase_47 AC 78 ms
19,948 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 2 ms
4,348 KB
testcase_60 AC 2 ms
4,348 KB
testcase_61 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
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]+d1[i][j] == D+2){
						cout<<2*D+2<<endl;
						return 0;
					}
				}
			}
		}
		cout<<-1<<endl;
	}
	
    return 0;
}
0