Rikka with Seam
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 221 Accepted Submission(s): 99
Problem Description
Seam carving is a novel algorithm for resizing images while maintaining as much information as possible from the source image.
Now, Rikka is going to use seam carving method to deal with an n×m black and white picture. We can abstract this picture into an n×m 01 matrix A.
A K-seam of this picture is an integer sequence a which satisfies the following conditions:
1. |a|=n, ai∈[1,m].
2. |ai−ai+1|≤K, ∀i∈[1,n).
After choosing a K-seam a, Rikka reduces the size of the picture by deleting pixels (i,ai), and then she gets a matrix A′ of size n×(m−1).
For example, if the chosen seam is [1,2,3] and the picture is
⎡⎣⎢110010010⎤⎦⎥
the result matrix will be
⎡⎣⎢010010⎤⎦⎥
Rikka finds that deleting different seams may get the same result. In the previous example, seam [1,2,3],[1,2,1],[1,2,2],[1,1,1] are equivalent.
Now Rikka wants to calculate the number of different matrixes she can get by deleting exactly one K-seam.
Input
The first line contains a single integer t(1≤t≤103), the numebr of testcases.
For each testcase, the first line contains three numbers n,m,K(2≤n,m≤2×103,1≤K≤m).
Then n lines follow, each line contains a 01 string of length m which describes one row of the matrix.
The input guarantees that there are at most 5 testcases with max(n,m)>300.
Output
For each testcase, output a single line with a single number, the answer modulo 998244353.
Sample Input
3
2 2 1
00
10
5 5 1
00100
10101
00100
01000
11101
5 5 2
00100
10101
00100
01000 1
1101
Sample Output
2
70
199
题意:给出一个只包含0、1的矩阵,现在每行删去一个数,求删去后形成的不同的矩阵个数。
思路:dp 参考了https://blog.csdn.net/CatDsy/article/details/81876341的写法。
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
const int maxn = 2e3+10;
ll tot[maxn][maxn];
ll same[maxn][maxn];
bool mat[maxn][maxn];
int main()
{
int n,m,k;
int t;
char s[maxn];
scanf("%d",&t);
while(t--)
{
int len;
scanf("%d%d%d",&n,&m,&k);
for(int i = 1;i<=n;i++)
{
scanf("%s",s);
len = strlen(s);
for(int j =0;j<len;j++)
{
if(s[j]=='0')mat[i][j+1] = 0;
else mat[i][j+1] = 1;
}
}
for(int i = 1;i<=m;i++)
{
tot[1][i] = 1;
if(i==1||mat[1][i]!=mat[1][i-1])
same[1][i] = 0;
else
same[1][i] = 1;
}
for(int i = 2;i<=n;i++)
{
for(int j = 1;j<=m;j++)
{
tot[i-1][j] = (tot[i-1][j] + tot[i-1][j-1])%mod;
same[i-1][j] = (same[i-1][j] + same[i-1][j-1])%mod;
}
for(int j = 1;j<=m;j++)
{
int l = j-k-1>=0?j-k-1:0;
int r = j+k<=m?j+k:m;
tot[i][j] = (tot[i-1][r] - tot[i-1][l] -(same[i-1][r] - same[i-1][l+1]))%mod;
tot[i][j] = (tot[i][j] +mod)%mod;
r = j+k-1<=m?j+k-1:m;
same[i][j] = (tot[i-1][r] - tot[i-1][l] -(same[i-1][r] - same[i-1][l+1]))%mod;
same[i][j] = (same[i][j] + mod)%mod;
if(j==0||mat[i][j]!=mat[i][j-1])same[i][j] = 0;
}
}
ll ans = tot[n][1];
for(int i = 2;i<=m;i++)
{
ans = (ans + tot[n][i] - same[n][i] + mod)%mod;
}
printf("%lld\n",ans);
}
}