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(小时/次). 各自每小时往 ...
随机推荐
- 多媒体开发之---h264快速运动估计算法
#include "stdio.h"#include "stdlib.h"#include "malloc.h"#include " ...
- Hibernate Jar包官方下载
1.新手入门,从官网下载Hibernate,选择 Hibernate ORM 2.选择Releases-Overview 3.上面列出的是最新版本,下面有一个see older series 直接下载 ...
- 在线安装Ganglia3.6.0,nginx+php搭建gweb,绝对通过
环境:CentOS6.5 minimal 目标:安装Ganglia核心组件(gmond, gmetad, gmetric, gstat, libganglia).Ganglia web 准备 yum增 ...
- kbmMemTable关于内存表的使用,以及各种三层框架的评价
关于内存表的使用(kbmMemTable) 关于内存表的使用说明一. Delphi使用内存表1.1 Delphi创建内存表步骤:1. 创建一个Ttable实例.2. 设置一个DataBaseName为 ...
- hrtimer高精度定时器的简单使用【学习笔记】
#include <linux/module.h> #include <linux/kernel.h> #include <linux/hrtimer.h> #in ...
- Spring Boot2.0之 整合Redis集群
项目目录结构: pom: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...
- hdu acm 1114 Piggy-Bank 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 题目意思:给出一个空的猪仔钱ang 的重量E,和一个装满钱的猪仔钱ang 的重量F你,实质上能装 ...
- 第五届蓝桥杯C++B组省赛
1.啤酒和饮料 2.切面条 3.李白打酒 4.史丰收速算 5.打印图形 6.奇怪的分式 7.六角填数 8.蚂蚁感冒 9.地宫取宝 10.小朋友排队
- hdu 4302 Holedox Eating(优先队列/线段树)
题意:一只蚂蚁位与原点,在x轴正半轴上会不时地出现一些蛋糕,蚂蚁每次想吃蛋糕时选取最近的去吃,如果前后距离相同,则吃眼前的那一块(即方向为蚂蚁的正前),求最后蚂蚁行进距离. 思路:优先队列q存储蚂蚁前 ...
- 纯CSS画WP8界面
我的手机是诺基亚920,13年4月份买的.工作之余,就想用css做一下WP8的界面效果,如上图所示.不做不知道,一做还挺难的.尤其是画那个QQ 的企鹅图标,太难画了.怎么画都不像. <!doct ...