結果

問題 No.2096 Rage With Our Friends
ユーザー 沙耶花沙耶花
提出日時 2022-10-07 22:13:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,986 bytes
コンパイル時間 4,612 ms
コンパイル使用メモリ 265,384 KB
実行使用メモリ 108,356 KB
最終ジャッジ日時 2023-09-03 13:01:49
合計ジャッジ時間 13,184 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 453 ms
42,512 KB
testcase_12 AC 1,244 ms
108,356 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 4000000000000000001

long long d[2005][2005];

int main(){
	
	int N,M;
	cin>>N>>M;
	
	int sx,sy,gx,gy;
	cin>>sx>>sy>>gx>>gy;
	sx--,sy--,gx--,gy--;
	
	vector<string> s(N);
	rep(i,N)cin>>s[i];
	
	rep(i,N){
		rep(j,M){
			d[i][j] = Inf64;
		}
	}
	using P = pair<long long,pair<int,int>>;
	priority_queue<P,vector<P>,greater<P>> Q;
	Q.emplace(0,make_pair(sx,sy));
	d[sx][sy] = 0;
	
	while(Q.size()>0){
		long long D = Q.top().first;
		int x = Q.top().second.first;
		int y = Q.top().second.second;
		Q.pop();
		if(D!=d[x][y])continue;
		
		if(s[x][y]=='.'){
			if(y!=0){
				int xx = x+1,yy = y-1;
				while(xx>=N || s[xx][yy]=='#')xx--;
				if(s[xx][yy]=='.'){
					long long DD = D;
					if(d[xx][yy]>DD){
						d[xx][yy] = DD;
						Q.emplace(DD,make_pair(xx,yy));
					}
				}
			}
			if(y!=M-1){
				int xx = x+1,yy = y+1;
				while(xx>=N || s[xx][yy]=='#')xx--;
				if(s[xx][yy]=='.'){
					long long DD = D;
					if(d[xx][yy]>DD){
						d[xx][yy] = DD;
						Q.emplace(DD,make_pair(xx,yy));
					}
				}
			}
			
			for(int k=-2;k<=2;k+=2){
				int xx = 1 + x/2,yy = y+k;
				if(y<0||y>=M)continue;
				
				long long DD = D;
				if(d[xx][yy]>DD){
					d[xx][yy] = DD;
					Q.emplace(DD,make_pair(xx,yy));
				}
				
			}
			for(int k=-2;k<=2;k+=2){
				int xx = min(N-1,1 + x),yy = y+k;
				if(y<0||y>=M)continue;
				
				long long DD = D+1;
				if(d[xx][yy]>DD){
					d[xx][yy] = DD;
					Q.emplace(DD,make_pair(xx,yy));
				}
				
			}
		}
		if(x!=0){
			int xx = x-1,yy = y;
			long long DD = D;
			if(d[xx][yy]>DD){
				d[xx][yy] = DD;
				Q.emplace(DD,make_pair(xx,yy));
			}
		}
		
	}
	/*
	rep(i,N){
		rep(j,M){
			cout<<d[i][j]<<',';
		}
		cout<<endl;
	}
	*/
	long long ans = d[gx][gy];
	if(ans==Inf64)ans = -1;
	cout<<ans<<endl;
	
	return 0;
}
0