跳到正文

题解

题解:P14661 音游扫键

yedalong约 6 分钟
原文

这是 2026年1月18日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年1月18日
更新
2026年6月4日
篇幅
约 6 分钟
题目
P14661

版本

  1. v2当前
  2. v1

Solution

第一问简单贪心。

首先,从上到下遍历每一行。
然后,从左往右遍历每一列。
假设当前的位置为 (i,j)(i,j),且满足 ci,j=c_{i,j}= #。
一下的占用是指下一行的某个点有其他路径可以到达。
能往下走的前提条件是那个位置也是 #。
若接下来只有一种方式向下,且没被其他的占用,肯定向下,如果被占用,那么这个操作的使命就结束了。
若接下来没办法继续向下,肯定断开。
若接下来都可以,如果左边没占用往左走,右边没占用往右走,为什么呢?因为 (i+1,j−1)(i+1,j-1) 这个位置遍历到当前 (i,j)(i,j) 这个位置剩下的唯一的方案就是从 (i,j)(i,j) 出发,而 (i+1,j+1)(i+1,j+1) 这个位置还有可能从 (i,j+2)(i,j+2) 来,选择往左走有可能能够省去一次操作。

第二问分类讨论。

发现方案数的产生来源于对于某一行到下一行,满足几种方案不会影响最终的操作数。举几个例子,然后自己归纳一下,最后发现只有两种类型。

代码
W 型
#.#.#
.#.#.
代码
M 型
.#.#.
#.#.#

这两种连通块的长度都可以继续拓展下去,但是 # 的个数不能为偶数,因为如果为偶数的话,每一个 # 往下走的方案一定是固定的。
若连通块个数为 xx 个,那么这一连通块产生的方案数为 x+12\frac{x+1}{2} 个,读者自证不难。

这一段找连通块的代码是有点实现难度的。

由于第二个问题的答案是从每两行间产生的,因此要两行两行地遍历,然后再将所有连通块产生的方案乘起来即可。

code
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod=998244353;
int T,n,m,ans;
bool pd[1005][1005];
char c[1005][1005];
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>T;
	while(T--){
		cin>>n>>m;
		ans=0;
		for(int i = 1;i<=n;i++)
			for(int j = 1;j<=m;j++)
				cin>>c[i][j],pd[i][j]=0;
		//the first question
		for(int i = 1;i<=n;i++)
			for(int j = 1;j<=m;j++){
				if(!pd[i][j]&&c[i][j]=='#') ans++;
				if(c[i][j]=='#')
					if(c[i+1][j-1]=='#'&&!pd[i+1][j-1]) pd[i+1][j-1]=1;
					else if(c[i+1][j+1]=='#') pd[i+1][j+1]=1;
			}
		cout<<ans<<' ';
		//the second question
		ans=1;
		for(int i = 1;i<n;i++){
			int sum=0;
			bool flag=0;
			for(int j = 1;j<=m;j+=2){
				if(c[i][j]=='#') sum++,flag|=sum==1;
				else sum=flag=0;
				if(c[i+1][j+1]=='#'&&j!=m) sum++;
				else{
					if(flag) (ans*=(sum+1)/2)%=mod;
					sum=flag=0;
				}
			}
			if(flag&&sum%2) (ans*=(sum+1)/2)%=mod;
            sum=flag=0;
			for(int j = 0;j<=m;j+=2){
				if(c[i][j]=='#') sum++,flag|=sum==1;
				else sum=flag=0;
				if(c[i+1][j+1]=='#'&&j!=m) sum++;
				else{
					if(flag) (ans*=(sum+1)/2)%=mod;
					sum=flag=0;
				}
			}
			if(flag&&sum%2) (ans*=(sum+1)/2)%=mod;
			sum=flag=0;
			for(int j = 1;j<=m;j+=2){
				if(c[i+1][j]=='#') sum++,flag|=sum==1;
				else sum=flag=0;
				if(c[i][j+1]=='#'&&j!=m) sum++;
				else{
					if(flag) (ans*=(sum+1)/2)%=mod;
					sum=flag=0;
				}
			}
			if(flag&&sum%2) (ans*=(sum+1)/2)%=mod;
            sum=flag=0;
			for(int j = 0;j<=m;j+=2){
				if(c[i+1][j]=='#') sum++,flag|=sum==1;
				else sum=flag=0;
				if(c[i][j+1]=='#'&&j!=m) sum++;
				else{
					if(flag) (ans*=(sum+1)/2)%=mod;
					sum=flag=0;
				}
			}
			if(flag&&sum%2) (ans*=(sum+1)/2)%=mod;
		}
		cout<<ans<<'\n';
	}
	return 0;
}