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.

Examples

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
Note

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(简单模拟)的更多相关文章

  1. 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 ...

  2. Codeforces Beta Round #1 B. Spreadsheets 模拟

    B. Spreadsheets 题目连接: http://www.codeforces.com/contest/1/problem/B Description In the popular sprea ...

  3. java web学习总结(二十二) -------------------简单模拟SpringMVC

    在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...

  4. WPF简单模拟QQ登录背景动画

    介绍 之所以说是简单模拟,是因为我不知道QQ登录背景动画是怎么实现的.这里是通过一些办法把它简化了,做成了类似的效果 效果图 大体思路 首先把背景看成是一个4行8列的点的阵距,X轴Y轴都是距离70.把 ...

  5. Linux 内核 链表 的简单模拟(2)

    接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the & ...

  6. Linux 内核 链表 的简单模拟(1)

    第零章:扯扯淡 出一个有意思的题目:用一个宏定义FIND求一个结构体struct里某个变量相对struc的编移量,如 struct student { int a; //FIND(struct stu ...

  7. JavaWeb学习总结(四十九)——简单模拟Sping MVC

    在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...

  8. 简单模拟Hibernate的主要功能实现

    在学习期间接触到Hibernate框架,这是一款非常优秀的O/R映射框架,大大简化了在开发web项目过程中对数据库的操作.这里就简单模拟其底层的实现. /*******代码部分,及其主要注解***** ...

  9. 【HDU 4452 Running Rabbits】简单模拟

    两只兔子Tom和Jerry在一个n*n的格子区域跑,分别起始于(1,1)和(n,n),有各自的速度speed(格/小时).初始方向dir(E.N.W.S)和左转周期turn(小时/次). 各自每小时往 ...

随机推荐

  1. bash_profile打不开怎么办,用nano .bash_profile打开

    I’ve spent years curating a collection of Mac bash aliases and shortcuts to make my life easier. My ...

  2. UVA - 12338 Anti-Rhyme Pairs 二分+hash

    题目链接:传送门 题意: 给你n个串 问你任意两个串的最大公共前缀长度是多少 题解: 二分+hash 思路很明显,我最近用来写hash 很鸡肋 #include<bits/stdc++.h> ...

  3. hadoop报JAVA_HOME is not set暂时解决办法

    直接在etc/hadoop/hadoop-env.sh中 export JAVA_HOME=XXX

  4. R学习-- 数组和矩阵

    生成 4行5列的数组,逐列逐行赋值x = array(1:20, dim= c(4,5)) 依据已知向量生成二维数组i = array(c(1:3,3:1,4:6,5:7), dim=c(3,4))也 ...

  5. Qt & opencv 学习(二)

    例子:打开图片并显示.打开图片利用Qt的标准文件对话框,第一步是利用OpenCV创建窗口并显示. 添加一个Button,转到信号槽添加代码: using namespace cv; Mat image ...

  6. IE6span 高度问题

    IE6盒子的最小高度为20px,所以设置低于20px大小时,加上_overflow:hidden;

  7. NOIP2016总结

    Day1: T1:模拟: #include<iostream> #include<cstdio> #include<cstdlib> #include<cst ...

  8. 通过Oracle sql developer从sqlserver迁移数据到oracle

    通过Oracle sql developer工具从sqlserver迁移数据到oracle 序言 一般情况下,sqlserver数据迁移到oracle,我们可以使用ODI来进行.但ODI的安装.配置. ...

  9. lucene倒排索引瘦身的一些实验——merge的本质是减少cfx文件 变为pos和doc;存储term vector多了tvx和tvd文件有337M

    store NO 压缩后的原始数据 原始数据大小 索引大小 索引时间 单词搜索时间 266 791 594 176 0.2 文件组成见后 运行forceMerge(3)后 merge的本质是减少cfx ...

  10. javacv开发详解之1:调用本机摄像头视频(建议使用javaCV最新版本)

    javaCV系列文章: javacv开发详解之1:调用本机摄像头视频 javaCV开发详解之2:推流器实现,推本地摄像头视频到流媒体服务器以及摄像头录制视频功能实现(基于javaCV-FFMPEG.j ...