Codeforces Round #419 (Div. 2) C. Karen and Game 题解


time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
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.


对于一个n*m的初始值全为0的矩阵,每次一操作能使一行或者一列的所有元素+1,现给出游戏要求的局面,要求输出最少步骤的满足局面的操作。若不存在解,输出-1.

100*100的矩阵强行搜索似乎是不行的,但这题是比较明显的确定了第一排操作次数就能确定所有或许操作的,因为对于任意位置a[i][j],所有可以影响到他的操作只有row[i]和col[j].所以对于第一排我们枚举其操作次数,对于还没有达到要求局面的位置就必须使用列操作,便确定了所有的列操作,而后续的每一列的操作也就因此确定下来了,此时,只有每一排i的所有元素a[i][j]-col[j]都相等才有解,否则无解。比较最小值更新答案,最后输出答案即可。

注意过程中出现不满足情况即时跳出循环。

#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
int inf = 0x3f3f3f3f;
const int maxn = 105;
const int maxm = 2005;
const int mod = 1000000007;
int n,m;
int a[maxn][maxn];
int row[maxn],ansrow[maxn];
int col[maxn],anscol[maxn];
int main(){
    int cnt = inf;
    int ans = inf;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++){
            scanf("%d", &a[i][j]);
        }
    }
    for (int j = 0; j < m; j++)cnt = min(cnt, a[0][j]);
    for (int k = 0; k <= cnt; k++){//枚举第一行操作次数
        int cntans = 0;
        memset(row, 0, sizeof(row));
        memset(col, 0, sizeof(col));
        row[0] = k;
        cntans += k;
        for (int i = 0; i < m; i++){//确定每一列需要的操作次数
            int cur = a[0][i] - k;
            if (cur > 0){
                col[i] = cur;
                cntans += cur;
                for (int j = 1; j < n; j++){
                    if (a[j][i] - col[i] < 0){
                        cntans = -1; break;
                    }
                }
            }
            if (cntans == -1)break;
        }
        if (cntans == -1)continue;
        for (int i = 1; i < n; i++){//确定剩下行的操作次数
            int cur = a[i][0] - col[0];
            row[i] = cur;
            cntans += cur;
            for (int j = 1; j < m; j++){
                if (a[i][j] - col[j] - row[i] != 0){
                    cntans = -1; break;
                }
            }
            if (cntans == -1)break;
        }   
        if (cntans == -1)continue;
        else if (cntans<ans){
            ans = cntans;
            for (int i = 0; i < n; i++)ansrow[i] = row[i];
            for (int i = 0; i < m; i++)anscol[i] = col[i];
        }
        }
    if (ans == inf)printf("-1");
    else{
        printf("%d\n", ans);
        for (int i = 0; i <m; i++){
            while (anscol[i]){
                printf("col %d\n", i + 1);
                anscol[i]--;
            }
        }
        for (int i = 0; i <n; i++){
            while (ansrow[i]){
                printf("row %d\n", i + 1);
                ansrow[i]--;
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值