• B. Square Filling


    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 AA and BB. Each matrix contains exactly nn rows and mm columns. Each element of AA is either 00 or 11; each element of BB is initially 00.

    You may perform some operations with matrix BB. During each operation, you choose any submatrix of BB having size 2×22×2, and replace every element in the chosen submatrix with 11. In other words, you choose two integers xx and yy such that 1≤x

    Your goal is to make matrix BB equal to matrix AA. Two matrices AA and BB are equal if and only if every element of matrix AA is equal to the corresponding element of matrix BB.

    Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes BB equal to AA. Note that you don't have to minimize the number of operations.

    Input

    The first line contains two integers nn and mm (2≤n,m≤502≤n,m≤50).

    Then nn lines follow, each containing mm integers. The jj-th integer in the ii-th line is Ai,jAi,j. Each integer is either 00 or 11.

    Output

    If it is impossible to make BB equal to AA, print one integer −1−1.

    Otherwise, print any sequence of operations that transforms BB into AA in the following format: the first line should contain one integer kk — the number of operations, and then kk lines should follow, each line containing two integers xx and yy for the corresponding operation (set Bx,yBx,y, Bx,y+1Bx,y+1, Bx+1,yBx+1,y and Bx+1,y+1Bx+1,y+1 to 11). The condition 0≤k≤25000≤k≤2500 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:

    000000000→110110000→110110110→110111111000110111111000→110→111→111000000000011

    =========================================================================

    无限次,那就能用就用,地毯式遍历即可

    1. # include
    2. # include
    3. using namespace std;
    4. int a[100][100],b[100][100];
    5. vectorint,int> >ans;
    6. int main()
    7. {
    8. int n,m;
    9. cin>>n>>m;
    10. for(int i=1;i<=n;i++)
    11. {
    12. for(int j=1;j<=m;j++)
    13. {
    14. cin>>a[i][j];
    15. }
    16. }
    17. for(int i=1;i<=n;i++)
    18. {
    19. for(int j=1;j<=m;j++)
    20. {
    21. if(a[i][j]==1)
    22. {
    23. if(a[i][j+1]&&a[i+1][j]&&a[i+1][j+1])
    24. {
    25. b[i][j]=1;
    26. b[i][j+1]=1;
    27. b[i+1][j]=1;
    28. b[i+1][j+1]=1;
    29. ans.push_back(make_pair(i,j));
    30. }
    31. }
    32. }
    33. }
    34. for(int i=1;i<=n;i++)
    35. {
    36. for(int j=1;j<=m;j++)
    37. {
    38. if(a[i][j]!=b[i][j])
    39. {
    40. cout<<-1;
    41. return 0;
    42. }
    43. }
    44. }
    45. cout<size()<
    46. for(auto it:ans)
    47. {
    48. cout<" "<
    49. }
    50. return 0;
    51. }

  • 相关阅读:
    学网安兴趣最重要
    PX4模块设计之二十六:BatteryStatus模块
    java开发:网络编程
    guava工具类常用方法
    第45节——页面中修改redux里的数据
    springboot整合MeiliSearch轻量级搜索引擎
    leetcode每日一题——Split With Minimum Sum
    Python中的异常处理以及自定义异常类型
    k8s驱逐篇(6)-kube-controller-manager驱逐-NodeLifecycleController源码分析
    [第五空间 2021]WebFTP
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126329071