HDU - 4804 Campus Design(轮廓线dp)

Nanjing University of Science and Technology is celebrating its 60th anniversary. In order to make room for student activities, to make the university a more pleasant place for learning, and to beautify the campus, the college administrator decided to start construction on an open space. 
The designers measured the open space and come to a conclusion that the open space is a rectangle with a length of n meters and a width of m meters. Then they split the open space into n x m squares. To make it more beautiful, the designer decides to cover the open space with 1 x 1 bricks and 1 x 2 bricks, according to the following rules: 

1. All the bricks can be placed horizontally or vertically 
2. The vertexes of the bricks should be placed on integer lattice points 
3. The number of 1 x 1 bricks shouldn’t be less than C or more than D. The number of 1 x 2 bricks is unlimited. 
4. Some squares have a flowerbed on it, so it should not be covered by any brick. (We use 0 to represent a square with flowerbet and 1 to represent other squares) 

Now the designers want to know how many ways are there to cover the open space, meeting the above requirements.

Input

There are several test cases, please process till EOF. 
Each test case starts with a line containing four integers N(1 <= N <= 100), M(1 <= M <= 10), C, D(1 <= C <= D <= 20). Then following N lines, each being a string with the length of M. The string consists of ‘0’ and ‘1’ only, where ‘0’ means the square should not be covered by any brick, and ‘1’ otherwise.

Output

Please print one line per test case. Each line should contain an integers representing the answer to the problem (mod 10 9 + 7).

Sample Input

1 1 0 0
1
1 1 1 2
0
1 1 1 2
1
1 2 1 2
11
1 2 0 2
01
1 2 0 2
11
2 2 0 0
10
10
2 2 0 0
01
10
2 2 0 0
11
11
4 5 3 5
11111
11011
10101
11111

Sample Output

0
0
1
1
1
2
1
0
2
954

 

       不懂轮廓线的先去看看轮廓线的定义~~:

       然后这样的话,与基本的轮廓线dp多了一个两个东西,一个是可以放置制定数量的1*1的小木块,另一个是有位置不可以防止。对于那些不可以防止的地方我们把他们看成放置好的,且不计入限制数量1*1的小木块就好了。

       另一个问题就是如果开全部的dp数组会mle,这样的话就需要滚动数组这个概念了。~~

       然后对于放置木块的情况:

       1.对于放置1*1的木块,他的上方必须是已经填充好的情况。

       2.对于横放1*2的木块,他的左边必须没有防止木块,且上方必须放置着木块。

       3.对于竖放1*2的木块,(如果他的上方有了木块,那这个地方就不能放1*2木块了,取空,如果上面没有,就可以继续进行状态转移);

       4.对于不能放置的地方,我们可以在每次特殊处理就好(当作不计入数量的1*1木块)

 

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n, m, c, d, big;
int pre = 0, now = 1;
long long dp[2][22][1222];//滚动数组。
char mp[105][15];
void init() {
	big = (1 << m);
	memset(dp[now], 0, sizeof(dp[now]));
	dp[now][0][big - 1] = 1;
	for (int i = 0; i < n; i++)
		cin >> mp[i];
}
int main() {
	ios::sync_with_stdio(0);
	while (cin >> n >> m >> c >> d) {
		init();
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				swap(now, pre);
				memset(dp[now], 0, sizeof(dp[now]));
				if (mp[i][j] != '0') {
					for (int k = 0; k <= d; k++) {
						for (int s = 0; s < big; s++) {
							if (k && (s & 1 << j))
								dp[now][k][s] = (dp[now][k][s] +
									dp[pre][k - 1][s]) % mod;
							if (j && !(s & 1 << (j - 1)) && (s & 1 << j))
								dp[now][k][s | 1 << (j - 1)] = (dp[now][k][s | 1 << (j - 1)] +
									dp[pre][k][s]) % mod;
							dp[now][k][s ^ 1 << j] = (dp[now][k][s ^ 1 << j] +
								dp[pre][k][s]) % mod;
						}
					}
				}
				else {
					for (int k = 0; k <= d; k++) {
						for (int s = 0; s < big; s++) {
							if (s & 1 << j)
								dp[now][k][s] = (dp[now][k][s] + dp[pre][k][s]) % mod;
						}
					}
				}
			}
		}
		long long int ans = 0;
		for (int i = c; i <= d; i++)
			ans = (dp[now][i][big - 1] + ans) % mod;
		cout << ans << endl;
	}
	return 0;
}

 

全部评论

相关推荐

1.&nbsp;自我介绍2.&nbsp;项目都是自己写的吗?3.&nbsp;我看你用&nbsp;koa2&nbsp;写后端,为什么选择它,能讲讲吗?4.&nbsp;那你提到&nbsp;koa2&nbsp;它是不提供中间件的,你是怎么解决的?5.&nbsp;中间件的原理是什么?(洋葱模型)6.&nbsp;你刚刚说碰到&nbsp;next()&nbsp;就进入下一个中间件,那&nbsp;next&nbsp;只能执行同步,如果是异步的话,你是怎么处理的?(async/await,但是我发现,有的中间件需要在异步中间件之前执行,所以我用&nbsp;try/catch&nbsp;来处理异步中间件的异常)7.&nbsp;JS&nbsp;异步发展史,以及它们的优缺点说一下&nbsp;(回调函数--Promise--Generator--async/await)8.&nbsp;你刚刚说&nbsp;Promise&nbsp;状态不能更改,那如果我要设计一个能修改&nbsp;Promise&nbsp;状态的函数,你会怎么设计?9.&nbsp;CSS&nbsp;水平垂直居中的方法(flex、grid、绝对定位&nbsp;+&nbsp;margin:auto、绝对定位&nbsp;+&nbsp;负&nbsp;margin、绝对定位&nbsp;+&nbsp;transform、table-cell)10.&nbsp;你刚刚说到&nbsp;flex&nbsp;布局,那&nbsp;flex:1&nbsp;是什么意思?(flex:&nbsp;flex-grow&nbsp;&nbsp;flex-shrink&nbsp;&nbsp;flex-basis;等价&nbsp;flex:1&nbsp;1&nbsp;0%表示元素可以均分剩余空间,可拉伸、可压缩,不依赖内容宽度,自动自适应填充布局。)11.&nbsp;父容器宽是&nbsp;500px,然后它左右各有两个子容器是&nbsp;100px,如果设置&nbsp;flex:&nbsp;1,那它的宽度是多少?(500-100-100=300px)12.&nbsp;说说你对浏览器缓存的理解(强缓存、协商缓存)13.&nbsp;如果一个用户,他怎么去刷新都无法刷到最新版的代码,你能说下可能的原因吗?(版本号、hash等)还有吗?(我说我不知道了,面试官说还有&nbsp;CDN&nbsp;没有同步,我说企业才会这么干,自己写项目一般不会,我知道&nbsp;cdn&nbsp;是用来解决高并发的手段)14.&nbsp;React你熟吗?说下&nbsp;React&nbsp;函数组件和类组件的区别15.&nbsp;怎么避免&nbsp;Hooks&nbsp;导致组件重新渲染?(使用&nbsp;useCallback、useMemo、React.memo、useRef等等)16.&nbsp;谈一下我对&nbsp;React&nbsp;的状态管理的理解(Redux、Mobx、Zustand,我说&nbsp;Zustand&nbsp;用的最多)17.&nbsp;React&nbsp;常见的&nbsp;hooks&nbsp;有哪些?(useState、useEffect、useRef、useCallback、useMemo、useReducer、useContext、useImperativeHandle、useLayoutEffect、useDebugValue)18.&nbsp;TS&nbsp;你熟吗?我们引进&nbsp;TS&nbsp;的目的是为什么?19.&nbsp;interface&nbsp;和&nbsp;type&nbsp;的区别20.&nbsp;说下&nbsp;TS&nbsp;里的泛型21.&nbsp;我现在有十个字段,比如十个字段就要&nbsp;A&nbsp;B&nbsp;C&nbsp;D&nbsp;E&nbsp;F&nbsp;G&nbsp;这种。那我现在另有另外一个方法,这个方法接受的参数呢,必须是这个&nbsp;interface&nbsp;A&nbsp;里面的这个&nbsp;K。就比如说你可以是&nbsp;A&nbsp;B&nbsp;C&nbsp;可以&nbsp;A&nbsp;B&nbsp;C&nbsp;D&nbsp;任何组合都可以,但是必须是这个&nbsp;interface&nbsp;里面的&nbsp;A&nbsp;里面的定义的。这个&nbsp;K&nbsp;这种类型的话是怎么去定义呢?(说实话我有点不太理解啥意思,反正我说了&nbsp;keyof)```&nbsp;TypeScriptinterface&nbsp;Obj&nbsp;{A:&nbsp;stringB:&nbsp;stringC:&nbsp;stringD:&nbsp;stringE:&nbsp;string//&nbsp;其他字段...}```22.&nbsp;vite&nbsp;用过吗?说说和&nbsp;webpack&nbsp;的区别。vite&nbsp;的优缺点是什么23.&nbsp;说说&nbsp;Tree&nbsp;shaking(树摇)&nbsp;和&nbsp;Code&nbsp;Splitting&nbsp;(代码分割)的区别24.&nbsp;Git&nbsp;你熟吗?说说&nbsp;git&nbsp;merge&nbsp;和&nbsp;git&nbsp;rebase&nbsp;的区别,什么时候用&nbsp;git&nbsp;merge,什么时候用&nbsp;git&nbsp;rebase?25.&nbsp;web3&nbsp;你熟吗?(不太熟,听说过而已)26.&nbsp;我看你自我介绍说了&nbsp;AI,你是怎么用的?27.&nbsp;除了提示词,还有什么能让&nbsp;AI&nbsp;更聪明?28.&nbsp;AI&nbsp;的优缺点你说一下29.&nbsp;AI&nbsp;发展这么快,你觉得我们以后会扮演什么角色?30.&nbsp;反问基本都答上来了。面了我80分钟,我还以为稳过的
查看29道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务