zoukankan      html  css  js  c++  java
  • 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!

    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.

    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
    ",cnt);
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=ans1[i];j++)
                printf("row %d
    ",i);
            }
            for(i=1;i<=m;i++)
            {
                for(j=1;j<=ans2[i];j++)
                printf("col %d
    ",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
    ",cnt);
                for(i=1;i<=n;i++)
                {
                    for(j=1;j<=ans1[i];j++)
                    printf("row %d
    ",i);
                }
                for(i=1;i<=m;i++)
                {
                    for(j=1;j<=ans2[i];j++)
                    printf("col %d
    ",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
    ",cnt);
                for(i=1;i<=n;i++)
                {
                    for(j=1;j<=ans1[i];j++)
                    printf("row %d
    ",i);
                }
                for(i=1;i<=m;i++)
                {
                    for(j=1;j<=ans2[i];j++)
                    printf("col %d
    ",i);
                }
            }
            else cout<<"-1";
        }
        return 0;
    }
  • 相关阅读:
    NO12 useradd-passwd-uname-hostname命令-上传rz下载sz-批量部署- Linux用户相关操作
    NO11 SSH故障排查思路和netstat命令
    NO10 查看Linux操作系统-版本-内核-Linux分区
    NO9 Linux快捷键整理及最常用命令
    NO8 find结合sed查找替换企业案例多方法精讲&命令总结!
    NO7 利用三剑客awk-grep-sed-head-tail等7种方法实践
    python 对比图片相似度
    MonkeyRunner 实现自动点击截屏后与本地图库进行对比输出
    monkeyrunner对比屏幕局部图像.getSubImage()
    锤子便签的 monkeyrunner 测试脚本(转)
  • 原文地址:https://www.cnblogs.com/huangdalaofighting/p/7042669.html
Copyright © 2011-2022 走看看