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 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
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 1, col 2, col 3.

题解:

这道题首先想过网络流,然而没有什么用。

后来发现贪心可以过,求出每行每列的最小值,然后一个一个找,从行开始,每次维护一下列的最小值,最后统计一下值的总合是不是和之前的相同,不是的话就说明还有残余,如样例2。是的话就输出结果。

注:这个代码有问题,正解在最下面

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
int s[][],n,m,sum,sum2;
int mminn[],mminm[];
int ans1[],cnt,ans2[];
int main()
{
int i,j;
memset(mminn,/,sizeof(mminn));
memset(mminm,/,sizeof(mminm));
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
scanf("%d",&s[i][j]);
sum+=s[i][j];
}
}
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
mminn[i]=min(mminn[i],s[i][j]);
}
for(j=;j<=m;j++)
{
for(i=;i<=n;i++)
mminm[j]=min(mminm[j],s[i][j]);
}
for(i=;i<=n;i++)
{
ans1[i]=mminn[i];
for(j=;j<=m;j++)
{
s[i][j]-=mminn[i];
mminm[j]=min(mminm[j],s[i][j]);
}
}
for(j=;j<=m;j++)
{
ans2[j]=mminm[j];
}
for(i=;i<=n;i++)
{
sum2+=ans1[i]*m;
}
for(i=;i<=m;i++)
{
sum2+=ans2[i]*n;
}
if(sum==sum2)
{
for(i=;i<=n;i++)
if(ans1[i])cnt+=ans1[i];
for(i=;i<=m;i++)
if(ans2[i])cnt+=ans2[i];
printf("%d\n",cnt);
for(i=;i<=n;i++)
{
for(j=;j<=ans1[i];j++)
printf("row %d\n",i);
}
for(i=;i<=m;i++)
{
for(j=;j<=ans2[i];j++)
printf("col %d\n",i);
}
}
else cout<<"-1";
return ;
}

没错,这份代码确实有问题,很显然随便设计一组测试数据

input

4 3

1 1 1

1 1 1

1 1 1

1 1 1

output

3

col 1

col 2

col 3

而上面这份代码会输出

output

4

row 1

row 2

row 3

row 4

然后就WA掉了,(心痛)。但是修改也很简单,最后判断一下是从行还是从列开始就可以了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
int s[][],n,m,sum,sum2;
int mminn[],mminm[];
int ans1[],cnt,ans2[];
int main()
{
int i,j;
memset(mminn,/,sizeof(mminn));
memset(mminm,/,sizeof(mminm));
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
scanf("%d",&s[i][j]);
sum+=s[i][j];
}
}
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
mminn[i]=min(mminn[i],s[i][j]);
}
for(j=;j<=m;j++)
{
for(i=;i<=n;i++)
mminm[j]=min(mminm[j],s[i][j]);
}
if(m>n)
{
for(i=;i<=n;i++)
{
ans1[i]=mminn[i];
for(j=;j<=m;j++)
{
s[i][j]-=mminn[i];
mminm[j]=min(mminm[j],s[i][j]);
}
}
for(j=;j<=m;j++)
{
ans2[j]=mminm[j];
}
for(i=;i<=n;i++)
{
sum2+=ans1[i]*m;
}
for(i=;i<=m;i++)
{
sum2+=ans2[i]*n;
}
if(sum==sum2)
{
for(i=;i<=n;i++)
if(ans1[i])cnt+=ans1[i];
for(i=;i<=m;i++)
if(ans2[i])cnt+=ans2[i];
printf("%d\n",cnt);
for(i=;i<=n;i++)
{
for(j=;j<=ans1[i];j++)
printf("row %d\n",i);
}
for(i=;i<=m;i++)
{
for(j=;j<=ans2[i];j++)
printf("col %d\n",i);
}
}
else cout<<"-1";
}
else
{
for(i=;i<=m;i++)
{
ans2[i]=mminm[i];
for(j=;j<=n;j++)
{
s[j][i]-=mminm[i];
mminn[j]=min(mminn[j],s[j][i]);
}
}
for(j=;j<=n;j++)
{
ans1[j]=mminn[j];
}
for(i=;i<=n;i++)
{
sum2+=ans1[i]*m;
}
for(i=;i<=m;i++)
{
sum2+=ans2[i]*n;
}
if(sum==sum2)
{
for(i=;i<=n;i++)
if(ans1[i])cnt+=ans1[i];
for(i=;i<=m;i++)
if(ans2[i])cnt+=ans2[i];
printf("%d\n",cnt);
for(i=;i<=n;i++)
{
for(j=;j<=ans1[i];j++)
printf("row %d\n",i);
}
for(i=;i<=m;i++)
{
for(j=;j<=ans2[i];j++)
printf("col %d\n",i);
}
}
else cout<<"-1";
}
return ;
}

C. Karen and Game的更多相关文章

  1. B. Karen and Coffee

    B. Karen and Coffee time limit per test 2.5 seconds memory limit per test 512 megabytes input standa ...

  2. A. Karen and Morning

    A. Karen and Morning time limit per test 2 seconds  memory limit per test 512 megabytes input standa ...

  3. CodeForces 816B Karen and Coffee(前缀和,大量查询)

    CodeForces 816B Karen and Coffee(前缀和,大量查询) Description Karen, a coffee aficionado, wants to know the ...

  4. codeforces 815C Karen and Supermarket

    On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...

  5. codeforces round #419 E. Karen and Supermarket

    On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...

  6. Codeforces Round #460 D. Karen and Cards

    Description Karen just got home from the supermarket, and is getting ready to go to sleep. After tak ...

  7. codeforces round #419 C. Karen and Game

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

  8. codeforces round #419 B. Karen and Coffee

    To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...

  9. codeforces round #419 A. Karen and Morning

    Karen is getting ready for a new school day! It is currently hh:mm, given in a 24-hour format. As yo ...

随机推荐

  1. Unity 多屏(分屏)显示,Muti_Display

    Unity 多屏(分屏)显示,Muti_Display  最近项目有个需求,主要用于在展厅的展示游戏. 比如,在一个很大的展厅,很大的显示屏挂在墙上,我们不可能通过操作墙上那块显示器上的按钮来控制游戏 ...

  2. openlayers应用(二):加载百度离线瓦片

    上一篇文章介绍了使用openlayers3加载百度在线地图,对某些项目或应用场景比如不允许上外网的单位,某些项目只针对一定区域地图加载应用,比如一个县的地图,可以采用下载百度瓦片地图,在服务器或者本机 ...

  3. dotNet Linux 学习记录 - Jexus寄宿WCF服务

    让WCF运行在Linux上(寄宿于服务器程序) WCF介绍请自行 bing 搜索 使用的开发工具为vs2017,系统为 Ubuntu16.04 服务器软件为Jexus ( 详情请看:  Jexus官网 ...

  4. GPIO控制LED

    一.LED灯的亮灭控制的配置步骤(GPIO4_D3): 1.GPIO4的CRU_CLKGATE_CON31时钟使能配置(用来保证GPIO4可写): io -4 0xff76037c 0xffff019 ...

  5. 20170515-20170523学习计划---学习java(1)

    背景: 组内搞了一个读书会,我是负责人,我们学习的主题是java,为了更好服务读书会,所以安排一下学习计划,到时候在读书会上做分享. 这是第二次读书会,第一次讲了java的一些基本知识,这次只要学习对 ...

  6. php 关于经纬度距离计算方法 成功版

    1.PHP实现通过经纬度计算距离 单位为公里 function getdistance($lng1,$lat1,$lng2,$lat2)//根据经纬度计算距离 { //将角度转为狐度  $radLat ...

  7. LINQ之LINQ to Objects(上)

    LINQ概述 LINQ,语言集成查询(Language Integrated Query),它允许使用C#或VB代码以查询数据库相同的方式来操作不同的数据源. 1.LINQ体系结构 从上图可以看出,L ...

  8. .Net程序员学用Oracle系列(8):触发器、作业、序列、连接

    1.触发器 2.作业 2.1.作业调度功能和应用 2.2.通过 DBMS_JOB 来调度作业 3.序列 3.1.创建序列 3.2.使用序列 & 删除序列 4.连接 4.1.创建连接 4.2.使 ...

  9. kafka 0.8.2 消息消费者 consumer

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  10. android学习-第一讲

    一.基础View控件 View类的常见XML属性,对应发放及说明 每个界面控件都需要设置Android:layout_height,Android:layout_width,指定控件的高度和宽度.通常 ...