跳到正文

题解

题解:P14065 [PO Final 2022] 对弈 / Laserschack

Big_Dinosaur约 8 分钟
原文
文章信息与版本

文章信息

发布
2025年9月23日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 8 分钟
题目
P14065

版本

  1. v1当前

二分+BFS。

首先预处理每一个格子第一次被迷雾覆盖的时间。一次 BFS 即可。

首先,答案具有单调性,若 xx 可行,容易发现一条可行路线(不会被迷雾阻挡)在 x−1x-1 时也一定不会有迷雾阻挡,可以攻击到国王。

也就是说,可以二分。

二分的 check 也使用 BFS。与前面不同之处,每个点被拆成两个点,代表竖直的光和水平的光。代码中使用了 BFS。DFS 貌似也可行。

具体如何 BFS,就是对光线进行发展,到镜子拓展一下方向。

AC Code

写的有点点丑。有些 assert 是调试时加的,可以忽略。

C++
#include<bits/stdc++.h>
#define int long long
#define ri register int
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
using namespace std;
namespace IO{
	char ibuf[(1<<20)+1],*iS,*iT;
	#if ONLINE_JUDGE
		#define gh() (iS==iT?iT=(iS=ibuf)+fread(ibuf,1,(1<<20)+1,stdin),(iS==iT?EOF:*iS++):*iS++)
	#else
		#define gh() getchar()
	#endif
	inline int r(){
		int o=1,p=0;
		char q=gh();
		while(q<'0'||q>'9'){if(q=='-')o=-1;q=gh();}
		while(q>='0'&&q<='9'){p=(p<<1)+(p<<3)+(q^48);q=gh();}
		return o*p;
	}
	inline char gc(){char q=gh();while(q<=' ')q=gh();return q;}
	inline string gs(){string s="";char g=gh();while(g<=' ')g=gh();while(g>' '){s+=g;g=gh();}return s;}
	inline void Wi(int E){
		if(E==0)return;if(E<0){E=-E;putchar('-');}
		Wi(E/10);putchar((E%10)^48);
	}
	inline void wln(int J){if(J==0)putchar('0');else Wi(J);putchar('\n');}
	inline void w(int J){if(J==0)putchar('0');else Wi(J);}
	inline void ww(int J){if(J==0)putchar('0');else Wi(J);putchar(' ');}
}
const int N=4e4,inf=1e18;
using namespace IO;
namespace D1n0{
	int n,m;
	vector<vector<int> >a(N),dis(N);
	vector<pii>X;
	vector<int>Empty_vector;
	pii st,en;
	inline int to(char ch){
		if(ch=='.')return 0;
		if(ch=='o')return 1;
		if(ch=='R')return 2;
		if(ch=='A')return 3;
		if(ch=='K')return 4;
	}
	inline bool chk(int x){
		vector<vector<int> >d[2];
		d[0].pb(Empty_vector);d[1].pb(Empty_vector);
		for(ri i=1;i<=n;++i){
			d[0].pb(Empty_vector);d[1].pb(Empty_vector);
			for(ri j=0;j<=m;++j)d[0][i].pb(inf),d[1][i].pb(inf);
		}
		queue<array<int,3> >q;
		d[0][st.fi][st.se]=x;
		d[1][st.fi][st.se]=x;
		q.push({0,st.fi,st.se});
		q.push({1,st.fi,st.se});
		auto upd=[&](array<int,3>x,int D){
			if(d[x[0]][x[1]][x[2]]!=inf)return;
			d[x[0]][x[1]][x[2]]=D;
			q.push(x);
		};
		auto ins=[&](array<int,3>X,int d){
			if(X[1]>n||X[1]<1||X[2]>m||X[2]<1)return;
			if(dis[X[1]][X[2]]<=x)return;
			if(a[X[1]][X[2]]==1){
				upd(X,d);
				X[0]^=1;
				upd(X,d);
			}
			else upd(X,d);
		};
		while(!q.empty()){
			array<int,3>nw=q.front(),nx;q.pop();
			int s=d[nw[0]][nw[1]][nw[2]];
			if(nw[0]==0){
				nx=nw;
				++nx[1];
				ins(nx,s+1);
				nx[1]-=2;
				ins(nx,s+1);
			}
			else{
				nx=nw;
				++nx[2];
				ins(nx,s+1);
				nx[2]-=2;
				ins(nx,s+1);
			}
		}
		if(d[0][en.fi][en.se]==inf&&d[1][en.fi][en.se]==inf)return 0;
		return 1;
	}
	inline void pre(){
		queue<pii>p;
		for(auto it:X)dis[it.fi][it.se]=0,p.push(it);
		while(!p.empty()){
			auto it=p.front();p.pop();
			int d=dis[it.fi][it.se];
			auto to=it;
			++to.fi;
			if(to.fi<=n&&dis[to.fi][to.se]==inf){
				dis[to.fi][to.se]=d+1;
				p.push(to);
			}
			to=it;
			--to.fi;
			if(to.fi&&dis[to.fi][to.se]==inf){
				dis[to.fi][to.se]=d+1;
				p.push(to);
			}
			to=it;
			++to.se;
			if(to.se<=m&&dis[to.fi][to.se]==inf){
				dis[to.fi][to.se]=d+1;
				p.push(to);
			}
			to=it;
			--to.se;
			if(to.se&&dis[to.fi][to.se]==inf){
				dis[to.fi][to.se]=d+1;
				p.push(to);
			}
		}
	}
	inline void _(){
		n=r(),m=r();
		st=mp(-1,-1);
		en=mp(-1,-1);
		for(ri i=1;i<=n;++i){
			string s=gs();
			a[i].pb(-1);dis[i].pb(inf);
			for(ri j=0;j<m;++j){
				int x=to(s[j]);
				if(x==2)X.pb({i,j+1});
				if(x==3)st=mp(i,j+1);
				if(x==4)en=mp(i,j+1);
				dis[i].pb(inf);
				a[i].pb(x);
			}
		}
		assert(st.fi!=-1&&en.fi!=-1);
		pre();
		int mid,L=0,R=dis[st.fi][st.se]-1,p=0;
		while(L<=R){
			mid=L+R>>1;
			if(chk(mid))L=mid+1,p=1;
			else R=mid-1;
		}
		assert(p);
		w(R+1);
	}
}
signed main(){
	D1n0::_();
}
原文