博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C. Karen and Game
阅读量:6583 次
发布时间:2019-06-24

本文共 5483 字,大约阅读时间需要 18 分钟。

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
#include
#include
#include
#include
#include
using namespace std;int s[101][101],n,m,sum,sum2;int mminn[101],mminm[101];int ans1[101],cnt,ans2[101];int main(){ int i,j; memset(mminn,127/3,sizeof(mminn)); memset(mminm,127/3,sizeof(mminm)); scanf("%d%d",&n,&m); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { scanf("%d",&s[i][j]); sum+=s[i][j]; } } for(i=1;i<=n;i++) { for(j=1;j<=m;j++) mminn[i]=min(mminn[i],s[i][j]); } for(j=1;j<=m;j++) { for(i=1;i<=n;i++) mminm[j]=min(mminm[j],s[i][j]); } for(i=1;i<=n;i++) { ans1[i]=mminn[i]; for(j=1;j<=m;j++) { s[i][j]-=mminn[i]; mminm[j]=min(mminm[j],s[i][j]); } } for(j=1;j<=m;j++) { ans2[j]=mminm[j]; } for(i=1;i<=n;i++) { sum2+=ans1[i]*m; } for(i=1;i<=m;i++) { sum2+=ans2[i]*n; } if(sum==sum2) { for(i=1;i<=n;i++) if(ans1[i])cnt+=ans1[i]; for(i=1;i<=m;i++) if(ans2[i])cnt+=ans2[i]; printf("%d\n",cnt); for(i=1;i<=n;i++) { for(j=1;j<=ans1[i];j++) printf("row %d\n",i); } for(i=1;i<=m;i++) { for(j=1;j<=ans2[i];j++) printf("col %d\n",i); } } else cout<<"-1"; return 0;}

 

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

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
#include
#include
#include
#include
#include
using namespace std;int s[101][101],n,m,sum,sum2;int mminn[101],mminm[101];int ans1[101],cnt,ans2[101];int main(){ int i,j; memset(mminn,127/3,sizeof(mminn)); memset(mminm,127/3,sizeof(mminm)); scanf("%d%d",&n,&m); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { scanf("%d",&s[i][j]); sum+=s[i][j]; } } for(i=1;i<=n;i++) { for(j=1;j<=m;j++) mminn[i]=min(mminn[i],s[i][j]); } for(j=1;j<=m;j++) { for(i=1;i<=n;i++) mminm[j]=min(mminm[j],s[i][j]); } if(m>n) { for(i=1;i<=n;i++) { ans1[i]=mminn[i]; for(j=1;j<=m;j++) { s[i][j]-=mminn[i]; mminm[j]=min(mminm[j],s[i][j]); } } for(j=1;j<=m;j++) { ans2[j]=mminm[j]; } for(i=1;i<=n;i++) { sum2+=ans1[i]*m; } for(i=1;i<=m;i++) { sum2+=ans2[i]*n; } if(sum==sum2) { for(i=1;i<=n;i++) if(ans1[i])cnt+=ans1[i]; for(i=1;i<=m;i++) if(ans2[i])cnt+=ans2[i]; printf("%d\n",cnt); for(i=1;i<=n;i++) { for(j=1;j<=ans1[i];j++) printf("row %d\n",i); } for(i=1;i<=m;i++) { for(j=1;j<=ans2[i];j++) printf("col %d\n",i); } } else cout<<"-1"; } else { for(i=1;i<=m;i++) { ans2[i]=mminm[i]; for(j=1;j<=n;j++) { s[j][i]-=mminm[i]; mminn[j]=min(mminn[j],s[j][i]); } } for(j=1;j<=n;j++) { ans1[j]=mminn[j]; } for(i=1;i<=n;i++) { sum2+=ans1[i]*m; } for(i=1;i<=m;i++) { sum2+=ans2[i]*n; } if(sum==sum2) { for(i=1;i<=n;i++) if(ans1[i])cnt+=ans1[i]; for(i=1;i<=m;i++) if(ans2[i])cnt+=ans2[i]; printf("%d\n",cnt); for(i=1;i<=n;i++) { for(j=1;j<=ans1[i];j++) printf("row %d\n",i); } for(i=1;i<=m;i++) { for(j=1;j<=ans2[i];j++) printf("col %d\n",i); } } else cout<<"-1"; } return 0;}

 

转载于:https://www.cnblogs.com/huangdalaofighting/p/7042669.html

你可能感兴趣的文章
转 我们工作的动力是什么 工作最终是为了什么?
查看>>
测试一个网站的最大并发量并发数并发用户
查看>>
适配器模式(数据库方面)支持不同的数据库连接
查看>>
CF456B Fedya and Maths 找规律
查看>>
nodejs安装及windows环境配置
查看>>
转载:Beginning WF 4.0翻译——第三章(流程图工作流)
查看>>
mysql alter table
查看>>
芯片测试
查看>>
在源代码中插入防止盗版代码片段的方式
查看>>
hdu 3367 Pseudoforest(最大生成树)
查看>>
一个人,一则故事,一份情愫,一个世界……
查看>>
ffserver联合ffmpeg建立媒体服务器
查看>>
下载稻草人下来刷新+gallery
查看>>
删除浏览器浏览器删除cookie方法
查看>>
微软URLRewriter.dll的url重写的简单使用(实现伪静态)
查看>>
leetcode -- Combination Sum II
查看>>
1z0-052 q209_7
查看>>
PIN码计算锦集
查看>>
[Unity3D]再次点击以退出程序
查看>>
架构师的97种习惯
查看>>