zoukankan      html  css  js  c++  java
  • C. Karen and Game(Round419)

    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.

    hint:给你一个矩阵,问你能不能通过一些操作把这个矩阵变成全0矩阵

    这个题目有2s,并且数据也不大。。。

    直接暴力加贪心就可以了

    我们先判断行和列的大小,从小的开始处理,例如,有10行5列的话,明显从列开始的话,答案更优

    确定从哪里开始之后,我们就开始扫描每行或列,减去该行(列)中最小的元素,若此时矩阵不为0,则再从列(行)开始减,用row, col两个数组记录答案

    最后输出即可

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<string>
      4 using namespace std;
      5 #define FOR(i, a, b) for(int i=a; i<b; i++)
      6 #define FO(i, a, b) for(int i=a; i<=b; i++)
      7 int str[240];
      8 int a[120][120];
      9 int n, m, i, j, cnt = 0;
     10 
     11 //模拟题。。。观察思考规律,每行每列减去最小的元素,如果最后得不到全 0 矩阵则失败(逆向思维,从所给矩阵减去一定数得到全0矩阵) ---
     12 //有一个坑点,如果列数小于行数,则先从列开始减,否则从行开始减
     13 int main(){
     14     scanf("%d%d", &n, &m);
     15     FOR(i, 0, n){
     16         FOR(j, 0, m){
     17             scanf("%d", &a[i][j]);
     18         }
     19     }
     20     if(n <= m){
     21     FOR(i, 0, n){
     22         int Min=600;
     23         FOR(j, 0, m){
     24             if(a[i][j] < Min) Min = a[i][j];
     25         }
     26         if(Min){
     27             FOR(k, 0, m){
     28                 a[i][k] -= Min;
     29             }
     30             str[i] = Min;
     31             cnt += Min;
     32         }
     33     }
     34     FOR(i, 0, m){
     35         int Min=600;
     36         FOR(j, 0, n){
     37             if(a[j][i] < Min) Min = a[j][i];
     38         }
     39         if(Min){
     40             FOR(k, 0, n){
     41                 a[k][i] -= Min;
     42             }
     43             str[100 + i] = Min;
     44             cnt += Min;
     45         }
     46     }
     47     }
     48     else{
     49         FOR(i, 0, m){
     50         int Min=600;
     51         FOR(j, 0, n){
     52             if(a[j][i] < Min) Min = a[j][i];
     53         }
     54         if(Min){
     55             FOR(k, 0, n){
     56                 a[k][i] -= Min;
     57             }
     58             str[100 + i] = Min;
     59             cnt += Min;
     60         }
     61         }
     62         FOR(i, 0, n){
     63         int Min=600;
     64         FOR(j, 0, m){
     65             if(a[i][j] < Min) Min = a[i][j];
     66         }
     67         if(Min){
     68             FOR(k, 0, m){
     69                 a[i][k] -= Min;
     70             }
     71             str[i] = Min;
     72             cnt += Min;
     73         }
     74     }
     75     }
     76 //    FOR(i, 0, n){
     77 //        FOR(j, 0, m){
     78 //            printf("%d ", a[i][j]);
     79 //        }
     80 //        printf("
    ");
     81 //    }
     82     int flag=1;
     83     FOR(i, 0, n){
     84         if(!flag) break;
     85         FOR(j, 0, m){
     86             if(a[i][j] != 0){
     87                 flag=0;
     88                 break;
     89             }
     90         }
     91     }
     92     if(!flag){
     93         printf("-1
    ");
     94     }
     95     else{
     96         printf("%d
    ", cnt);
     97         FOR(i, 0, 100){
     98             if(str[i] != 0){
     99                 while(str[i]--){
    100                     printf("row %d
    ", i + 1);
    101                     cnt++;
    102                 }
    103             }
    104         }
    105         FOR(i, 100, 200){
    106             if(str[i] != 0){
    107                 while(str[i]--){
    108                     printf("col %d
    ", i + 1 - 100);
    109                     cnt++;
    110                 }
    111             }
    112         }
    113     }
    114     return 0;
    115 }
  • 相关阅读:
    正则表达式(二):Unicode诸问题(上)
    ANT Notes
    Linux下OpenGL开发 -- 准备篇 (转)
    两个和尚
    Office 2008 for Mac 安装笔记
    从软件工程师到IT猎头:我的一点经历和感触 (转)
    One splitpath implementation (platform independent)
    ANT的使用(转)
    80后中专毕业奋斗10年 我的理财选择
    用ANT来实现邮件发送
  • 原文地址:https://www.cnblogs.com/ledoc/p/7126891.html
Copyright © 2011-2022 走看看