CodeForces - 816C Karen and Game(简单模拟)
Problem Description
On the way to school, Karen became fixated on the puzzle game on her phone!

The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0.
One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column.
To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to gi, j.
Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task!
Input
The first line of input contains two integers, n and m (1 ≤ n, m ≤ 100), the number of rows and the number of columns in the grid, respectively.
The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains gi, j (0 ≤ gi, j ≤ 500).
Output
If there is an error and it is actually not possible to beat the level, output a single integer -1.
Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level.
The next k lines should each contain one of the following, describing the moves in the order they must be done:
- row x, (1 ≤ x ≤ n) describing a move of the form "choose the x-th row".
- col x, (1 ≤ x ≤ m) describing a move of the form "choose the x-th column".
If there are multiple optimal solutions, output any one of them.
input
3 5
2 2 2 3 2
0 0 0 1 0
1 1 1 2 1
output
4
row 1
row 1
col 4
row 3
input
3 3
0 0 0
0 1 0
0 0 0
output
-1
input
3 3
1 1 1
1 1 1
1 1 1
output
3
row 1
row 2
row 3
In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level:

In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center.
In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level:

Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
解题思路:贪心模拟,行操作完再进行列操作。注意:题目要求用最少的步数,因此当n>m时,应先对列进行贪心减操作,再对行进行操作;否则先对行进行贪心减操作,最后如果减不完,则直接输出-1.
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=;
int n,m,cnt1,cnt2,ans,sum,mp[maxn][maxn],min_row[maxn],min_col[maxn];
pair<int,int> paii_row[maxn],paii_col[maxn];
int main(){
while(cin>>n>>m){
ans=sum=cnt1=cnt2=;
for(int i=;i<=n;++i)min_row[i]=inf;
for(int j=;j<=m;++j)min_col[j]=inf;
for(int i=;i<=n;++i){
for(int j=;j<=m;++j){
cin>>mp[i][j],sum+=mp[i][j];
min_row[i]=min(min_row[i],mp[i][j]);
min_col[j]=min(min_col[j],mp[i][j]);
}
}
if(n>m){///如果行大于列,则从列开始操作
for(int j=;j<=m;++j){
for(int i=;i<=n;++i){
mp[i][j]-=min_col[j];
min_row[i]=min(min_row[i],mp[i][j]);
}
if(min_col[j])sum-=min_col[j]*n,ans+=(paii_col[cnt2].first=min_col[j]),paii_col[cnt2++].second=j,min_col[j]=;
}
for(int i=;i<=n;++i){///从行开始操作
for(int j=;j<=m;++j){
mp[i][j]-=min_row[i];
min_col[j]=min(min_col[j],mp[i][j]);
}
if(min_row[i])sum-=min_row[i]*m,ans+=(paii_row[cnt1].first=min_row[i]),paii_row[cnt1++].second=i,min_row[i]=;
}
}
else{
for(int i=;i<=n;++i){///否则先从行开始操作
for(int j=;j<=m;++j){
mp[i][j]-=min_row[i];
min_col[j]=min(min_col[j],mp[i][j]);
}
if(min_row[i])sum-=min_row[i]*m,ans+=(paii_row[cnt1].first=min_row[i]),paii_row[cnt1++].second=i,min_row[i]=;
}
for(int j=;j<=m;++j){
for(int i=;i<=n;++i){
mp[i][j]-=min_col[j];
min_row[i]=min(min_row[i],mp[i][j]);
}
if(min_col[j])sum-=min_col[j]*n,ans+=(paii_col[cnt2].first=min_col[j]),paii_col[cnt2++].second=j,min_col[j]=;
}
}
if(sum)cout<<-<<endl;
else{
cout<<ans<<endl;
for(int i=;i<cnt1;++i)
for(int j=;j<=paii_row[i].first;++j)
cout<<"row "<<paii_row[i].second<<endl;
for(int i=;i<cnt2;++i)
for(int j=;j<=paii_col[i].first;++j)
cout<<"col "<<paii_col[i].second<<endl;
}
}
return ;
}
CodeForces - 816C Karen and Game(简单模拟)的更多相关文章
- Karen and Game CodeForces - 816C (暴力+构造)
On the way to school, Karen became fixated on the puzzle game on her phone! The game is played as fo ...
- Codeforces Beta Round #1 B. Spreadsheets 模拟
B. Spreadsheets 题目连接: http://www.codeforces.com/contest/1/problem/B Description In the popular sprea ...
- java web学习总结(二十二) -------------------简单模拟SpringMVC
在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...
- WPF简单模拟QQ登录背景动画
介绍 之所以说是简单模拟,是因为我不知道QQ登录背景动画是怎么实现的.这里是通过一些办法把它简化了,做成了类似的效果 效果图 大体思路 首先把背景看成是一个4行8列的点的阵距,X轴Y轴都是距离70.把 ...
- Linux 内核 链表 的简单模拟(2)
接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the & ...
- Linux 内核 链表 的简单模拟(1)
第零章:扯扯淡 出一个有意思的题目:用一个宏定义FIND求一个结构体struct里某个变量相对struc的编移量,如 struct student { int a; //FIND(struct stu ...
- JavaWeb学习总结(四十九)——简单模拟Sping MVC
在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...
- 简单模拟Hibernate的主要功能实现
在学习期间接触到Hibernate框架,这是一款非常优秀的O/R映射框架,大大简化了在开发web项目过程中对数据库的操作.这里就简单模拟其底层的实现. /*******代码部分,及其主要注解***** ...
- 【HDU 4452 Running Rabbits】简单模拟
两只兔子Tom和Jerry在一个n*n的格子区域跑,分别起始于(1,1)和(n,n),有各自的速度speed(格/小时).初始方向dir(E.N.W.S)和左转周期turn(小时/次). 各自每小时往 ...
随机推荐
- python day - 17 面向对象的 类空间 和 组合
1. 类命名空间 在类的代码中,当python 解释器在 运行的那一刻.就会在内存中开辟一个类空间,在类的空间中会加载静态变量,以及类方法的内存地址. 当类名+()(也就是实例化过程中),内存中会再次 ...
- 解析腾讯企业邮箱到自己域名,设置mail的cname
之前注册了腾讯企业邮的免费邮箱,后来想把企业邮箱和域名绑定起来,发现了一些问题. 先来看正常的部分,假设你已经注册过了腾讯企业邮箱免费版,并且已经绑定好了域名. 然后在域名提供商那里设置域名解析的MX ...
- Struts%$#区别
1.#符号的用途一般有三种.1)访问非根对象属性,例如示例中的#session.msg表达式,由于Struts 2中值栈被视为根对象,所以访问其他非根对象时,需要加#前缀.实际上,#相当于Action ...
- 织梦CMS首页、列表页文章如何调出该文章TAG标签?
1.如果是dedecms v5.7版本直接使用标签 [field:id function=GetTags(@me)/] 就可以调用出来了.只不过不带连接的. 2.如果需要连接请注释掉include/h ...
- Axios 请求配置参数详解
axios API 可以通过向 axios 传递相关配置来创建请求 axios(config) // 发送 POST 请求 axios({ method: 'post', url: ' ...
- Oracle备份与恢复:RMAN
今天第一次学习RMAN的使用.先登录系统: 数据库未启动,rman命令不能执行.在rman下 也可以 startup . 全库备份的命令:backup database 查看备份集. 下面模拟,数据 ...
- oracle 转移表空间
一.系统表空间数据文件迁移:SQL> conn /@tmpdb as sysdba 已连接. SQL> desc dba_data_files; 名称 是否为空? 类型 --------- ...
- opencv直方图该怎么画
图像直方图是反映图像中像素分布特性的统计表,一般显示如下: 其中横坐标代表的是图像像素的种类,或者说是灰度级,纵坐标代表的是每一级灰度下像素数或者该灰度级下像素数在所有图像总像素数总所占的百分比. 直 ...
- 【POJ 2407】 Relatives
[题目链接] 点击打开链接 [算法] 欧拉函数 [代码] #include <algorithm> #include <bitset> #include <cctype& ...
- 浅谈getAttribute兼容性
最近终于证实tag.setAttribute("style", "color:#000;");在IE7中不起作用.于是百度了一些解决办法. IE的setAttr ...