C. Karen and Game【模拟+思维】

来源:互联网 发布:金融直播软件 编辑:程序博客网 时间:2024/06/09 03:50

C. Karen and Game
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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 52 2 2 3 20 0 0 1 01 1 1 2 1
output
4row 1row 1col 4row 3
input
3 30 0 00 1 00 0 0
output
-1
input
3 31 1 11 1 11 1 1
output
3row 1row 2row 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 1col 2col 3.

题意:根据题目所提供的目标矩阵,你每次可以操作,每行或者每列全体加1(没有减1的操作),问是否能经过操作后达到目标矩阵;

思路:一道大模拟题,刚开始只考虑到添加列,其实应该行和列分别考虑,取最小值。无论是考虑行还是考虑列,一个道理,矩阵颠倒一下就行;

介绍从列添加的做法:

统计每行的最小值,超出最小值的部分,都应该通过添加列的方式弥补。

统计每列需要添加的最大值和该行的最小值相加,最后和目标矩阵对比;

//按照从列添加的方法 :mp1数组存储生成的矩阵,和mp初数组进行对比;//a数组代表每行的最小值,b数组代表每列需要添加的最大值(而不是每列的最大值 )//按照从列行加的方法 :mp2数组存储生成的矩阵,和mp初数组进行对比;//c数组代表每列的最小值,d数组代表每行需要添加的最大值(而不是每行的最大值 )#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define max_n 510typedef long long LL;using namespace std;int mp[max_n][max_n],mp1[max_n][max_n],mp2[max_n][max_n],a[max_n],b[max_n],c[max_n],d[max_n];int main(){int n,m,t2=0,t1=0;scanf("%d %d",&n,&m);for(int i=1;i<=n;i++){a[i]=1010;for(int j=1;j<=m;j++){scanf("%d",&mp[i][j]);a[i]=min(a[i],mp[i][j]);}t1+=a[i];}for(int j=1;j<=m;j++){for(int i=1;i<=n;i++){mp1[i][j]=a[i];if(mp[i][j]>mp1[i][j]){b[j]=max(b[j],mp[i][j]-mp1[i][j]);}}t1+=b[j];}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){mp1[i][j]+=b[j];}}bool flag1=true,flag2=true;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(mp[i][j]!=mp1[i][j]){flag1=false;break;}}if(!flag1) break;}for(int j=1;j<=m;j++){c[j]=1010;for(int i=1;i<=n;i++){c[j]=min(c[j],mp[i][j]);}t2+=c[j];}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){mp2[i][j]=c[j];if(mp[i][j]>mp2[i][j]){d[i]=max(d[i],mp[i][j]-mp2[i][j]);}}t2+=d[i];}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){mp2[i][j]+=d[i];}}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(mp[i][j]!=mp2[i][j]){flag2=false;break;}}if(!flag2) break;}if(!flag1 && !flag2) printf("-1\n");else{if(t1<=t2){printf("%d\n",t1);for(int i=1;i<=n;i++){if(a[i]){for(int j=0;j<a[i];j++)printf("row %d\n",i);}}for(int i=1;i<=m;i++){if(b[i]){for(int j=0;j<b[i];j++)printf("col %d\n",i);}}}else{printf("%d\n",t2);for(int i=1;i<=n;i++){if(d[i]){for(int j=0;j<d[i];j++)printf("row %d\n",i);}}for(int i=1;i<=m;i++){if(c[i]){for(int j=0;j<c[i];j++)printf("col %d\n",i);}}}}return 0;}

原创粉丝点击