题解:P14661 音游扫键
像我这种赛时只会 T1 和 T3(就是这道题),不会 T2 的人应该很少了吧。
先考虑第一小问。考虑进行到第 # 一定在某一次“扫键”操作中。对于第 # 时,如果能和第
但如果左上方和右上方都可以接,怎么接呢?考虑如下情况:
#.#.
.#.#
如果接右上方,答案会多一。往左接一定更优,因为给了以后的 # 更多接上的可能。
第一小问就做完了。
#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;
}
考虑哪些情况对方案数有贡献。可以先考虑两行。
#.#
.#.
显然,有两种方案。
.#.
#.#
显然,还是有两种方案。
#.#.#
.#.#.
显然,有三种方案。
#.#.
.#.#
显然,只有一种方案。
.#.#.#.#
#.#.#.#.
显然,还是只有一种方案。
揭晓规律:当井号数量为奇数时,方案数就是井号数除以
扩展到
#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 记录。