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!
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).
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.
3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1
4 row 1 row 1 col 4 row 3
3 3 0 0 0 0 1 0 0 0 0
-1
3 3 1 1 1 1 1 1 1 1 1
3 row 1 row 2 row 3
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.
关键点:(参考dalao的代码写出来的)
1、当这一行(/列)中有0,则这一行(/列)不能再走
2、行走的次序与结果无关,所以我们可以先走完行的再走列的。走完行后,因为只进行列行走,所以每一列中的每个剩余步数必须相等,否则直接输出-1;同理,走完列后,因为只进行行行走,所以每一行中的每个剩余步数必须相等,否则直接输出-1。
3、由上述两点可以判断是否有结果,但是题目要求所走的总步数最少。所以,问题变成“是先走行还是先走列?”“行行走和列行走要走到什么程度停止?”
4、我们在每个格子中走的总步数不变。所以,在行走时,尽量使此次行走过程中每个格子的总步数达到最大。所以,当n<m时(即行行走经过的格子数多于列行走的格子数时),先进行行行走,直到该行中的某一个格子中的数为0(即该行无法再行走),再进行列行走且行行走时。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn=110;
const int inf=550;
int grh[maxn][maxn];
int ansr[maxn],ansc[maxn];
void work(int n,int m)
{
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
scanf("%d",&grh[i][j]);
if(n<=m)
{
for(int i=0; i<n; i++)
{
ansr[i]=inf;
for(int j=0; j<m; j++)
{
if(grh[i][j]<ansr[i])
ansr[i]=grh[i][j];
}
for(int j=0; j<m; j++)
grh[i][j]-=ansr[i];
}
for(int j=0; j<m; j++)
ansc[j]=grh[0][j];
for(int i=1; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(grh[i][j]!=ansc[j])
{
printf("-1\n");
return;
}
}
}
}
else
{
for(int j=0;j<m;j++)
{
ansc[j]=inf;
for(int i=0;i<n;i++)
{
if(grh[i][j]<ansc[j])
ansc[j]=grh[i][j];
}
for(int i=0;i<n;i++)
grh[i][j]-=ansc[j];
}
for(int i=0;i<n;i++)
ansr[i]=grh[i][0];
for(int j=1;j<m;j++)
{
for(int i=0;i<n;i++)
{
if(grh[i][j]!=ansr[i])
{
printf("-1\n");
return;
}
}
}
}
int ans=0;
for(int i=0;i<n;i++) ans+=ansr[i];
for(int i=0;i<m;i++) ans+=ansc[i];
printf("%d\n",ans);
for(int i=0;i<n;i++)
{
for(int k=0;k<ansr[i];k++)
printf("row %d\n",i+1);
}
for(int i=0;i<m;i++)
{
for(int k=0;k<ansc[i];k++)
printf("col %d\n",i+1);
}
}
int main()
{
#ifdef LOCAL
freopen("input.in","r",stdin);
// freopen("temp.in","w",stdout);
#endif // LOCAL
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(grh,0,sizeof(grh));
memset(ansr,0,sizeof(ansr));
memset(ansc,0,sizeof(ansc));
work(n,m);
}
return 0;
}
//cout<<"NO"<<endl;cout<<"YES"<<endl;