CodeForces - 816C Karen and Game(简单模拟)

Problem Description

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 52 2 2 3 20 0 0 1 01 1 1 2 1

output

4row 1row 1col 4row 3

input

3 30 0 00 1 00 0 0

output

-1

input

3 31 1 11 1 11 1 1

output

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

解题思路:贪心模拟,行操作完再进行列操作。注意:题目要求用最少的步数,因此当n>m时,应先对列进行贪心减操作,再对行进行操作;否则先对行进行贪心减操作,最后如果减不完,则直接输出-1.

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=105;
 4 const int inf=500;
 5 int n,m,cnt1,cnt2,ans,sum,mp[maxn][maxn],min_row[maxn],min_col[maxn];
 6 pair<int,int> paii_row[maxn],paii_col[maxn];
 7 int main(){
 8     while(cin>>n>>m){
 9         ans=sum=cnt1=cnt2=0;
10         for(int i=1;i<=n;++i)min_row[i]=inf;
11         for(int j=1;j<=m;++j)min_col[j]=inf;
12         for(int i=1;i<=n;++i){
13             for(int j=1;j<=m;++j){
14                 cin>>mp[i][j],sum+=mp[i][j];
15                 min_row[i]=min(min_row[i],mp[i][j]);
16                 min_col[j]=min(min_col[j],mp[i][j]);
17             }
18         }
19         if(n>m){///如果行大于列,则从列开始操作
20             for(int j=1;j<=m;++j){
21                 for(int i=1;i<=n;++i){
22                     mp[i][j]-=min_col[j];
23                     min_row[i]=min(min_row[i],mp[i][j]);
24                 }
25                 if(min_col[j])sum-=min_col[j]*n,ans+=(paii_col[cnt2].first=min_col[j]),paii_col[cnt2++].second=j,min_col[j]=0;
26             }
27             for(int i=1;i<=n;++i){///从行开始操作
28                 for(int j=1;j<=m;++j){
29                     mp[i][j]-=min_row[i];
30                     min_col[j]=min(min_col[j],mp[i][j]);
31                 }
32                 if(min_row[i])sum-=min_row[i]*m,ans+=(paii_row[cnt1].first=min_row[i]),paii_row[cnt1++].second=i,min_row[i]=0;
33             }
34         }
35         else{
36             for(int i=1;i<=n;++i){///否则先从行开始操作
37                 for(int j=1;j<=m;++j){
38                     mp[i][j]-=min_row[i];
39                     min_col[j]=min(min_col[j],mp[i][j]);
40                 }
41                 if(min_row[i])sum-=min_row[i]*m,ans+=(paii_row[cnt1].first=min_row[i]),paii_row[cnt1++].second=i,min_row[i]=0;
42             }
43             for(int j=1;j<=m;++j){
44                 for(int i=1;i<=n;++i){
45                     mp[i][j]-=min_col[j];
46                     min_row[i]=min(min_row[i],mp[i][j]);
47                 }
48                 if(min_col[j])sum-=min_col[j]*n,ans+=(paii_col[cnt2].first=min_col[j]),paii_col[cnt2++].second=j,min_col[j]=0;
49             }
50         }
51         if(sum)cout<<-1<<endl;
52         else{
53             cout<<ans<<endl;
54             for(int i=0;i<cnt1;++i)
55                 for(int j=1;j<=paii_row[i].first;++j)
56                     cout<<"row "<<paii_row[i].second<<endl;
57             for(int i=0;i<cnt2;++i)
58                 for(int j=1;j<=paii_col[i].first;++j)
59                     cout<<"col "<<paii_col[i].second<<endl;
60         }
61     }
62     return 0;
63 }

原文地址:https://www.cnblogs.com/acgoto/p/9966933.html

时间: 2024-10-07 11:09:14

CodeForces - 816C Karen and Game(简单模拟)的相关文章

Codeforces Round #259 (Div. 2) (简单模拟实现题)

题目链接:http://codeforces.com/problemset/problem/454/A A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine

Karen and Game CodeForces - 816C (暴力+构造)

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 mcolumns. Each cell originally contains the number 0. One move consists of choosing one row or c

Codeforces 309C Memory for Arrays 二进制模拟进位

题目链接:点击打开链接 题意: 给定n个箱子m个物品 下面n个数字表示箱子的容量 下面m个数字b1-bm 表示物品体积为2^bi大 问最多有多少个物品可以放入箱子. 思路: 贪心,先放小的,小的不能放再放大的 显然我们把n个箱子拆成二进制,然后模拟二进制减法运算. 剩下就是简单模拟 #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<m

HDU-1034-Candy Sharing Game(C++ &amp;&amp; 简单模拟)

Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3703    Accepted Submission(s): 2311 Problem Description A number of students sit in a circle facing their teacher in the cent

Jquery源码分析与简单模拟实现

前言 最近学习了一下jQuery源码,顺便总结一下,版本:v2.0.3 主要是通过简单模拟实现jQuery的封装/调用.选择器.类级别扩展等.加深对js/Jquery的理解. 正文 先来说问题: 1.jQuery为什么能使用$的方式调用,$是什么.$()又是什么.链式调用如何实现的 2.jQuery的类级别的扩展内部是怎样实现的,方法级别的扩展有是怎样实现的,$.fn又是什么 3.jQuery选择器是如何执行的,又是如何将结果包装并返回的 带着这些问题,我们进行jquery的模拟实现,文章下方有

Linux 内核 链表 的简单模拟(2)

接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the &struct list_head to use as a loop cursor. * @head: the head for your list. */ #define list_for_each(pos, head) for (pos = (head)->next; pos != (head);

HDU 1048 What Is Your Grade? (简单模拟)

 What Is Your Grade? Problem Description "Point, point, life of student!" This is a ballad(歌谣)well known in colleges, and you must care about your score in this exam too. How many points can you get? Now, I told you the rules which are used in

JavaWeb学习总结(四十九)——简单模拟Sping MVC

在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: 1 /** 2 * 使用Controller注解标注LoginUI类 3 */ 4 @Controller 5 public class LoginUI { 6 7 //使用RequestMapping注解指明forward1方法的访问路径 8 @RequestMapping("LoginUI/Lo

简单模拟Hibernate的主要功能实现

在学习期间接触到Hibernate框架,这是一款非常优秀的O/R映射框架,大大简化了在开发web项目过程中对数据库的操作.这里就简单模拟其底层的实现. /*******代码部分,及其主要注解**********************/1.实体类User:public class User {    private int id;    private String username;    private String password; public int getId() {