题意:给你一个矩阵,有些点是黑的,让你横切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. ubuntu复制文件或目录

    转自http://www.linuxidc.com/Linux/2008-11/17179.htm cp(copy)命令 该命令的功能是将给出的文件或目录拷贝到另一文件或目录中. 语法: cp [选项 ...

  2. O_NONBLOCK与O_NDELAY有何不同?

    O_NONBLOCK和O_NDELAY所产生的结果都是使I/O变成非搁置模式(non-blocking),在读取不到数据或是写入缓冲区已满会马上return,而不会搁置程序动作,直到有数据或写入完成. ...

  3. MySQL数据记录大小写敏感问题【转】

    MySQL大小写敏感 字符串大小写敏感和Mysql的数据库的名字.表名字.字段名字.还有字段值有关. 1.和数据库名字.表名字.存储过程和触发器有关 为0时:表示区分大小写,使用CREATE TABL ...

  4. 22 Gobs of data 设计和使用采集数据的包

    Gobs of data 24 March 2011 Introduction To transmit a data structure across a network or to store it ...

  5. Extjs 基础篇—— Function 能在定义时就能执行的方法的写法 function(){...}()

    Ext.js 中 Function能在定义时就能执行的方法的写法 function(){...}() /** * 第二部分Function:能在定义时就能执行的方法的写法 function(){... ...

  6. Python学习1-Python和Pycharm的下载与安装

    本文主要介绍Python的下载安装和Python编辑器Pycharm的下载与安装. 一.Python的下载与安装 1.下载 到Python官网上下载Python的安装文件,进入网站后显示如下图: 网速 ...

  7. 20155225 实验二《Java面向对象程序设计》实验报告

    20155225 实验二<Java面向对象程序设计>实验报告 一.单元测试 三种代码 知道了伪代码.产品代码.测试代码的关系和用途,并根据老师的例子,按测试代码调试了产品代码. 值得注意的 ...

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

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

  9. Oracle与Sqlserver数据共享

    需求:在一个集成平台中有一个主系统使用的是Oralce数据库,子系统使用的SqlServer 数据库,如何让子系统的数据库与主系统的人员同步呢? 思路:通过服务WebService 公开接口 1.与主 ...

  10. hdu 5137 去掉一个点 使得最短路最大(2014广州现场赛 K题)

    题意:从2~n-1这几个点中任意去掉一个点,使得从1到n的最短路径最大,如果任意去掉一个点1~n无通路输出Inf. Sample Input4 51 2 31 3 71 4 502 3 43 4 23 ...