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. 分针网—IT教育:作为PHP开发人员容易忽视的几个重点

    无论是学习什么样的一个开发.ASP开发.java开发.当学习还不是很久的时候,一般都是不知道它们的精华是在哪里,而现在很多的php程序员也是不知道PHP的精华所在,为什么perl在当年在商界如此的出名 ...

  2. TPshop分销功能的使用与表设计

    首先来段科普,摘自百度百科: 在西方经济学中,分销 的含义是建立销售渠道的意思,根据著名的营销大师菲利普·科特勒的定义,分销渠道(Distribution Channel)又或者叫营销渠道(Marke ...

  3. linux文件系统下的特殊权限

    SUID, SGID, Sticky 1 权限 r, w, x user, group, other 2 安全上下文 前提:进程有属主和属组:文件有属主和属组: (1) 任何一个可执行程序文件能不能启 ...

  4. java泛型探索——介绍篇

    1. 泛型出现前后代码对比 先来看看泛型出现前,代码是这么写的: List words = new ArrayList(); words.add("Hello "); words. ...

  5. ThreadGroup详解

     ①定义线程组 ThreadGroup类中有 2个构造方法,它们用来定义线程组.这 2个构造方法的使用格 式如下: public ThreadGroup(String name); public Th ...

  6. dubbo+zookeeper+springmvc+mybatis+shiro+redis架构

    内容管理(CMS)系统,包括内容管理,栏目管理.站点管理.公共留言.文件管理.前端网站展示等功能: 在线办公(OA)系统,主要提供简单的流程实例. Jeesz提供了常用工具进行封装,包括日志工具.缓存 ...

  7. 初识数据库连接池开源框架Druid

    Druid是阿里巴巴的一个数据库连接池开源框架,准确来说它不仅仅包括数据库连接池这么简单,它还提供强大的监控和扩展功能.本文仅仅是在不采用Spring框架对Druid的窥探,采用目前最新版本druid ...

  8. VR全景加盟-了解VR就来全景智慧城市

    关于什么是真正的VR说了这么多,面对刚刚起步的VR,如何辨别判断一个真正的VR形式呢.除了我们所说几个参数或者大家关注的眩晕感.临场感,真正的VR究竟带给大家什么样的特性呢?这个就要从VR的本质谈起. ...

  9. apply/call/bind和this的使用

    fun.apply(context,[argsArray]) 立即调用fun,同时将fun函数原来的this指向传入的新context对象,实现同一个方法在不同对象上重复使用. context:传入的 ...

  10. Handler线程间通信

    package com.hixin.appexplorer; import java.util.List; import android.app.Activity; import android.ap ...