0
点赞
收藏
分享

微信扫一扫

poj 1038 Bugs Integrated, Inc.


Description

Bugs Integrated, Inc. is a major manufacturer of advanced memory chips. They are launching production of a new six terabyte Q-RAM chip. Each chip consists of six unit squares arranged in a form of a 2*3 rectangle. The way Q-RAM chips are made is such that one takes a rectangular plate of silicon divided into N*M unit squares. Then all squares are tested carefully and the bad ones are marked with a black marker.

poj 1038 Bugs Integrated, Inc._#include

Finally, the plate of silicon is cut into memory chips. Each chip consists of 2*3 (or 3*2) unit squares. Of course, no chip can contain any bad (marked) squares. It might not be possible to cut the plate so that every good unit square is a part of some memory chip. The corporation wants to waste as little good squares as possible. Therefore they would like to know how to cut the plate to make the maximum number of chips possible.
Task
You are given the dimensions of several silicon plates and a list of all bad unit squares for each plate. Your task is to write a program that computes for each plate the maximum number of chips that can be cut out of the plate.
Input
The first line of the input file consists of a single integer D (1 <= D <= 5), denoting the number of silicon plates. D blocks follow, each describing one silicon plate. The first line of each block contains three integers N (1 <= N <= 150), M (1 <= M <= 10), K (0 <= K <= MN) separated by single spaces. N is the length of the plate, M is its height and K is the number of bad squares in the plate. The following K lines contain a list of bad squares. Each line consists of two integers x and y (1 <= x <= N, 1 <= y <= M) ?coordinates of one bad square (the upper left square has coordinates [1, 1], the bottom right is [N,M]).

Output

For each plate in the input file output a single line containing the maximum number of memory chips that can be cut out of the plate.

Sample Input
2
6 6 5
1 4
4 6
2 2
3 6
6 4
6 5 4
3 3
6 1
6 2
6 4

Sample Output
3
4

【分析】
题意:给了一个n*m的棋盘,让用3*2的方块填棋盘,方块不能互相覆盖,且棋盘中有一些坏点(不能放方块),问最多能放几个方块?

一道神题…根本想不出来这么gecudadan的方法…
主要是借助Monster_Yi的讲解才搞懂…
大家看注释自行理解下…

【代码】

//poj 1038
#include<iostream>
#include<cstdio>
#include<cstring>
#define p(i) p/pow[i]%3
#define q(i) q/pow[i]%3
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
bool ok[200][12];
int pow[12],dp[2][100000];
int n,m,T,c,p,q,now,ans;
inline void dfs(int x,int y,int tot)
{
if(y>m) return;
if(y<m && !(p(y)) && !(p(y+1)) && !(q(y)) && !(q(y+1)))
{
q+=(pow[y]+pow[y+1])*2;
dp[now][q]=max(dp[now][q],tot+1);
dfs(x,y+2,tot+1);
q-=(pow[y]+pow[y+1])*2;
}
if(y<m-1 && !(q(y)) && !(q(y+1)) && !(q(y+2)))
{
q+=(pow[y]+pow[y+1]+pow[y+2])*2;
dp[now][q]=max(dp[now][q],tot+1);
dfs(x,y+3,tot+1);
q-=(pow[y]+pow[y+1]+pow[y+2])*2;
}
dfs(x,y+1,tot);
}
int main()
{
int i,j,k,a,b;
pow[1]=1;
fo(i,2,11) pow[i]=pow[i-1]*3;
scanf("%d",&T);
while(T--)
{
ans=p=q=0;now=1;
memset(dp,-1,sizeof dp);
memset(ok,1,sizeof ok);
scanf("%d%d%d",&n,&m,&c);
while(c--)
{
scanf("%d%d",&a,&b);
ok[a][b]=0; //坏点标记
}
// 预处理第一行状态 0表示上下ok,1表示上被占下未被占,2表示下被占 //
fo(j,1,m)
if(ok[1][j]) p+=pow[j];
else p+=pow[j]*2;
dp[1][p]=0;
// 枚举i-1行状态p,计算出相应i行状态q,然后进行dfs //
fo(i,2,n)
{
now^=1;
memset(dp[now],-1,sizeof dp[now]);
fo(p,0,pow[m+1]-1) if(dp[now^1][p]!=-1) //筛去不合法状态
{
q=0;
fo(j,1,m)
if(ok[i][j]) q+=pow[j]*(p(j)?p(j)-1:0);
else q+=pow[j]*2;
dp[now][q]=max(dp[now][q],dp[now^1][p]); //p都不放的情况
dfs(i,1,dp[now^1][p]);
}
}
fo(p,0,pow[m+1]-1) ans=max(ans,dp[now][p]);
printf("%d\n",ans);
}
return 0;
}
//2
//6 6 5
//1 4
//4 6
//2 2
//3 6
//6 4
//6 5 4
//3 3
//6 1
//6 2
//6 4


举报

相关推荐

0 条评论