結果

問題 No.2096 Rage With Our Friends
ユーザー 沙耶花沙耶花
提出日時 2022-10-07 22:30:45
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 875 ms / 5,000 ms
コード長 2,513 bytes
コンパイル時間 4,525 ms
コンパイル使用メモリ 266,700 KB
実行使用メモリ 58,096 KB
最終ジャッジ日時 2023-09-03 13:43:47
合計ジャッジ時間 11,689 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 319 ms
57,904 KB
testcase_12 AC 875 ms
58,024 KB
testcase_13 AC 197 ms
57,900 KB
testcase_14 AC 311 ms
57,880 KB
testcase_15 AC 320 ms
58,096 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 386 ms
57,892 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 719 ms
57,836 KB
testcase_22 AC 739 ms
57,948 KB
testcase_23 AC 38 ms
25,792 KB
testcase_24 AC 430 ms
38,084 KB
testcase_25 AC 295 ms
40,664 KB
testcase_26 AC 164 ms
37,556 KB
testcase_27 AC 152 ms
18,208 KB
testcase_28 AC 5 ms
8,116 KB
testcase_29 AC 77 ms
13,100 KB
権限があれば一括ダウンロードができます

ソースコード

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;
		}
	}
	
	vector pre(N+1,vector<int>(M,0));
	for(int i=1;i<N+1;i++){
		rep(j,M){
			if(i==N||s[i][j]=='#')pre[i][j] = pre[i-1][j];
			else pre[i][j] = i;
		}
	}
	
	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 = 0,yy = y-1;
				xx = pre[x+1][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 = 0,yy = y+1;
				xx = pre[x+1][yy];
				
				long long DD = D;
				if(d[xx][yy]>DD){
					d[xx][yy] = DD;
					Q.emplace(DD,make_pair(xx,yy));
				}
				
			}
			
			for(int l=-1;l<=1;l+=2){
				if(y+l<0||y+l>=M)continue;
				int tx = x+1;
				int ty = y+l;
				tx = pre[tx][ty];
				for(int k=-2;k<=2;k+=2){
					if(abs(l-k)!=1)continue;
					int xx = 0,yy = y+k;
					if(yy<0||yy>=M)continue;
					xx = tx + 1 + (max(0,(x-tx))/2);
					xx = min(xx,N);
					xx = pre[xx][yy];
					
					long long DD = D;
					if(d[xx][yy]>DD){
						d[xx][yy] = DD;
						Q.emplace(DD,make_pair(xx,yy));
					}
				}
			}
				
			for(int l=-1;l<=1;l+=2){
				if(y+l<0||y+l>=M)continue;
				int tx = x+1;
				int ty = y+l;
				tx = pre[tx][ty];
				for(int k=-2;k<=2;k+=2){
					if(abs(l-k)!=1)continue;
					int xx = 0,yy = y+k;
					if(yy<0||yy>=M)continue;
					xx = tx + 1 + (max(0,(x-tx)));
					xx = min(xx,N);
					xx = pre[xx][yy];
					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