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

题目链接:

CodeForces - 816C

题意:

给定一个n*m的矩阵,

每一个操作你可以选择一行或者一列,使该行或列的数值全部加1,求使用最小的操作次数使一个全部为0的矩阵变成给定矩阵,

如果不可能实现请输出-1.

思路:

预处理出每一行和每一列的最小值,

然后以n和m的关系进行分类处理,

如果n<=m,就先处理行再处理列,这样可以用最小的次数,

反而反之。

举例:

1 1 1 1

1 1 1 1

1 1 1 1

如果先处理列,就要4次,处理行只需要3次。

接下来:

如果每一行或列的最小值大于0,那么我们就可以处理最小值次,然后每一次处理,暴力的把数组中的对应元素减去1,

行和列全部处理好后,去n*m扫一边数组,如果还有数大于0,那么就是无法实现的情况。

每一步具体为什么可能需要大家自己好好思考。

细节见我的代码。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int a[][];
int sumr[];
int sumc[];
int n,m;
void buildr(int x)
{
repd(i,,n)
{
repd(j,,m)
{
if(i==x)
{
a[i][j]--;
} }
} }
void buildl(int x)
{
repd(i,,n)
{
repd(j,,m)
{
if(j==x)
{
a[i][j]--;
}
}
}
}
int main()
{
gg(n);
gg(m);
repd(i,,n)
{
repd(j,,m)
{
gg(a[i][j]); }
}
repd(i,,m)
{
int cnt=inf;
repd(j,,n)
{
cnt=min(cnt,a[j][i]);
}
sumc[i]=cnt;
}
repd(i,,n)
{
int cnt=inf;
repd(j,,m)
{
cnt=min(cnt,a[i][j]);
}
sumr[i]=cnt;
}
int ans=;
int flag=;
int fu=;
std::vector<int> r;
std::vector<int> c;
if(n<=m)
{
repd(i,,n)
{
while(sumr[i])
{
r.pb(i);
ans++;
sumr[i]--;
buildr(i);
}
}
repd(i,,m)
{
int cnt=inf;
repd(j,,n)
{
cnt=min(cnt,a[j][i]);
}
sumc[i]=cnt;
}
repd(i,,m)
{
while(sumc[i])
{
c.pb(i);
ans++;
sumc[i]--;
buildl(i);
}
}
// repd(i)
}else
{
repd(i,,m)
{
while(sumc[i])
{
c.pb(i);
ans++;
sumc[i]--;
buildl(i);
}
}
repd(i,,n)
{
int cnt=inf;
repd(j,,m)
{
cnt=min(cnt,a[i][j]);
}
sumr[i]=cnt;
}
repd(i,,n)
{
while(sumr[i])
{
r.pb(i);
ans++;
sumr[i]--;
buildr(i);
}
}
}
repd(i,,n)
{
repd(j,,m)
{
if(a[i][j]<)
{
fu=min(fu,a[i][j]);
}
if(a[i][j]!=)
{
flag=;
break;
}
}
}
if(flag)
{ printf("-1");
return ;
}
printf("%d\n",ans );
repd(i,,sz(r)-)
{
int x=r[i];
printf("row %d\n",x);
}
repd(i,,sz(c)-)
{
int x=c[i];
printf("col %d\n", x);
}
// db(fu);
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

Karen and Game CodeForces - 816C (暴力+构造)的更多相关文章

  1. 暴力+构造 Codeforces Round #283 (Div. 2) C. Removing Columns

    题目传送门 /* 题意:删除若干行,使得n行字符串成递增排序 暴力+构造:从前往后枚举列,当之前的顺序已经正确时,之后就不用考虑了,这样删列最小 */ /*********************** ...

  2. codeforces 1041 e 构造

    Codeforces 1041 E 构造题. 给出一种操作,对于一棵树,去掉它的一条边.那么这颗树被分成两个部分,两个部分的分别的最大值就是这次操作的答案. 现在给出一棵树所有操作的结果,问能不能构造 ...

  3. Codeforces Round #306 (Div. 2)A B C D 暴力 位/暴力 暴力 构造

    A. Two Substrings time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. Codeforces Round #487 (Div. 2) A Mist of Florescence (暴力构造)

    C. A Mist of Florescence time limit per test 1 second memory limit per test 256 megabytes input stan ...

  5. CodeForces - 816C Karen and Game(简单模拟)

    Problem Description On the way to school, Karen became fixated on the puzzle game on her phone! The ...

  6. Codeforces 816C/815A - Karen and Game

    传送门:http://codeforces.com/contest/816/problem/C 本题是一个模拟问题. 有一个n×m的矩阵.最初,这个矩阵为零矩阵O.现有以下操作: a.行操作“row  ...

  7. 【codeforces 816C】Karen and Game

    [题目链接]:http://codeforces.com/contest/816/problem/C [题意] 给你一个n*m的矩阵; 一开始所有数字都是0; 每次操作,你能把某一行,或某一列的数字全 ...

  8. Codeforces 959 树构造 暴力求最小字典序互质序列

    A B C 题目给你一个结论 最少需要min((odd,even)个结点可以把一棵树的全部边连起来 要求你输出两颗树 一棵树结论是正确的 另外一棵结论是正确的 正确结论的树很好造 主要是错误的树 题目 ...

  9. Codeforces 1138B Circus (构造方程+暴力)

    题意: 给你两个01串,要你选n/2个位置,使得选的位置在s1中"1"的数量等于未选的s2中"1"的数量 n<=5000,1s 思路: 设两个串中出现&q ...

随机推荐

  1. ElementUI在IE11下兼容性修改

    1.在项目里面使用了axios.js来发送http请求,在IE下报错Promise未定义,解决办法: 到http://bluebirdjs.com/docs/getting-started.html  ...

  2. Python3中操作字符串str必须记住的几个方法

    几个Python的字符串常用内建函数 1.方法:Python3 isdigit()方法 描述:Python isdigit() 方法检测字符串是否只由数字组成. 语法:str.isdigit() 参数 ...

  3. Sudoku 小项目

    Sudoku 小项目 - 软工第二次作业 Part 1 · 项目相关 Github 地址: https://github.com/TheSkyFucker/Sudoku 项目的更多信息以及所有开发文档 ...

  4. Python虚拟环境笔记

    虚拟环境 为什么需要虚拟环境: 到目前位置,我们所有的第三方包安装都是直接通过pip install xx的方式进行安装的,这样安装会将那个包安装到你的系统级的Python环境中.但是这样有一个问题, ...

  5. JavaScript的内置对象(Math对象)

    Math对象概述 Math(算数)对象的作用是:执行常见的算数任务.保存数学公式和信息. 与我们在JavaScript 直接编写计算功能相比,Math 对象提供的计算功能执行起来要快得多. Math ...

  6. XPATH如何选择不包含某一个属性的节点?

    XPATH如何选择不包含某一个属性的节点?今天博主在写一个爬虫的时候就碰到了这个问题. 我们知道选择包含某一特定属性的节点,可以使用例如//tbody/tr[@class]来选择.那么不含某属性的节点 ...

  7. 用powershell实现自动化操作

    每天登录OA太繁琐,公司OA又只允许用IE,本身写chrome扩展水平也不高,更搞不懂selenium 既然是windows下工作,当然还得微软的东东.研究了几天,才发现用powershell就很方便 ...

  8. 转://因触发器限制导致oracle用户登录失败

    使用PL/SQL DEV登录数据库时,出现如下错误 手工创建了test用户,通过dev工具登录没问题.怀疑数据库中有些用户限制了登录的.再看错误编号:ORA-20001,oracle保留的异常错误号范 ...

  9. 【PHP】最详细PHP从入门到精通(一)——想学习PHP的朋友们福利来了!

     PHP从入门到精通 (一)PHP简介和基本知识 PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java ...

  10. java 1.5 自动拆箱和装箱的注意事项

    背景 java1.5后引入了自动装箱和自动拆箱的概念 自动拆箱:将引用类型转化为基本数据类型 自动装箱:将基本数据类型装为引用类型 但是实际使用中,什么情况自动拆箱什么情况自动装箱呢? 自动装箱 In ...