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

Posted leonard-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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.

题意:给定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 }

 

 

 

以上是关于CF 815 A. Karen and Game(暴力,模拟)的主要内容,如果未能解决你的问题,请参考以下文章

CF815C Karen and Supermarket

Codeforces 815 C Karen and Supermarket

Codeforces 815 B Karen and Test 杨辉三角 数学

Codeforces 815C. Karen and Supermarket树形DP

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

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