跳到正文

题解

你可以拿着这个去自首吗?

coding_goat约 10 分钟
原文
文章信息与版本

文章信息

收录
2026年4月29日
更新
2026年10月28日
篇幅
约 10 分钟
题目
CF1898F

版本

  1. v2当前
  2. v1

做完题之后的感想:

思维远小于码力的题,很不喜欢但是还是做了。

考虑三种情况何时会出现:

  • 第一种是找不到出口,答案为 n×m−1n\times m-1,即除了自己在的位置其他全部堵死。

  • 第二种为只有一个出口,那么答案就是 n×mn\times m 减去当前位置到出口的最短路。

  • 第三种情况呢就相当于 n×mn\times m 减去找到两个出口使得当前位置到两个出口的路径并的最小值。

前面两种都是好找的,第三种呢?

我们意识到路径并是先走到一个最近的相遇的点然后在一起走过去。那么我们先对于当前位置跑 bfs 跑出每个位置到当前位置的最短距离,然后我们对于所有在边界上的点跑多源 bfs,求到所有点的最短路和起点与最短路不同的次短路。所以路径并相当于起点到这里的最短距离加上两个边界点到这里的最短路和次短路。

所以我们就做完了,只有最大值和次大值的话根据 bfs 的性质每个点只会被进队访问两次所以时间复杂度是 O(nm)O(nm) 的。

代码见下:

C++
#include<bits/stdc++.h>

#define int ll
#define pii pair<int,int>
#define pll pair<long long,long long>
#define ll long long
#define i128 __int128

#define mem(a,b) memset((a),(b),sizeof(a))
#define m0(a) memset((a),0,sizeof(a))
#define m1(a) memset(a,-1,sizeof(a))
#define lb(x) ((x)&-(x))
#define lc(x) ((x)<<1)
#define rc(x) (((x)<<1)|1)
#define pb(G,x) (G).push_back((x))
#define For(a,b,c) for(int a=(b);a<=(c);a++)
#define Rep(a,b,c) for(int a=(b);a>=(c);a--)
#define in1(a) a=read()
#define in2(a,b) a=read(), b=read()
#define in3(a,b,c) a=read(), b=read(), c=read()
#define in4(a,b,c,d) a=read(), b=read(), c=read(), d=read()
#define fst first
#define scd second
#define dbg puts("IAKIOI")

using namespace std;

int read() {
	int x=0,f=1; char c=getchar();
	for(;c<'0'||c>'9';c=getchar()) f=(c=='-'?-1:1);
	for(;c<='9'&&c>='0';c=getchar()) x=(x<<1)+(x<<3)+(c^48);
	return x*f;
}
void write(int x) { if(x>=10) write(x/10); putchar('0'+x%10); }

const int mod = 998244353;
int qpo(int a,int b) {int res=1; for(;b;b>>=1,a=(a*a)%mod) if(b&1) res=res*a%mod; return res; }
int inv(int a) {return qpo(a,mod-2); }

const int maxn = 1050;

int n,m;
char ch[maxn][maxn];

int calc(int x,int y) {
	return (x-1)*m+y;
}

pii recalc(int S) {
	int x=(S-1)/m+1,y=(S-1)%m+1;
	return {x,y};
}

#define ppp array<int,4>

int dis1[maxn*maxn];
ppp dis2[maxn*maxn];

void upd(ppp &x,ppp y) {
	if((y[0]<x[0]&&y[1]!=x[1])||(y[0]==x[0]&&y[1]!=x[1])) {
		x[3]=x[1],x[2]=x[0];
		x[1]=y[1],x[0]=y[0];
	} else if(y[0]<x[2]&&y[1]!=x[1]) {
		x[2]=y[0],x[3]=y[1];
	}

	swap(y[0],y[2]),swap(y[1],y[3]);

	if((y[0]<x[0]&&y[1]!=x[1])||(y[0]==x[0]&&y[1]!=x[1])) {
		x[3]=x[1],x[2]=x[0];
		x[1]=y[1],x[0]=y[0];
	} else if(y[0]<x[2]&&y[1]!=x[1]) {
		x[2]=y[0],x[3]=y[1];
	}

	swap(y[0],y[2]),swap(y[1],y[3]);
}

bool ck(ppp x,ppp y) {
	ppp qwq=x; upd(qwq,y);
	return (qwq!=x);
}

int dx[5]={0,0,0,1,-1},dy[5]={0,1,-1,0,0};

void bfs1(int st) {
	queue<int> q; q.push(st);
	For(i,1,n) For(j,1,m) dis1[calc(i,j)]=1e9;
	dis1[st]=0;
	while(!q.empty()) {
		auto [x,y]=recalc(q.front()); q.pop();
		For(i,1,4) {
			int X=x+dx[i],Y=y+dy[i];
			if(X>=1&&X<=n&&Y>=1&&Y<=m&&ch[X][Y]!='#'&&dis1[calc(X,Y)]>dis1[calc(x,y)]+1) {
				dis1[calc(X,Y)]=dis1[calc(x,y)]+1;
				q.push(calc(X,Y));
			}
		}
	}
}

void bfs2() {
	queue<int> q;

	For(i,1,n) For(j,1,m) dis2[calc(i,j)]={(ll)1e9,0,(ll)1e9,0};
	For(i,1,n) {
		if(ch[i][1]!='#') {
			dis2[calc(i,1)]={0,calc(i,1),(ll)1e9,0}; q.push(calc(i,1));
		}
		if(ch[i][m]!='#') {
			dis2[calc(i,m)]={0,calc(i,m),(ll)1e9,0}; q.push(calc(i,m));
		}
	}
	For(i,2,m-1) {
		if(ch[1][i]!='#') {
			dis2[calc(1,i)]={0,calc(1,i),(ll)1e9,0}; q.push(calc(1,i));
		}
		if(ch[n][i]!='#') {
			dis2[calc(n,i)]={0,calc(n,i),(ll)1e9,0}; q.push(calc(n,i));
		}
	}

	while(!q.empty()) {
		auto [x,y]=recalc(q.front()); q.pop();
		ppp tmp=dis2[calc(x,y)];
		tmp[0]=min(tmp[0]+1,(ll)1e9);
		tmp[2]=min(tmp[2]+1,(ll)1e9);
		For(i,1,4) {
			int X=x+dx[i],Y=y+dy[i];
			if(X>=1&&X<=n&&Y>=1&&Y<=m&&ch[X][Y]!='#') {
//				cout<<"pd:"<<x<<' '<<y<<':'<<tmp[0]<<' '<<tmp[1]<<' '<<tmp[2]<<' '<<tmp[3]<<' '<<dis2[calc(X,Y)][0]<<' '<<dis2[calc(X,Y)][2]<<'\n';
				if(ck(dis2[calc(X,Y)],tmp)) {
					upd(dis2[calc(X,Y)],tmp);
					q.push(calc(X,Y));
				}
			}
		}
	}
}

void work() {
	cin>>n>>m;
	int blk=0;
	For(i,1,n) For(j,1,m) cin>>ch[i][j],blk+=(ch[i][j]=='#');
	int X=0,Y=0;
	For(i,1,n) For(j,1,m) if(ch[i][j]=='V') X=i,Y=j,bfs1(calc(i,j));
	bfs2();

	int cnt=0;
	For(i,1,n) {
		if(dis1[calc(i,1)]!=1e9) cnt++;
		if(dis1[calc(i,m)]!=1e9) cnt++;
	}
	For(i,2,m-1) {
		if(dis1[calc(1,i)]!=1e9) cnt++;
		if(dis1[calc(n,i)]!=1e9) cnt++;
	}
	if(cnt==0) {
		return cout<<n*m-1-blk<<'\n',void();
	}
	if(cnt==1) {
		return cout<<n*m-dis2[calc(X,Y)][0]-blk-1<<'\n',void();
	}
	int ans=1e18;
	For(i,1,n) For(j,1,m) {
//		cout<<i<<' '<<j<<' '<<dis1[calc(i,j)]<<' '<<dis2[calc(i,j)][0]<<' '<<dis2[calc(i,j)][1]<<' '<<dis2[calc(i,j)][2]<<' '<<dis2[calc(i,j)][3]<<'\n';
		ans=min(ans,dis1[calc(i,j)]+dis2[calc(i,j)][0]+dis2[calc(i,j)][2]);
	}
//	cout<<ans<<' '<<blk<<'\n';
	cout<<n*m-ans-blk-1<<'\n';
}

signed main() {

//	freopen("data.in","r",stdin);
//	freopen("myans.out","w",stdout);
//	ios::sync_with_stdio(false);
//	cin.tie(0); cout.tie(0);
	double stt=clock();
	int _=1;
//	_=read();
	cin>>_;
	For(i,1,_) {
		work();
	}
	cerr<<"\nTotal Time is:"<<(clock()-stt)*1.0/1000<<" second(s)."<<'\n';
	return 0;
}
原文