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
每次可以令一行或者一列加1,问你达到题目给的局面最少多少步。
一开始是 先去行的最小值再取列-行的最大值。然后全部加上,看是否符合,符合的话
输出步骤就行,然后被hack掉,比赛还有20分钟的时候发现了一个坑点,就是可能先找列
的最小值和行-列的最大值会导致步数不一样,或者一些局面单方向无法到达。然后改改
= = 可能是急了 没改好,今天早上一改果然过了
#include <bits/stdc++.h>
using namespace std;
int mp[1010][1010];
int vis[1010][1010];
int mm[1010][1010];
vector<int> v;
int mmm[1010][1010];
int minn[1010];
int col[1010];
int col1[1010];
int minn1[1010];
const int inf = 0x3f3f3f3f;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
memset(minn,inf,sizeof(minn));
int sum=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&mp[i][j]);
minn[i]=min(minn[i],mp[i][j]);
}
sum+=minn[i];
}
int flag=0,flag1=0;
for(int j=1;j<=m;j++)
{
for(int i=1;i<=n;i++)
{
mm[i][j]=minn[i];
if(mp[i][j]>mm[i][j])
{
col[j]=max(col[j],mp[i][j]-mm[i][j]);
}
}
sum+=col[j];
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
mm[i][j]+=col[j];
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(mm[i][j]!=mp[i][j])
{
flag=1;
break;
}
}
if(flag) break;
}
int sum1=0;
memset(col1,inf,sizeof(col1));
for(int j=1;j<=m;j++)
{
for(int i=1;i<=n;i++)
{
col1[j]=min(col1[j],mp[i][j]);
}
sum1+=col1[j];
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
mmm[i][j]=col1[j];
if(mp[i][j]>mmm[i][j])
{
minn1[i]=max(minn1[i],mp[i][j]-mmm[i][j]);
}
}
sum1+=minn1[i];
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
mmm[i][j]+=minn1[i];
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(mm[i][j]!=mp[i][j])
{
flag1=1;
break;
}
}
if(flag1) break;
}
if(flag&&flag1)
{
printf("-1\n");
return 0;
}
if(sum<sum1)
{
printf("%d\n",sum );
for(int i=1;i<=n;i++)
{
if(minn[i]!=0)
{
while(minn[i]--)
printf("row %d\n",i );
}
}
for(int i=1;i<=m;i++)
{
if(col[i]!=0)
{
while(col[i]--)
printf("col %d\n",i );
}
}
}
else
{
printf("%d\n",sum1 );
for(int i=1;i<=n;i++)
{
if(minn1[i]!=0)
{
while(minn1[i]--)
printf("row %d\n",i );
}
}
for(int i=1;i<=m;i++)
{
if(col1[i]!=0)
{
while(col1[i]--)
printf("col %d\n",i );
}
}
}
}