# Karen and Game

## 题目描述

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 $g_{i, 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!

## 代码

#include <iostream>
using namespace std;
int n, m, p, q, top, f, a, ans, mode;
bool check(int t, int mode) {
if (!mode) {
for (int i = 1; i <= m; ++i) if (!a[t][i]) return false;
for (int i = 1; i <= m; ++i) --a[t][i];
return true;
} else {
for (int i = 1; i <= n; ++i) if (!a[i][t]) return false;
for (int i = 1; i <= n; ++i) --a[i][t];
return true;
}
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> a[i][j];
}
}
p = n, q = m;
if (p > q) {
p ^= q ^= p ^= q;
f = 1;
}
for (int i = 1; i <= p; ++i) while (check(i, f)) {
ans[++top] = i;
mode[top] = f;
}
for (int i = 1; i <= q; ++i) while (check(i, !f)) {
ans[++top] = i;
mode[top] = !f;
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (a[i][j]) {
cout << -1 << endl;
return 0;
}
}
}
cout << top << endl;
for (int i = 1; i <= top; ++i) {
if (!mode[i]) cout << "row ";
else cout << "col ";
cout << ans[i] << endl;
}
return 0;
} 418 I'm a teapot