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;
}
發佈了113 篇原創文章 · 獲贊 41 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章