C. Karen and Game(Round419)
On the way to school, Karen became fixated on the puzzle game on her phone!
![](http://codeforces.com/predownloaded/13/27/1327478902a073d7774242480818f1d4d93d7270.png)
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!
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).
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.
3 5
2 2 2 3 2
0 0 0 1 0
1 1 1 2 1
4
row 1
row 1
col 4
row 3
3 3
0 0 0
0 1 0
0 0 0
-1
3 3
1 1 1
1 1 1
1 1 1
3
row 1
row 2
row 3
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:
![](http://codeforces.com/predownloaded/dd/0f/dd0fcb1e5b1fe4c79e563210a46087f050e31b23.png)
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:
![](http://codeforces.com/predownloaded/e5/66/e56682602939194f6486566eb2bf1a2c106dca6b.png)
Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
hint:给你一个矩阵,问你能不能通过一些操作把这个矩阵变成全0矩阵
这个题目有2s,并且数据也不大。。。
直接暴力加贪心就可以了
我们先判断行和列的大小,从小的开始处理,例如,有10行5列的话,明显从列开始的话,答案更优
确定从哪里开始之后,我们就开始扫描每行或列,减去该行(列)中最小的元素,若此时矩阵不为0,则再从列(行)开始减,用row, col两个数组记录答案
最后输出即可
1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 using namespace std; 5 #define FOR(i, a, b) for(int i=a; i<b; i++) 6 #define FO(i, a, b) for(int i=a; i<=b; i++) 7 int str[240]; 8 int a[120][120]; 9 int n, m, i, j, cnt = 0; 10 11 //模拟题。。。观察思考规律,每行每列减去最小的元素,如果最后得不到全 0 矩阵则失败(逆向思维,从所给矩阵减去一定数得到全0矩阵) --- 12 //有一个坑点,如果列数小于行数,则先从列开始减,否则从行开始减 13 int main(){ 14 scanf("%d%d", &n, &m); 15 FOR(i, 0, n){ 16 FOR(j, 0, m){ 17 scanf("%d", &a[i][j]); 18 } 19 } 20 if(n <= m){ 21 FOR(i, 0, n){ 22 int Min=600; 23 FOR(j, 0, m){ 24 if(a[i][j] < Min) Min = a[i][j]; 25 } 26 if(Min){ 27 FOR(k, 0, m){ 28 a[i][k] -= Min; 29 } 30 str[i] = Min; 31 cnt += Min; 32 } 33 } 34 FOR(i, 0, m){ 35 int Min=600; 36 FOR(j, 0, n){ 37 if(a[j][i] < Min) Min = a[j][i]; 38 } 39 if(Min){ 40 FOR(k, 0, n){ 41 a[k][i] -= Min; 42 } 43 str[100 + i] = Min; 44 cnt += Min; 45 } 46 } 47 } 48 else{ 49 FOR(i, 0, m){ 50 int Min=600; 51 FOR(j, 0, n){ 52 if(a[j][i] < Min) Min = a[j][i]; 53 } 54 if(Min){ 55 FOR(k, 0, n){ 56 a[k][i] -= Min; 57 } 58 str[100 + i] = Min; 59 cnt += Min; 60 } 61 } 62 FOR(i, 0, n){ 63 int Min=600; 64 FOR(j, 0, m){ 65 if(a[i][j] < Min) Min = a[i][j]; 66 } 67 if(Min){ 68 FOR(k, 0, m){ 69 a[i][k] -= Min; 70 } 71 str[i] = Min; 72 cnt += Min; 73 } 74 } 75 } 76 // FOR(i, 0, n){ 77 // FOR(j, 0, m){ 78 // printf("%d ", a[i][j]); 79 // } 80 // printf("\n"); 81 // } 82 int flag=1; 83 FOR(i, 0, n){ 84 if(!flag) break; 85 FOR(j, 0, m){ 86 if(a[i][j] != 0){ 87 flag=0; 88 break; 89 } 90 } 91 } 92 if(!flag){ 93 printf("-1\n"); 94 } 95 else{ 96 printf("%d\n", cnt); 97 FOR(i, 0, 100){ 98 if(str[i] != 0){ 99 while(str[i]--){ 100 printf("row %d\n", i + 1); 101 cnt++; 102 } 103 } 104 } 105 FOR(i, 100, 200){ 106 if(str[i] != 0){ 107 while(str[i]--){ 108 printf("col %d\n", i + 1 - 100); 109 cnt++; 110 } 111 } 112 } 113 } 114 return 0; 115 }