CF 815 A. Karen and Game(暴力,模拟)

题目链接:http://codeforces.com/problemset/problem/815/A

题目:

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 x m的初始零矩阵,每次操作可以为某一行,某一列全部+1。问最小需要步数变成题目中给定的矩阵(如果可以的话),否则输出-1。

题解:暴力模拟一下过程。先行再列或先列再行。比较一下,最后输出就可以了。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 #define PI acos(-1.0)
 5 #define INF 0x3f3f3f3f
 6 #define FAST_IO ios::sync_with_stdio(false)
 7 #define CLR(arr,val) memset(arr,val,sizeof(arr))
 8 const int N=111;
 9 const int D=555;
10
11 typedef long long LL;
12 int M1[N][N],M2[N][N],r1[N],c1[N],r2[N],c2[N];
13 string ans1[D*D],ans3[D*D];
14 int ans2[D*D],ans4[D*D];
15
16 int main(){
17     int n,m,cnt1=1,cnt2=1;
18     cin>>n>>m;
19     for(int i=1;i<=n;i++)
20     for(int j=1;j<=m;j++)
21     cin>>M1[i][j],M2[i][j]=M1[i][j];
22
23     for(int i=1;i<=n;i++){
24         r1[i]=INF;
25         for(int j=1;j<=m;j++) r1[i]=min(r1[i],M1[i][j]);
26         for(int j=1;j<=m;j++) M1[i][j]-=r1[i];
27         if(r1[i]!=0){
28             while(r1[i]--){
29                 ans1[cnt1]="row ";
30                 ans2[cnt1]=i;
31                 cnt1++;
32             }
33         }
34     }
35     for(int i=1;i<=m;i++){
36         int idx=0;
37         c1[i]=INF;
38         for(int j=1;j<=n;j++) c1[i]=min(c1[i],M1[j][i]);
39         for(int j=1;j<=n;j++){
40             M1[j][i]-=c1[i];
41             if(M1[j][i]!=0) idx=1;
42         }
43         if(c1[i]!=0){
44             while(c1[i]--){
45                 ans1[cnt1]="col ";
46                 ans2[cnt1]=i;
47                 cnt1++;
48             }
49         }
50         if(idx==1) {cout<<-1<<endl;return 0;}
51     }
52
53
54     for(int i=1;i<=m;i++){
55         c2[i]=INF;
56         for(int j=1;j<=n;j++) c2[i]=min(c2[i],M2[j][i]);
57         for(int j=1;j<=n;j++) M2[j][i]-=c2[i];
58         if(c2[i]!=0){
59             while(c2[i]--){
60                 ans3[cnt2]="col ";
61                 ans4[cnt2]=i;
62                 cnt2++;
63             }
64         }
65     }
66
67     for(int i=1;i<=n;i++){
68         int idx=0;
69         r2[i]=INF;
70         for(int j=1;j<=m;j++) r2[i]=min(r2[i],M2[i][j]);
71         for(int j=1;j<=m;j++){
72             M2[i][j]-=r2[i];
73             if(M2[i][j]!=0) idx=1;
74         }
75         if(r2[i]!=0){
76             while(r2[i]--){
77                 ans3[cnt2]="row ";
78                 ans4[cnt2]=i;
79                 cnt2++;
80             }
81         }
82         if(idx==1) {cout<<-1<<endl;return 0;}
83     }
84     if(cnt1<=cnt2){
85         cout<<cnt1-1<<endl;
86         for(int i=1;i<cnt1;i++){
87             cout<<ans1[i]<<" "<<ans2[i]<<endl;
88         }
89     }
90     else{
91         cout<<cnt2-1<<endl;
92         for(int i=1;i<cnt2;i++){
93             cout<<ans3[i]<<" "<<ans4[i]<<endl;
94         }
95     }
96     return 0;
97 }

原文地址:https://www.cnblogs.com/Leonard-/p/8110938.html

时间: 2024-07-30 06:09:13

CF 815 A. Karen and Game(暴力,模拟)的相关文章

[ 9.11 ]CF每日一题系列—— 441C暴力模拟

Description: n * m 的地图,建设k个管道管道只能横竖走,且长度大于等于2,问你任意一种建设方法 Solution: 图里没有障碍,所以先把前k - 1个管道每个分2个长度,最后一个管道一连到底 Code: PS:今天时间没来的急,其实函数可以封装一下的,虽然都是暴力,但也得暴力的优美不是?? #include <iostream> #include <cstdio> using namespace std; int main() { int n,m,k; whil

hdu 5641 King&#39;s Phone(暴力模拟题)

Problem Description In a military parade, the King sees lots of new things, including an Andriod Phone. He becomes interested in the pattern lock screen. The pattern interface is a 3×3 square lattice, the three points in the first line are labeled as

HDU 4831 Scenic Popularity 暴力模拟

Scenic Popularity Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 340    Accepted Submission(s): 110 Problem Description 临近节日,度度熊们最近计划到室外游玩公园,公园内部包括了很多的旅游景点区和休息区,由于旅游景点很热门,导致景点区和休息区都聚集了很多人.所以度度熊

CF 505A Mr. Kitayuta&#39;s Gift(暴力)

题意  在一个字符串中插入一个字母使其变成一个回文串  可以的话输出这个回文串  否则NA 大水题  插入情况最多就26*11种  可以直接暴力 #include<cstdio> #include<cstring> using namespace std; const int N = 20; char s[N], p[N]; int l; bool ispal() { for(int i = 0; i < (l + 1) / 2; ++i) if(p[i] != p[l -

HDU 4858 项目管理(邻接表 暴力模拟)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4858 我们建造了一个大项目!这个项目有n个节点,用很多边连接起来,并且这个项目是连通的! 两个节点间可能有多条边,不过一条边的两端必然是不同的节点.每个节点都有一个能量值. 现在我们要编写一个项目管理软件,这个软件呢有两个操作:1.给某个项目的能量值加上一个特定值.2.询问跟一个项目相邻的项目的能量值之和.(如果有多条边就算多次,比如a和b有2条边,那么询问a的时候b的权值算2次). 解题报告:这个

201/7/31-zznuoj-问题 A: A + B 普拉斯【二维字符串+暴力模拟+考虑瑕疵的题意-0的特例】

问题 A: A + B 普拉斯 在计算机中,数字是通过像01像素矩阵来显示的,最终的显示效果如下:  现在我们用01来构成这些数字 当宝儿姐输入A + B 时(log10(A)<50,log10(B)<50,且A,B均为正整数),你来计算A+B的和C,并按格式在屏幕上打印C. 输入 每组输入包括两个非负整数 A,B(log10(A)<50,log10(B)<50),已EOF结束输入 输出 按格式在屏幕中打印C,数字之间相隔三列0. 样例输入 3 8 样例输出 00100000001

2017/7/31-zznu-oj-问题 B: N! 普拉斯 -【求大数的阶乘-ll存不下-然后取尾零的个数输出-暴力模拟】

问题 B: N! 普拉斯 时间限制: 1 Sec  内存限制: 128 MB提交: 114  解决: 35[提交] [状态] [讨论版] [命题人:admin] 题目描述 在处理阶乘时也需要借助计算器. 在计算机中,数字是通过像01像素矩阵来显示的,最终的显示效果如下:  宝儿姐一直在思考一个问题,N!末尾究竟有多少个0?我们假设N!末尾有k个0,请按照规则打印k. 输入 输入一个正整数n(n< 50) ,输入以EOF结尾. 输出 我们假设N!末尾有k个0,请按照规则打印k,数字之间间隔3列0.

CF #392(2) C 暴力模拟

CF #392(2)  C. Unfair Poll 题意:n行m列人,老师点k次名.点名次序,每一行都是从1到m,但行是按1,2....(n-1),n,(n-1),(n-2)...1,2,3....(n-1),n.....求点完k次名后被点的最多的次数和最少的次数,以及给定的(x,y)被点次数. 总结:有点麻烦,但还是很好找规律,只是fst了,有时间再写一遍...还是太菜了,连着三场CF都是fst #include<bits/stdc++.h> using namespace std; #p

HDU 1032 [The 3n + 1 problem] 暴力模拟

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032 题目大意:给出i,j,要求输出i j之间"3N+1"的循环次数的最大值. 关键思想:暴力,模拟.可以优化,因为某些大数在进行操作时,会变为已经求过的小数,之后的循环次数已求过. 代码如下: //between i,j 模拟,暴力 #include <iostream> #include <algorithm> using namespace std; int