HDU-6416 Rikka with Seam

                                                                                               Rikka with Seam

                                                                           Time Limit: 16000/8000 MS (Java/Others)


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).

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
11101
 

Sample Output
2
70
199
 

题意:从上往下走,每次可以往前或者往后走k个,删掉当前元素,问最多可以形成多少个不同的矩阵.

思路:每次删掉当前点的方案数dp[i,j]都可以由dp[i-1,j-k],dp[i-1][j+k]转移过来,但是会有重复,所以我们给每个点记录一个真正的由i-1转移过来的方案数,再记录如果和前一个字符重复的方案数,这样我们每次转移的时候[i-1,j-k]肯定要用真正的,其余的部分都用去重之后的.   详见代码.

代码:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const ll mod = 998244353;

int n,m,k;
char mp[3456][3456];
ll sum[3456][3456],a[3456][3456],d[3456][3456];

inline ll query(int x,int l,int r)
{
	ll ans = 0;
	ans+= d[x][l];
	ans+= sum[x][r]-sum[x][l];
	return ans%mod;
}

int main()
{
	int t;
	cin>>t;
	
	while(t--)
	{	
		scanf("%d %d %d",&n,&m,&k);
		for(int i = 1;i<= n;i++)
			scanf(" %s",mp[i]+1);
		
		for(int i = 1;i<= m;i++)
		{
			d[1][i] = 1;
			a[1][i] = mp[1][i] == mp[1][i-1]?0:1;
			sum[1][i] = sum[1][i-1]+a[1][i];
		}
		for(int i = 2;i<= n;i++)
		{
			for(int j = 1;j<= m;j++)
			{
				d[i][j] = query(i-1,max(1,j-k),min(m,j+k));
				a[i][j] = query(i-1,max(1,j-k),min(m,j+k));
				if(mp[i][j] == mp[i][j-1]) a[i][j]-= query(i-1,max(1,j-k),min(m,j+k-1));
				a[i][j] = (a[i][j]+mod)%mod;
				sum[i][j] = (sum[i][j-1]+a[i][j])%mod;
			}
		}
		
		ll ans = 0;
		for(int i = 1;i<= m;i++)
			(ans+= a[n][i])%= mod;
		printf("%lld\n",ans);
	}
	
	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值