제출 #1251364

#제출 시각아이디문제언어결과실행 시간메모리
1251364yf_yusuf로봇 (APIO13_robots)C++20
10 / 100
139 ms20044 KiB
//```//    YF YUSUF
#define Free_open(File) freopen((File".in"),"r",stdin);freopen((File".out"),"w",stdout);
#define YF ios_base::sync_with_stdio(0);cout.setf(ios::fixed);
#define YUSUF cout.precision(0);cout.tie(0);cin.tie(0);
// #include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <unordered_map>
#include <cmath>
#include <numeric>
#include <queue>
#include <stack>
#include <cassert>
#include <climits>
#include <string>
#include <cstdlib>
using namespace std;
#ifdef YF_CHECK
	bool Output=1;
//	#include "g/debug.h"
//  #include "g/bigint.h"
//	#include "g/text.h"
#else
	#pragma GCC optimize ("unroll-loops")
	#pragma GCC optimize ("inline")
	#pragma GCC optimize ("Ofast")
	#pragma GCC optimize ("O3")
	bool Output=0;
	#define deb(x...) 42 
#endif
#ifdef DEV
	bool LOUT=1;
#else
	bool LOUT=0;
#endif
using ll   = long long;
using ld   = long double;
using sll  = set <ll>;
using vll  = vector <ll>;
using mll  = map <ll,ll>;
using pll  = pair <ll,ll>;
using vvl  = vector <vll>;
using vpll = vector <pll>;
template<class T>T MIN(T&a,T b){a=min(a,b);return a;}
template<class T>T MAX(T&a,T b){a=max(a,b);return a;}
#define revers(a) reverse(all(a))
#define all(a) a.begin(),a.end()
#define sortt(a) sort(all(a))
#define sgr v+v+1,(tl+tr)/2+1,tr
#define sgl v+v,tl,(tl+tr)/2
#define pb push_back
#define ins insert
#define S second
#define F first
ll BP(ll a,ll b,ll mod=1e9+7){
	if(b==0)return 1;
	ll q=BP(a,b/2,mod);
	return ((q*q)%mod*(b%2?a:1ll))%mod;
}
ll f(ll x){return x*(x+1)/2;}
ll dup(ll a,ll b){return (a+b-1)/b;}
ll lcm(ll a,ll b){return a/__gcd(a,b)*b;}
ll invf(ll x){return (-1+sqrt(1+8*x))/2;}
const ll  mod=998244353;
const ll  INF=1e18;
const ll  inf=1e9+7;
const ll  N  =1e6+7;
ll n, w, h;
pll nx[22][22][8];
char m[22][22];
vpll d = {{-1, 0}, // UP
					{ 0,-1}, // LEFT
					{ 1, 0}, // DOWN
					{ 0, 1}};// RIGHT
map<vll,bool>mp;
pll NX(ll x,ll y,ll dir){
	// cout<<x<<" "<<y<<" "<<X<<" "<<Y<<" "<<dir<<" F\n";
	{
		auto [X, Y] = d[dir];
		vll v = {x,y,X,Y};
		if(mp[v]){
			return {-1, -1};
		}
		mp[v] = 1;
	}
	if(x > w || x < 1 || y > h || y < 1 || m[x][y]=='x' || m[x][y]=='#')return {0,0};
	if(m[x][y]=='A')dir++;
	if(m[x][y]=='C')dir--;
	dir = (dir+4)%4;
	auto [X, Y] = d[dir];
	// cout<<x<<" "<<y<<" "<<X<<" "<<Y<<" "<<dir<<" "<<m[x][y]<<" S\n";
	auto now = NX(x+X,y+Y,dir);
	if(now.F == 0)return {x,y};
	return now;
}
vvl get(pll st){
	ll A = st.F, B = st.S;
	queue<pll>q;
	vvl d(w+1,vll(h+1,inf));
	d[A][B] = 0;
	q.push(st);
	while(q.size()){
		auto [x,y] = q.front();
		q.pop();
		for(int j=0;j<4;j++){
			auto [X, Y] = nx[x][y][j];
			if(X == -1)continue;
			if(d[X][Y] > d[x][y] + 1){
				d[X][Y] = d[x][y] + 1;
				q.push({X, Y});
			}
		}
	}
	// for(int i=1;i<=w;i++){
		// for(int j=1;j<=h;j++){
			// // cout<<d[i][j]<<" ";
			// if(d[i][j]==inf)cout<<"M ";
			// else cout<<d[i][j]<<" ";
		// }
		// cout<<"\n";
	// }
	// cout<<"\n";
	return d;
}
void YF_MAIN(ll TEST){
	cin>>n>>w>>h;
	pll A, B;
	for(int i=1;i<=w;i++){
		for(int j=1;j<=h;j++){
			cin>>m[i][j];
			if(m[i][j]=='1')A = {i, j};
			else if(m[i][j]=='2')B={i,j};
		}
	}
	for(int i=1;i<=w;i++){
		for(int j=1;j<=h;j++){
			mp.clear();
			nx[i][j][0] = NX(i, j, 0);// UP
			mp.clear();
			nx[i][j][1] = NX(i, j, 1);// LEFT
			mp.clear();
			nx[i][j][2] = NX(i, j, 2);// DOWN
			mp.clear();
			nx[i][j][3] = NX(i, j, 3);// RIGHT
			mp.clear();
		}
	}
	// for(int k=0;k<4;k++){
		// for(int i=1;i<=w;i++){
			// for(int j=1;j<=h;j++){
				// if(nx[i][j][k].F != -1)cout<<nx[i][j][k].F<<","<<nx[i][j][k].S<<" ";
				// else cout<<"M,M ";
			// }
			// cout<<"\n";
		// }
		// cout<<"\n";
	// }
	auto AA = get(A);
	auto BB = get(B);
	ll ans = inf;
	for(int i=1;i<=w;i++){
		for(int j=1;j<=h;j++){
			MIN(ans, AA[i][j] + BB[i][j]);
		}
	}
	cout<<(ans == inf ? -1 : ans);
}
/*
1,1 1,2 1,3 1,4 
1,1 1,2 1,3 1,4 
1,1 1,2 1,3 1,4 
1,1 1,2 1,3 1,4 

1,0 1,0 1,0 1,0 
2,0 2,0 2,0 2,0 
3,0 3,0 3,0 3,0 
4,0 4,0 4,0 4,0 

1,4 1,4 1,4 1,4 
2,4 2,4 2,4 2,4 
3,4 3,4 3,4 3,4 
4,4 4,4 4,4 4,4 

4,1 4,2 4,3 4,4 
4,1 4,2 4,3 4,4 
4,1 4,2 4,3 4,4 
4,1 4,2 4,3 4,4 
*/
bool TECT=0;
bool CASE=0;
bool OUTP=1;
const ll LN=1e1 + 7; ll lg[LN];
const ll SN=2e1 + 7; ll SM[SN];
const ll FN=2e6 + 7; 
ll FACT[FN],inv[FN],FMOD=inf;
ll PER(ll n,ll k){return FACT[n] *inv[n-k]%FMOD;}
ll CNK(ll n,ll k){return PER(n,k)*inv[k  ]%FMOD;}
void BEFORE(){
	for(ll i=2;i<SN;i++){
		if(SM[i])continue;
		for(ll j=i;j<SN;j+=i)
			MAX(SM[j],i);
	}
	for(int i=2;i<LN;i++) lg[i]=lg[i/2]+1;
	FACT[0]=inv[0]=1;
	for(int i=1;i<FN;i++){
		FACT[i]=FACT[i-1]*i%FMOD;
		if(i<5e5+7)inv[i]=BP(FACT[i],FMOD-2,FMOD);
	}
}
signed main(){
	if(OUTP){YF YUSUF}
	// freopen(("input.txt"),"r",stdin);freopen(("output.txt"),"w",stdout);
	// Free_open("TEST");
	cout<<(LOUT && Output ? "\nYF_OUTPUT:\n\n" : "");
	int TEST=1;
	if(TECT)
		cin>>TEST;
	BEFORE();
	for(int T=1;T<=TEST;T++){
		if(CASE)
			cout<<"Case "<<T<<": ";
		YF_MAIN(T);
		cout<<(T==TEST ? "" : "\n");
	}
	return 0;
} 
//```
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...