結果

問題 No.3121 Prime Dance
ユーザー shobonvip
提出日時 2025-04-20 02:32:32
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,951 ms / 2,000 ms
コード長 4,535 bytes
コンパイル時間 6,679 ms
コンパイル使用メモリ 299,176 KB
実行使用メモリ 177,608 KB
最終ジャッジ日時 2025-04-20 02:32:51
合計ジャッジ時間 15,176 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
/**
	author:  shobonvip
	created: 2025.04.20 01:49:37
**/

#include<bits/stdc++.h>
using namespace std;

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)
#define all(v) v.begin(), v.end()

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	int h, w;
	cin >> h >> w;
	int sx, sy, gx, gy;
	cin >> sx >> sy >> gx >> gy; 
	sx--; sy--; gx--; gy--;

	vector<vector<char>> c(h, vector<char>(w));
	rep(i,0,h) rep(j,0,w) {
		cin >> c[i][j];
	}

	if (sx > gx) {
		vector<vector<char>> nc(h, vector<char>(w));
		rep(i,0,h) {
			rep(j,0,w) {
				nc[i][j] = c[h-1-i][j];
			}
		}
		swap(c, nc);
		sx = h-1-sx;
		gx = h-1-gx;
	}

	if (sy > gy) {
		vector<vector<char>> nc(h, vector<char>(w));
		rep(i,0,h) {
			rep(j,0,w) {
				nc[i][j] = c[i][w-1-j];
			}
		}
		swap(c, nc);
		sy = w-1-sy;
		gy = w-1-gy;
	}

	int geta_h = gx - sx;
	int geta_w = gy - sy;

	static int d[1600][1620][4];
	static bool seen[1600][1620][4];
	rep(i,0,1600)rep(k,0,1620)rep(s,0,4){
		d[i][k][s]=1e9;
	}

	d[sx*w+sy][0][0] = 0;
	deque<tuple<int,int,int>> mada;
	mada.push_back(make_tuple(sx*w+sy, 0, 0));
	while(!mada.empty()){
		
		auto [val, a, state] = mada.front();
		mada.pop_front();
		if(seen[val][a][state]) continue;
		seen[val][a][state] = 1;
		int i = val/w;
		int j = val%w;
		vector<int> cango;
		for (auto [x, y]:{
			pair(i-1, j),
			pair(i+1, j),
			pair(i, j-1),
			pair(i, j+1)
		}) {
			if (!(0<=x&&x<h)) continue;
			if (!(0<=y&&y<w)) continue;
			if (c[x][y]=='#') continue;
			if (x != i) cango.push_back(1);
			if (y != j) cango.push_back(2);
			if (j-y > 0) {
				if (chmin(d[x*w+y][a][state], d[val][a][state] + 1)) {
					mada.push_back(make_tuple(x*w+y,a,state));
				}
			}else if(y-j > 0) {
				if (chmin(d[x*w+y][a][state], d[val][a][state])) {
					mada.push_front(make_tuple(x*w+y,a,state));
				}
			}else if (i-x > 0) {
				if (a+1 >= h*w+10) continue;
				if (chmin(d[x*w+y][a+1][state], d[val][a][state])) {
					mada.push_front(make_tuple(x*w+y,a+1,state));
				}
			}else if (x-i > 0) {
				if (chmin(d[x*w+y][a][state], d[val][a][state])) {
					mada.push_front(make_tuple(x*w+y,a,state));
				}

			}
		}
		for (int news: cango) {
			if (news == 1) {
				if (a+1 >= h*w+10) continue;
				if (chmin(d[val][a+1][state|news], d[val][a][state])) {
					mada.push_front(make_tuple(val,a+1,state|news));
				}
			}else {
				if (chmin(d[val][a][state|news], d[val][a][state] + 1)) {
					mada.push_back(make_tuple(val,a,state|news));
				}

			}
		}
	}

	const int mx = 2e6;
	vector<bool> p(mx,true);
	p[0] = false;
	p[1] = false;
	for (int i=2; i<mx; i++) {
		if (!p[i]) continue;
		for (int j=2*i; j<mx; j+=i) {
			p[j] = false;
		}
	}

	vector<bool> hs((int)1e6);
	vector<bool> ws((int)1e6);
	for (int i=0; i<(int)1e6; i++) {
		if (p[i] && p[i+geta_h]) hs[i] = 1;
		if (p[i] && p[i+geta_w]) ws[i] = 1;
	}

	vector<int> ht((int)1e6, 1e9);
	vector<int> wt((int)1e6, 1e9);

	for (int i=(int)1e6-1; i>=0; i--){
		if (hs[i]) ht[i]=0;
		if (ws[i]) wt[i]=0;
	}

	for (int i=(int)1e6-2; i>=0; i--) {
		chmin(ht[i], ht[i+1] + 1);
		chmin(wt[i], wt[i+1] + 1);
	}

	ll ans = 1e9;
	for (int a=0; a<h*w+10; a++) {
		for (int state=0; state<4; state++) {
			int b = d[gx*w+gy][a][state];
			if (b > (int)1e8) continue;
			//cout << a << ' ' << b << ' ' << state << endl;
			if ((state & 1) == 0) if (ht[a] != 0) continue;
			if ((state & 2) == 0) if (wt[b] != 0) continue;
			chmin(ans, ll(ht[a] + wt[b] + a + b) * 2 + geta_h + geta_w);
		}
	}

	if (ans > 1e8) {
		cout << -1 << '\n';
	}else {
		cout << ans << '\n';
	}

}

0