zoukankan      html  css  js  c++  java
  • [暴力] Educational Codeforces Round 71 (Rated for Div. 2) B. Square Filling (1207B)

     
    B. Square Filling
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given two matrices A

    and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0

    .

    You may perform some operations with matrix B

    . During each operation, you choose any submatrix of B having size 2×2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1x<n and 1y<m, and then set Bx,y, Bx,y+1, Bx+1,y and Bx+1,y+1 to 1

    .

    Your goal is to make matrix B

    equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B

    .

    Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B

    equal to A

    . Note that you don't have to minimize the number of operations.

    Input

    The first line contains two integers n

    and m (2n,m50

    ).

    Then n

    lines follow, each containing m integers. The j-th integer in the i-th line is Ai,j. Each integer is either 0 or 1

    .

    Output

    If it is impossible to make B

    equal to A, print one integer 1

    .

    Otherwise, print any sequence of operations that transforms B

    into A in the following format: the first line should contain one integer k — the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set Bx,y, Bx,y+1, Bx+1,y and Bx+1,y+1 to 1). The condition 0k2500

    should hold.

    Examples
    Input
    Copy
    3 3
    1 1 1
    1 1 1
    0 1 1
    
    Output
    Copy
    3
    1 1
    1 2
    2 2
    
    Input
    Copy
    3 3
    1 0 1
    1 0 1
    0 0 0
    
    Output
    Copy
    -1
    
    Input
    Copy
    3 2
    0 0
    0 0
    0 0
    
    Output
    Copy
    0
    
    Note

    The sequence of operations in the first example:

    000000000110110000110110110110111111

    题意:

    给两个n*m的01矩阵A和B,给出了A中的元素,B中元素全为0,现在有一个操作可以在B中选一个坐标,它和右边下面右下的元素都变为1,问能否通过一些操作(不要求最少)使得B等于A,若能则输出步数和坐标,否则输出-1

    思路:

    暴力并判断边界(当时忘记判断边界结果被Hack了 ~TAT~ )

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int amn=55;
     4 int a[amn][amn],idx[amn][amn],ansx[2600],ansy[2600];
     5 int main(){
     6     int n,m,tp=0,valid=1;
     7     cin>>n>>m;
     8     for(int i=1;i<=n;i++){
     9         for(int j=1;j<=m;j++){
    10             cin>>a[i][j];
    11             idx[i][j]=0;
    12         }
    13     }
    14     for(int i=1;i<=n;i++){
    15         for(int j=1;j<=m;j++){
    16             if(a[i][j]){
    17                 if(!idx[i][j]&&(i+1>n||j+1>m)){ ///这里判断边界条件被hack了...当时想着i<n&&j<m,判非法时忘记判边界了
    18                     valid=0;
    19                     break;
    20                 }
    21                 if(a[i][j+1]&&a[i+1][j]&&a[i+1][j+1]){
    22                     idx[i][j+1]=idx[i+1][j]=idx[i+1][j+1]=1;
    23                     ansx[++tp]=i;ansy[tp]=j;
    24                 }
    25                 else if(!idx[i][j]&&(!idx[i][j+1]||!idx[i+1][j]||!idx[i+1][j+1])){
    26                     valid=0;
    27                     break;
    28                 }
    29             }
    30         }
    31         if(valid==0)break;
    32     }
    33     if(valid){
    34         cout<<tp<<endl;
    35         for(int i=1;i<=tp;i++)cout<<ansx[i]<<' '<<ansy[i]<<endl;
    36     }
    37     else cout<<-1<<endl;
    38 }
    39 /**
    40 给两个n*m的01矩阵A和B,给出了A中的元素,B中元素全为0,现在有一个操作可以在B中选一个坐标,它和右边下面右下的元素都变为1,问能否通过一些操作(不要求最少)使得B等于A,若能则输出步数和坐标,否则输出-1
    41 暴力并判断边界
    42 **/
  • 相关阅读:
    转义字符:html、mysql、postgresql、json、php
    php_mysql、php_mysqli 与 pdo_mysql 的区别与选择
    一件小事测试各个搜索引擎:谷歌、bing、有道、百度、搜狗、360
    一键安装lnmp:自动检测最新稳定版、无需root权限
    apache graceful 与 cpu占用率
    互联网创业的准备——版本控制与上线
    201212互联网创意、创业项目整理
    【图】游东天目山
    那些在11gR2中可能惹祸的新特性,一张列表帮助你摆脱升级11gR2带来的烦恼
    【转】卡巴斯基安全公告称甲骨文数据库存在加密漏洞
  • 原文地址:https://www.cnblogs.com/Railgun000/p/11402795.html
Copyright © 2011-2022 走看看