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:

  • rowx, (1 ≤ x ≤ n) describing a move of the form "choose the x-th row".
  • colx, (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.

Sample Input

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

Hint

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.

  分析这种棋盘的特性,要加加一行,要加加一列,且不能减,且棋盘初始都为零。所以如果同一行上出现了不同的数,则数大的一列必然有该列的加,行同理。

所以可以先处理列,把所有列加过的都还原回去,所以只剩下了由个别的行改变所导致的棋盘,再用同样的方法将行还原回去,最后如果棋盘仍然不为零的话,说明所有的行都进行过相同次数的

加(这里要注意!!!如果最后棋盘不为零的话,既有可能是所有的行进行了操作,也有可能是所有的列进行了操作,因为题目让求最少的操作次数,所以应判断行和列谁小操作的谁)。

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
const double e=exp();
const int N = ; LL con[][];
LL check[][];
LL col[],row[]; int main()
{
LL i,p,j,m,n;
LL flag=;
scanf("%lld%lld",&m,&n);
LL head=,tail=,mid; for(i=; i<=m; i++)
{
for(j=; j<=n; j++)
{
scanf("%lld",&con[i][j]);
if(con[][j]<head)
head=con[][j];
if(con[i][]<tail)
tail=con[i][];
}
} if(m<=n)
{
for(i=; i<=n; i++)
{
if(con[][i]>head)
col[i]+=con[][i]-head;
}
mid=tail-col[];
for(i=; i<=m; i++)
{
if(con[i][]>tail)
row[i]+=con[i][]-tail;
row[i]+=mid;
}
}
else
{
for(i=; i<=m; i++)
{
if(con[i][]>tail)
row[i]+=con[i][]-tail;
}
mid=head-row[];
for(i=; i<=n; i++)
{
if(con[][i]>head)
col[i]+=con[][i]-head;
col[i]+=mid;
}
} flag=; for(i=; i<=n; i++)
{
if(col[i])
{
flag+=col[i];
for(j=; j<=m; j++)
check[j][i]+=col[i];
}
}
for(i=; i<=m; i++)
{
if(row[i])
{
flag+=row[i];
for(j=; j<=n; j++)
check[i][j]+=row[i];
}
} for(i=; i<=m; i++)
{
for(j=; j<=n; j++)
{
if(con[i][j]!=check[i][j])
break;
}
if(j<=n)
{
flag=-;
break;
}
} if(flag==-)
printf("-1\n");
else
{
printf("%lld\n",flag);
for(i=; i<=n; i++)
{
while(col[i])
{
col[i]--;
printf("col %lld\n",i);
}
}
for(i=; i<=m; i++)
{
while(row[i])
{
row[i]--;
printf("row %lld\n",i);
}
}
}
return ;
}

  

CodeForces 816C 思维的更多相关文章

  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 424A (思维题)

    Squats Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  3. Codeforces 1060E(思维+贡献法)

    https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...

  4. Queue CodeForces - 353D (思维dp)

    https://codeforces.com/problemset/problem/353/D 大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧 $dp[i]$为 ...

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

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

  6. 【codeforces 816C】Karen and Game

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

  7. codeforces 1244C (思维 or 扩展欧几里得)

    (点击此处查看原题) 题意分析 已知 n , p , w, d ,求x , y, z的值 ,他们的关系为: x + y + z = n x * w + y * d = p 思维法 当 y < w ...

  8. CodeForces - 417B (思维题)

    Crash Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status ...

  9. CodeForces - 417A(思维题)

    Elimination Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit  ...

随机推荐

  1. SCRIPT7002: XMLHttpRequest: 网络错误 0x2efe, 由于出现错误 00002efe 而导致此项操作无法完成

    google中带中文参数可能查询,但是在IE带中文参数不能查询:报如下错误 SCRIPT7002: XMLHttpRequest: 网络错误 0x2efe, 由于出现错误 00002efe 而导致此项 ...

  2. [转帖]通俗解释 AWS 云服务每个组件的作用

    你有听说过 ContainerCache,ElastiCast 和 QR72 这些 AWS 的新服务吗? 没有就对了,这些都是我编的:) 不过,AWS 有 50 多个服务,从名称也不能看出这些服务是做 ...

  3. override toString() function for TreeNode to output OJ's Binary Tree Serialization

    class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } @Override publ ...

  4. MyFlash闪回恢复数据

    使用限制: .binlog格式必须为row,且binlog_row_image=full. .仅支持5.6与5.. .只能回滚DML(增.删.改). .mysqlbinlog版本请保持一致. 1.安装 ...

  5. BZOJ5308 ZJOI2018胖

    贝尔福特曼(?)的方式相当于每次将所有与源点直接相连的点的影响区域向两边各扩展一格.显然每个点在过程中最多更新其他点一次且这些点构成一段连续区间.这个东西二分st表查一下就可以了.注意某一轮中两点都更 ...

  6. P3119 [USACO15JAN]草鉴定Grass Cownoisseur

    题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...

  7. 【比赛】NOIP2017 宝藏

    这道题考试的时候就骗了部分分.其实一眼看过去,n范围12,就知道是状压,但是不知道怎么状压,想了5分钟想不出来就枪毙了状压,与AC再见了. 现在写的是状压搜索,其实算是哈希搜索,感觉状压DP理解不了啊 ...

  8. Vue设置页面的title

    原文地址:http://www.cnblogs.com/JimmyBright/p/7410771.html 前端框架如Vue.React等都是单页面的应用,也就是说整个web站点其实都是一个inde ...

  9. BZOJ2437 [Noi2011]兔兔与蛋蛋 【博弈论 + 二分图匹配】

    题目链接 BZOJ2437 题解 和JSOI2014很像 只不过这题动态删点 如果我们把空位置看做\(X\)的话,就会发现我们走的路径是一个\(OX\)交错的路径 然后将图二分染色,当前点必胜,当且仅 ...

  10. Go interface{}、类型断言

    在 golang 中 interface{} 可用于向函数传递任意类型的变量, 但在函数内部使用的话, 该变量的类型就是 interface{}, 也称为空接口类型 比如我们定义一个函数, 输出字符串 ...