题意:给你一个矩阵,有些点是黑的,让你横切h刀,纵切v刀,问你是否能让切出的所有子矩阵的黑点数量相等。

设黑点总数为sum,sum必须能整除(h+1),进而sum/(h+1)必须能整除(v+1)。

先考虑横行,贪心地扫过去,如果到了某一行,当前统计的黑点数恰好为sum/(h+1),就在这里切一刀,接着统计。否则,如果>sum/(h+1),则无解。在这个过程中,每一行被切到哪一横组里就确定了。

然后纵切,过程跟横切类似,只不过统计的不是一个变量,而是一个大小为(h+1)的数组,如果到了某一列,当前统计的数组的每个分量的黑点数恰好为sum/(h+1)/(v+1),就在这里切一刀,接着统计。否则,如果数组的某个分量>sum/(h+1),则无解。

#include<cstdio>
#include<cstring>
using namespace std;
int T,n,m,h,v;
char a[105][105];
int hq[105];
int cnts[105],t;
bool check1(){
for(int i=1;i<=h+1;++i){
if(cnts[i]!=t){
return 0;
}
}
return 1;
}
bool check2(){
for(int i=1;i<=h+1;++i){
if(cnts[i]>t){
return 0;
}
}
return 1;
}
int main(){
//freopen("a.in","r",stdin);
scanf("%d",&T);
for(int zu=1;zu<=T;++zu){
printf("Case #%d: ",zu);
scanf("%d%d%d%d",&n,&m,&h,&v);
for(int i=1;i<=n;++i){
scanf("%s",a[i]+1);
}
int sum=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
sum+=(a[i][j]=='@');
}
}
if(sum%(h+1)!=0){
puts("IMPOSSIBLE");
continue;
}
t=sum/(h+1);
int cnt=0;
bool flag=1;
int last=0,num=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
cnt+=(a[i][j]=='@');
}
if(cnt==t){
++num;
for(int k=last+1;k<=i;++k){
hq[k]=num;
}
last=i;
cnt=0;
}
else if(cnt>t){
flag=0;
break;
}
}
if(!flag){
puts("IMPOSSIBLE");
continue;
} if(sum/(h+1)%(v+1)!=0){
puts("IMPOSSIBLE");
continue;
}
t=sum/(h+1)/(v+1);
memset(cnts,0,sizeof(int)*(2+h));
flag=1;
for(int i=1;i<=m;++i){
for(int j=1;j<=n;++j){
cnts[hq[j]]+=(a[j][i]=='@');
}
if(check1()){
memset(cnts,0,sizeof(int)*(2+h));
}
else if(!check2()){
flag=0;
break;
}
}
if(!flag){
puts("IMPOSSIBLE");
continue;
}
puts("POSSIBLE");
}
return 0;
}

【贪心】Google Code Jam Round 1A 2018 Waffle Choppers的更多相关文章

  1. 【二分答案】Google Code Jam Round 1A 2018

    题意:有R个机器人,去买B件商品,有C个收银员,每个收银员有能处理的商品数量上限mi,处理单件商品所需的时间si,以及最后的装袋时间pi. 每个收银员最多只能对应一个机器人,每个机器人也最多只能对应一 ...

  2. Google Code Jam Round 1A 2015 解题报告

    题目链接:https://code.google.com/codejam/contest/4224486/ Problem A. Mushroom Monster 这题题意就是,有N个时间点,每个时间 ...

  3. Google Code Jam Round 1A 2015 Problem B. Haircut 二分

    Problem You are waiting in a long line to get a haircut at a trendy barber shop. The shop has B barb ...

  4. [Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product

    Problem A. Minimum Scalar Product   This contest is open for practice. You can try every problem as ...

  5. Google Code Jam Round 1C 2015 Problem A. Brattleship

    Problem You're about to play a simplified "battleship" game with your little brother. The ...

  6. [C++]Store Credit——Google Code Jam Qualification Round Africa 2010

    Google Code Jam Qualification Round Africa 2010 的第一题,很简单. Problem You receive a credit C at a local ...

  7. Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words

    Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words https://code.google.com/cod ...

  8. Google Code Jam Africa 2010 Qualification Round Problem A. Store Credit

    Google Code Jam Qualification Round Africa 2010 Problem A. Store Credit https://code.google.com/code ...

  9. Google Code Jam 2010 Round 1C Problem A. Rope Intranet

    Google Code Jam 2010 Round 1C Problem A. Rope Intranet https://code.google.com/codejam/contest/61910 ...

随机推荐

  1. Android选择头像

    http://www.jianshu.com/p/8b3e78046c1c 注意:在Android6.0之后,使用相机拍照需要权限 在选择头像使用相机拍摄时添加以下代码即可. Acp.getInsta ...

  2. mysql insert锁机制【转】

    最近再找一些MySQL锁表原因,整理出来一部分sql语句会锁表的,方便查阅,整理的不是很全,都是工作中碰到的,会持续更新 笔者能力有限,如果有不正确的,或者不到位的地方,还请大家指出来,方便你我,方便 ...

  3. 18 A GIF decoder: an exercise in Go interfaces 一个GIF解码器:go语言接口训练

    A GIF decoder: an exercise in Go interfaces  一个GIF解码器:go语言接口训练 25 May 2011 Introduction At the Googl ...

  4. linux之发送邮件--sendmail服务配置

    新手入门也不知道什么日志分析服务好,鸟哥说logwatch,那我就从logwatch开始吧! logwatch用到了emai发邮件,先从配置邮件发送sendmail开始: 安装sendmail服务,我 ...

  5. NVME SSD vs SATA SSD(转)

    NVMe是个啥?未来SSD主流标准早知 关注固态硬盘的朋友应该对于这个词汇并不陌生,特别是今年NVMe也频繁出现在各大媒体文章中,随着高端SSD市场逐渐从SATA专项PCI-E时,以前的AHCI标准已 ...

  6. SQLAlchemy-对象关系教程ORM-连接,子查询

    对象关系教程ORM-连接 一:内连接 方法一: for u, a in session.query(User, Address).\ filter(User.id==Address.user_id). ...

  7. L1和L2特征的适用场景

    How to decide which regularization (L1 or L2) to use? Is there collinearity among some features? L2 ...

  8. 洛谷P3621风铃

    传送门啦 分析: 这个题看起来像是个树形dp,嗯,就是看起来像. 所以我们就按树形dp的思路去分析就好了,这个题是一个树形dp的变形题. 和以前建树是一样的,我们用邻接表来进行储存.利用邻接表的特性, ...

  9. Linux 命令find、grep

    本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下: find命令 find命令的一般形式 find命令的常用选项及实例 find与xar ...

  10. DDD领域模型企业级系统(二)

    用户层: 1.请求应用层获取用户显示的信息 2.发送命令给应用层要求执行某个命令 应用层: 对用户界面提供各种应用功能(包括信息获取与命令执行),应用层不包含业务逻辑,业务层是由应用层调用领域层(领域 ...