您的位置:首页 > 其它

Karen and Game

2017-07-28 14:40 351 查看
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 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

这道题的大意就是一个m*n的二维数组初始为0,问最少经过几次操作(每次操作可以每一行或每一列对应的每个数加1)得到给出的二维数组并输出操作过程(多种可能性输出任意一种即可)。

如果列数大于行数,说明是一个扁的二维数组,找到每一行的最小值并记录(用于对行的输出),把该行所有数减去这个最小值;现在每一行的每个数之和应该相等,[若不相等输出-1(证明不能操作成功)]只看第一行进行对列的输出即可。

如果行数大于列数,说明是一个瘦高的二维数组,进行类似的操作即可。

#include<iostream>
#include<string.h>
using namespace std;
int grid[510][510];
int mmin[510],sum[510];
int main(){
int col,roo;
while(cin>>roo>>col){
memset(grid,0,sizeof(grid));
memset(sum,0,sizeof(sum));
for(int i=0;i<510;i++)
mmin[i]=9999;
for(int i=0;i<roo;i++)
for(int j=0;j<col;j++)
cin>>grid[i][j];

int chose=max(roo,col);
int ans=0;
if(chose==col){
for(int i=0;i<roo;i++)
for(int j=0;j<col;j++)
if(grid[i][j]<mmin[i]) mmin[i]=grid[i][j];
for(int i=0;i<roo;i++){
ans=ans+mmin[i];
for(int j=0;j<col;j++){
grid[i][j]=grid[i][j]-mmin[i];
sum[i]=sum[i]+grid[i][j];
}
}
for(int i=1;i<roo;i++)
if(sum[i]!=sum[i-1]){
ans=-1;break;
}
if(ans==-1){
cout<<"-1"<<endl;
continue;
}
for(int j=0;j<col;j++){
if(grid[0][j]!=0){
ans=ans+grid[0][j];
}
}
cout<<ans<<endl;
for(int i=0;i<roo;i++){
for(int j=0;j<mmin[i];j++)
cout<<"row "<<i+1<<endl;
}
for(int j=0;j<col;j++){
if(grid[0][j]!=0){
for(int i=0;i<grid[0][j];i++){
cout<<"col "<<j+1<<endl;
}
}
}
}
else{

for(int i=0;i<col;i++)
for(int j=0;j<roo;j++)
if(grid[j][i]<mmin[i]) mmin[i]=grid[j][i];
for(int i=0;i<col;i++){
ans=ans+mmin[i];
for(int j=0;j<roo;j++){
grid[j][i]=grid[j][i]-mmin[i];
sum[i]=sum[i]+grid[j][i];
}
}
for(int i=1;i<col;i++)
if(sum[i]!=sum[i-1]){
ans=-1;break;
}
if(ans==-1){
cout<<"-1"<<endl;
continue;
}
for(int j=0;j<roo;j++){
if(grid[j][0]!=0){
ans=ans+grid[j][0];
}
}
cout<<ans<<endl;
for(int i=0;i<col;i++){
for(int j=0;j<mmin[i];j++)
cout<<"col "<<i+1<<endl;
}
for(int j=0;j<roo;j++){
if(grid[j][0]!=0){
for(int i=0;i<grid[j][0];i++){
cout<<"row "<<j+1<<endl;
}
}
}
}
}

return 0;
}


然而这种写法代码冗长,而且写循环控制位置的时候容易出错,于是想到了二维数组的转置。

#include<iostream>
#include<string.h>
using namespace std;
int grid[510][510];
int temp[510][510];
int mmin[510],sum[510];
int main(){
int col,roo;
while(cin>>roo>>col){
memset(grid,0,sizeof(grid));
memset(sum,0,sizeof(sum));
for(int i=0;i<510;i++)
mmin[i]=9999;
for(int i=0;i<roo;i++)
for(int j=0;j<col;j++)
cin>>temp[i][j];

int chose=max(roo,col);
int ans=0;
int det=0;
if(chose==roo){
//转置并交换行列数 并为转置变量赋值为1
for(int i=0;i<roo;i++)
for(int j=0;j<col;j++)
grid[j][i]=temp[i][j];
int tttemp=col;col=roo;roo=tttemp;
det=1;

}
else
for(int i=0;i<roo;i++)
for(int j=0;j<col;j++)
grid[i][j]=temp[i][j];//直接赋值过去

//开始计算
for(int i=0;i<roo;i++)
for(int j=0;j<col;j++)
if(grid[i][j]<mmin[i]) mmin[i]=grid[i][j];
for(int i=0;i<roo;i++){
ans=ans+mmin[i];
for(int j=0;j<col;j++){
grid[i][j]=grid[i][j]-mmin[i];
sum[i]=sum[i]+grid[i][j];
}
}
for(int i=1;i<roo;i++)
if(sum[i]!=sum[i-1]){
ans=-1;break;
}
if(ans==-1){
cout<<"-1"<<endl;
continue;
}
for(int j=0;j<col;j++){
if(grid[0][j]!=0){
ans=ans+grid[0][j];
}
}
cout<<ans<<endl;
for(int i=0;i<roo;i++){
for(int j=0;j<mmin[i];j++)
det?cout<<"col "<<i+1<<endl:cout<<"row "<<i+1<<endl; //判断是否转置过 控制相应输出 注意这里是i
}
for(int j=0;j<col;j++){
if(grid[0][j]!=0){
for(int i=0;i<grid[0][j];i++){
det?cout<<"row "<<j+1<<endl:cout<<"col "<<j+1<<endl;//判断是否转置过 控制相应输出 注意这里是j
}
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: