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

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 mcolumns. 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 52 2 2 3 20 0 0 1 01 1 1 2 1

Output

4row 1row 1col 4row 3

Input

3 30 0 00 1 00 0 0

Output

-1

Input

3 31 1 11 1 11 1 1

Output

3row 1row 2row 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.

题目链接:

CodeForces - 816C

题意:

给定一个n*m的矩阵,

每一个操作你可以选择一行或者一列,使该行或列的数值全部加1,求使用最小的操作次数使一个全部为0的矩阵变成给定矩阵,

如果不可能实现请输出-1.

思路:

预处理出每一行和每一列的最小值,

然后以n和m的关系进行分类处理,

如果n<=m,就先处理行再处理列,这样可以用最小的次数,

反而反之。

举例:

1 1 1 1

1 1 1 1

1 1 1 1

如果先处理列,就要4次,处理行只需要3次。

接下来:

如果每一行或列的最小值大于0,那么我们就可以处理最小值次,然后每一次处理,暴力的把数组中的对应元素减去1,

行和列全部处理好后,去n*m扫一边数组,如果还有数大于0,那么就是无法实现的情况。

每一步具体为什么可能需要大家自己好好思考。

细节见我的代码。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ‘\0‘, sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int a[550][550];
int sumr[550];
int sumc[550];
int n,m;
void buildr(int x)
{
    repd(i,1,n)
    {
        repd(j,1,m)
        {
            if(i==x)
            {
                a[i][j]--;
            }

        }
    }

}
void buildl(int x)
{
    repd(i,1,n)
    {
        repd(j,1,m)
        {
            if(j==x)
            {
                a[i][j]--;
            }
        }
    }
}
int main()
{
    gg(n);
    gg(m);
    repd(i,1,n)
    {
        repd(j,1,m)
        {
            gg(a[i][j]);

        }
    }
    repd(i,1,m)
    {
        int cnt=inf;
        repd(j,1,n)
        {
            cnt=min(cnt,a[j][i]);
        }
        sumc[i]=cnt;
    }
    repd(i,1,n)
    {
        int cnt=inf;
        repd(j,1,m)
        {
            cnt=min(cnt,a[i][j]);
        }
        sumr[i]=cnt;
    }
    int ans=0;
    int flag=0;
    int fu=0;
    std::vector<int> r;
    std::vector<int> c;
    if(n<=m)
    {
        repd(i,1,n)
        {
            while(sumr[i])
            {
                r.pb(i);
                ans++;
                sumr[i]--;
                buildr(i);
            }
        }
        repd(i,1,m)
        {
            int cnt=inf;
            repd(j,1,n)
            {
                cnt=min(cnt,a[j][i]);
            }
            sumc[i]=cnt;
        }
        repd(i,1,m)
        {
            while(sumc[i])
            {
                c.pb(i);
                ans++;
                sumc[i]--;
                buildl(i);
            }
        }
//        repd(i)
    }else
    {
        repd(i,1,m)
        {
            while(sumc[i])
            {
                c.pb(i);
                ans++;
                sumc[i]--;
                buildl(i);
            }
        }
        repd(i,1,n)
        {
            int cnt=inf;
            repd(j,1,m)
            {
                cnt=min(cnt,a[i][j]);
            }
            sumr[i]=cnt;
        }
        repd(i,1,n)
        {
            while(sumr[i])
            {
                r.pb(i);
                ans++;
                sumr[i]--;
                buildr(i);
            }
        }
    }
    repd(i,1,n)
    {
        repd(j,1,m)
        {
            if(a[i][j]<0)
            {
                fu=min(fu,a[i][j]);
            }
            if(a[i][j]!=0)
            {
                flag=1;
                break;
            }
        }
    }
    if(flag)
    {

        printf("-1");
        return 0;
    }
    printf("%d\n",ans );
    repd(i,0,sz(r)-1)
    {
        int x=r[i];
        printf("row %d\n",x);
    }
    repd(i,0,sz(c)-1)
    {
        int x=c[i];
        printf("col %d\n", x);
    }
//    db(fu);
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ‘ ‘ || ch == ‘\n‘);
    if (ch == ‘-‘) {
        *p = -(getchar() - ‘0‘);
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 - ch + ‘0‘;
        }
    }
    else {
        *p = ch - ‘0‘;
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 + ch - ‘0‘;
        }
    }
}

原文地址:https://www.cnblogs.com/qieqiemin/p/10323009.html

时间: 2024-10-14 03:58:36

Karen and Game CodeForces - 816C (暴力+构造)的相关文章

暴力+构造 Codeforces Round #283 (Div. 2) C. Removing Columns

题目传送门 1 /* 2 题意:删除若干行,使得n行字符串成递增排序 3 暴力+构造:从前往后枚举列,当之前的顺序已经正确时,之后就不用考虑了,这样删列最小 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-3 10:49:53 8 File Name :C.cpp 9 ************************************

CodeForces 26C Parquet 构造题

题目链接:点击打开链接 #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <math.h> #include <set> using namespace std; #define N 105 int n,m,a,b,c; char s[N][N]; set<char>myset; bool inm

Codeforces Round #487 (Div. 2) A Mist of Florescence (暴力构造)

C. A Mist of Florescence time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output As the boat drifts down the river, a wood full of blossoms shows up on the riverfront. "I've been here once," M

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

Problem Description 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

Codeforces 1030E 【暴力构造】

LINK 题目大意:给你n个数,你可以交换一个数的任意二进制位,问你可以选出多少区间经过操作后异或和是0 思路 充分必要条件: 区间中二进制1的个数是偶数 区间中二进制位最多的一个数的二进制个数小于等于和的一半 然后因为每个数最少会贡献1,所以直接暴力向前跳128位,再之前的就直接前缀和做掉就可以了 #include<bits/stdc++.h> using namespace std; #define LL long long #define IL inline #define fu(a,b

CodeForces 670D1 暴力或二分

今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1 This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find

codeforces #306D Polygon 构造

题目大意:给定n,要求构造一个凸n边形,使得每个内角都相同,每条边长度都不同 膜拜题解 其实我一开始想的是构造一个正n边形然后把每条边微移一下--不过似乎不是很好写的样子= = #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 110 #define PI 3.14159265358979

Codeforces 1030D 【构造】

LINK 题目大意:给你n,m,k,让你在一个n*m的点阵里构造出一个面积为\(\frac{n*m}{k}\)的三角形 思路 首先要有一个结论是整点三角形的面积分母最多为2,然后就可以判断不存在的情况了 接下来就直接进行构造就可以了 #include<bits/stdc++.h> using namespace std; #define LL long long #define IL inline #define fu(a,b,c) for(LL a=b;a<=c;++a) #defin

Vicious Keyboard CodeForces - 801A (暴力+模拟)

题目链接 题意: 给定一个字符串,最多更改一个字符,问最多可以有多少个"VK"子串? 思路: 由于数据量很小,不妨尝试暴力写.首先算出不更改任何字符的情况下有多个VK字串,然后尝试每一次更改一个位置的字符,然后暴力算出有多少个VK,取出这些答案中 的最大值,即是答案. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #inc