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(小时/次). 各自每小时往 ...
随机推荐
- mac classpath设置
I've been searching for the answer daylong, and finally had the problems solved. I am going to write ...
- 几个 PHP 的"魔术常量"
__LINE__ 文件中的当前行号. __FILE__ 文件的完整路径和文件名.如果用在被包含文件中,则返回被包含的文件名.自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路径(如果是符 ...
- C/S与B/S架构 区别·与优点
C/S和B/S,是再普通不过的两种软件架构方式,都可以进行同样的业务处理,甚至也可以用相同的方式实现共同的逻辑.既然如此,为何还要区分彼此呢?那我们就来看看二者的区别和联系. 一.C/S 架构 1 ...
- spring的依赖注入(DI)、控制反转(IOC)和面向切面(AOP)
在spring的配置文件增加 <context:component-scan base-package="com.jmu.ccjoin.service"/> <c ...
- Vijos P1389婚礼上的小杉
背景 小杉的幻想来到了经典日剧<求婚大作战>的场景里……他正在婚礼上看幻灯片,一边看着可爱的新娘长泽雅美,一边想,如果能再来一次就好了(-.-干嘛幻想这么郁闷的场景……). 小杉身为新一代 ...
- 一步一步学Silverlight 2系列(6):键盘事件处理
一步一步学Silverlight 2系列(6):键盘事件处理 概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言V ...
- putty与emacs
win环境下putty登录到linux并使用emacs时,需要折腾好配置才能正常工作.下面是折腾过程中碰到的问题与解决步骤: 1)要在putty控制台中启用鼠标,只需要在.emacs配置文件中启用xt ...
- Quartz2D绘图 及实例:下载进度
基础绘图: C语言coregraphics框架 绘制一条线:(不常用) UIBezierPath 路径画图 1.线段 线段2: 2.三角形 填充颜色 如果边框颜色和填充颜色都为红色:[[UICol ...
- (转)Vim自动补全神器:YouCompleteMe
原文出处:http://blog.jobbole.com/58978/ 第一次听说这个插件还是在偶然的情况下看到别人的博客,听说了这个插件的大名.本来打算在实训期间来完成安装的,无奈网实在不给力,也就 ...
- 「LuoguP3191」 [HNOI2007]紧急疏散EVACUATE(最大流
Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是’.’,那么表示这是一块空地:如果是’X’,那么表示这是一面墙,如果是’D’,那么表示这是一 ...