跳到正文

题解

题解:P14661 音游扫键

wyyinput约 6 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 6 分钟
题目
P14661

版本

  1. v1当前

像我这种赛时只会 T1 和 T3(就是这道题),不会 T2 的人应该很少了吧。

先考虑第一小问。考虑进行到第 ii 行时需要多少次“扫键”操作。显然,这和前 i−1i-1 行哪些位置是“扫键”操作的开头无关,因为第 i−1i-1 行的每一个 # 一定在某一次“扫键”操作中。对于第 ii 行,从左往右扫,遇到 # 时,如果能和第 i−1i-1 行接上,那就不用管。否则,要对操作数加一。

但如果左上方和右上方都可以接,怎么接呢?考虑如下情况:

代码
#.#.
.#.#

如果接右上方,答案会多一。往左接一定更优,因为给了以后的 # 更多接上的可能。

第一小问就做完了。

C++
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define fi first
#define se second
#define gtc getchar
#define ptc putchar
using namespace std;
const int N=1e3+5;
int t;
int n,m;
char c[N][N];
int f[N];
bool vis[N];
int main(){
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++){
			scanf("%s",c[i]+1);
		}
		f[1]=0;
		for(int i=1;i<=m;i++){
			f[1]+=(c[1][i]=='#');
		}
		for(int i=2;i<=n;i++){
			f[i]=f[i-1];
			for(int j=1;j<=m;j++){
				vis[j]=0;
			}
			for(int j=1;j<=m;j++){
				if(c[i][j]=='.') continue;
				if(j-1&&c[i-1][j-1]=='#'&&vis[j-1]==0){
					vis[j-1]=1;
				}
				else if(j+1<=m&&c[i-1][j+1]=='#'&&vis[j+1]==0){
					vis[j+1]=1;
				}
				else{
					++f[i];
				}
			}
		}
		printf("%d 1\n",f[n]);
	}
	return 0;
}

考虑哪些情况对方案数有贡献。可以先考虑两行。

代码
#.#
.#.

显然,有两种方案。

代码
.#.
#.#

显然,还是有两种方案。

代码
#.#.#
.#.#.

显然,有三种方案。

代码
#.#.
.#.#

显然,只有一种方案。

代码
.#.#.#.#
#.#.#.#.

显然,还是只有一种方案。

揭晓规律:当井号数量为奇数时,方案数就是井号数除以 22 加 11。因为无论怎样选,一定会空出一个井号。当井号数量为偶数时,不产生贡献,因为上下一一对应。

扩展到 nn 行就相当于累乘,应该想明白了两行就都好做了。

C++
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define fi first
#define se second
#define gtc getchar
#define ptc putchar
using namespace std;
const int N=1e3+5;
const int mod=998244353;
int t;
int n,m;
char c[N][N];
int f[N];
bool vis[N];
ll ans;
int main(){
	scanf("%d",&t);
	while(t--){
		ans=1;
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++){
			scanf("%s",c[i]+1);
		}
		f[1]=0;
		for(int i=1;i<=m;i++){
			f[1]+=(c[1][i]=='#');
		}
		for(int i=2;i<=n;i++){
			int cnt=0;
			for(int j=1;j<=m;j++){
				if(cnt){
					if(j%2==0&&c[i-1][j]=='#') ++cnt;
					else if(j%2&&c[i][j]=='#') ++cnt;
					else if(cnt%2){
						ans*=(cnt+1)/2;
						ans%=mod;
						cnt=0;
					}
					else{
						cnt=0;
					}
				}
				else{
					if(c[i-(j%2==0)][j]=='#') cnt=1;
				}
			}
			if(cnt%2){
				ans*=(cnt+1)/2;
				ans%=mod;
			}
			cnt=0;
			for(int j=1;j<=m;j++){
				if(cnt){
					if(j%2&&c[i-1][j]=='#') ++cnt;
					else if(j%2==0&&c[i][j]=='#') ++cnt;
					else if(cnt%2){
						ans*=(cnt+1)/2;
						ans%=mod;
						cnt=0;
					}
					else{
						cnt=0;
					}
				}
				else{
					if(c[i-j%2][j]=='#') cnt=1;
				}
			}
			if(cnt%2){
				ans*=(cnt+1)/2;
				ans%=mod;
			}
			f[i]=f[i-1];
			for(int j=1;j<=m;j++){
				vis[j]=0;
			}
			for(int j=1;j<=m;j++){
				if(c[i][j]=='.') continue;
				if(j-1&&c[i-1][j-1]=='#'&&vis[j-1]==0){
					vis[j-1]=1;
				}
				else if(j+1<=m&&c[i-1][j+1]=='#'&&vis[j+1]==0){
					vis[j+1]=1;
				}
				else{
					++f[i];
				}
			}
		}
		printf("%d %lld\n",f[n],ans);
	}
	return 0;
}

AC 记录。

原文