C. Karen and Game

C. Karen and Game
time limit per test  2 seconds
memory limit per test  512 megabytes
input  standard input
output  standard output

On the way to school, Karen became fixated on the puzzle game on her phone!ios

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.ui

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!this

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.spa

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.3d

Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level.code

The next k lines should each contain one of the following, describing the moves in the order they must be done:orm

  • 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 5
2 2 2 3 2
0 0 0 1 0
1 1 1 2 1
Output
4
row 1
row 1
col 4
row 3
Input
3 3
0 0 0
0 1 0
0 0 0
Output
-1
Input
3 3
1 1 1
1 1 1
1 1 1
Output
3
row 1
row 2
row 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.

題解:

這道題首先想過網絡流,然而沒有什麼用。

後來發現貪心能夠過,求出每行每列的最小值,而後一個一個找,從行開始,每次維護一下列的最小值,最後統計一下值的總合是否是和以前的相同,不是的話就說明還有殘餘,如樣例2。是的話就輸出結果。

注:這個代碼有問題,正解在最下面

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
int s[101][101],n,m,sum,sum2;
int mminn[101],mminm[101];
int ans1[101],cnt,ans2[101];
int main()
{
    int i,j;
    memset(mminn,127/3,sizeof(mminn));
    memset(mminm,127/3,sizeof(mminm));
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            scanf("%d",&s[i][j]);
            sum+=s[i][j];
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        mminn[i]=min(mminn[i],s[i][j]);
    }
    for(j=1;j<=m;j++)
    {
        for(i=1;i<=n;i++)
        mminm[j]=min(mminm[j],s[i][j]);
    }
    for(i=1;i<=n;i++)
    {
        ans1[i]=mminn[i];
        for(j=1;j<=m;j++)
        {
            s[i][j]-=mminn[i];
            mminm[j]=min(mminm[j],s[i][j]);
        }
    }
    for(j=1;j<=m;j++)
    {
        ans2[j]=mminm[j];
    }
    for(i=1;i<=n;i++)
    {
        sum2+=ans1[i]*m;
    }
    for(i=1;i<=m;i++)
    {
        sum2+=ans2[i]*n;
    }
    if(sum==sum2)
    {
        for(i=1;i<=n;i++)
        if(ans1[i])cnt+=ans1[i];
        for(i=1;i<=m;i++)
        if(ans2[i])cnt+=ans2[i];
        printf("%d\n",cnt);
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=ans1[i];j++)
            printf("row %d\n",i);
        }
        for(i=1;i<=m;i++)
        {
            for(j=1;j<=ans2[i];j++)
            printf("col %d\n",i);
        }
    }
    else cout<<"-1";
    return 0;
}

 

 沒錯,這份代碼確實有問題,很顯然隨便設計一組測試數據

input

4 3

1 1 1

1 1 1

1 1 1

1 1 1

output

3

col 1

col 2

col 3

 而上面這份代碼會輸出

output

4

row 1

row 2

row 3

row 4

而後就WA掉了,(心痛)。可是修改也很簡單,最後判斷一下是從行仍是從列開始就能夠了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
int s[101][101],n,m,sum,sum2;
int mminn[101],mminm[101];
int ans1[101],cnt,ans2[101];
int main()
{
    int i,j;
    memset(mminn,127/3,sizeof(mminn));
    memset(mminm,127/3,sizeof(mminm));
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            scanf("%d",&s[i][j]);
            sum+=s[i][j];
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        mminn[i]=min(mminn[i],s[i][j]);
    }
    for(j=1;j<=m;j++)
    {
        for(i=1;i<=n;i++)
        mminm[j]=min(mminm[j],s[i][j]);
    }
    if(m>n)
    {
        for(i=1;i<=n;i++)
        {
            ans1[i]=mminn[i];
            for(j=1;j<=m;j++)
            {
                s[i][j]-=mminn[i];
                mminm[j]=min(mminm[j],s[i][j]);
            }
        }
        for(j=1;j<=m;j++)
        {
            ans2[j]=mminm[j];
        }
        for(i=1;i<=n;i++)
        {
            sum2+=ans1[i]*m;
            }
        for(i=1;i<=m;i++)
        {
            sum2+=ans2[i]*n;
        }
        if(sum==sum2)
        {
            for(i=1;i<=n;i++)
            if(ans1[i])cnt+=ans1[i];
            for(i=1;i<=m;i++)
            if(ans2[i])cnt+=ans2[i];
            printf("%d\n",cnt);
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=ans1[i];j++)
                printf("row %d\n",i);
            }
            for(i=1;i<=m;i++)
            {
                for(j=1;j<=ans2[i];j++)
                printf("col %d\n",i);
            }
        }
        else cout<<"-1";
    }
    else
    {
        for(i=1;i<=m;i++)
        {
            ans2[i]=mminm[i];
            for(j=1;j<=n;j++)
            {
                s[j][i]-=mminm[i];
                mminn[j]=min(mminn[j],s[j][i]);
            }
        }
        for(j=1;j<=n;j++)
        {
            ans1[j]=mminn[j];
        }
        for(i=1;i<=n;i++)
        {
            sum2+=ans1[i]*m;
        }
        for(i=1;i<=m;i++)
        {
            sum2+=ans2[i]*n;
        }
        if(sum==sum2)
        {
            for(i=1;i<=n;i++)
            if(ans1[i])cnt+=ans1[i];
            for(i=1;i<=m;i++)
            if(ans2[i])cnt+=ans2[i];
            printf("%d\n",cnt);
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=ans1[i];j++)
                printf("row %d\n",i);
            }
            for(i=1;i<=m;i++)
            {
                for(j=1;j<=ans2[i];j++)
                printf("col %d\n",i);
            }
        }
        else cout<<"-1";
    }
    return 0;
}
相關文章
相關標籤/搜索