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

    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 1x<n1≤x<n and 1y<m1≤y<m, and then set Bx,yBx,y, Bx,y+1Bx,y+1, Bx+1,yBx+1,y and Bx+1,y+1Bx+1,y+1 to 11.

    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 (2n,m502≤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 0k25000≤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:

     

     分析:刚开始理解有点偏差,打算map,测试错了,改成vector,或者二维数组也是可以的,遍历一下就是把2x2矩阵左上角得位置保存下来,然后和原数组比较一下

    二维数组,【2500+5】【2】就行

    #define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #define Rep for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>dp[i][j];
    int
    a[55][55],b[55][55]; int vis[55][55]; int dp[1000][1000]; vector<pair<int,int> >vc; map<int,int>mp; int main() { TLE; int n,m,k,cnt,ans; memset(b,0,sizeof(b)); cin>>n>>m; Rep; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if( dp[i][j]==1 && dp[i+1][j]==1 && dp[i][j+1]==1 && dp[i+1][j+1]==1 ) vc.push_back(make_pair(i,j)); for(int i=0;i<vc.size();i++) { int x=vc[i].first,y=vc[i].second; //cout<<x<<" -------- "<<y<<endl; b[x][y]=1; b[x+1][y]=1; b[x][y+1]=1; b[x+1][y+1]=1; } for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) if(dp[i][j] != b[i][j]) { cout<<-1<<endl; return 0; } cout<<vc.size()<<endl; for(int i=0;i<vc.size();i++) cout<<vc[i].first<<" "<<vc[i].second<<endl; ok; }

     

    int a[55][55],b[55][55];
    int vis[2500][2];
    vector<pair<int,int> >vc;
    map<int,int>mp;
    int main()
    {
        TLE;
        int n,m,k=0,cnt,ans;
        memset(b,0,sizeof(b));
        cin>>n>>m;
        Rep;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if( dp[i][j]==1 && dp[i+1][j]==1 && dp[i][j+1]==1 && dp[i+1][j+1]==1 )
                {
                    //vc.push_back(make_pair(i,j));
                    vis[k][0]=i;vis[k++][1]=j;
                    b[i][j]=1;b[i+1][j+1]=1;b[i+1][j]=1;b[i][j+1]=1;
                }    
        for(int i = 1;i <= n;i++)
            for(int j = 1;j <= m;j++)
                if(dp[i][j] != b[i][j])
                {
                    cout<<-1<<endl;     
                    return 0;
                }
        cout<<k<<endl;
        for(int i=0;i<k;i++)
            cout<<vis[i][0]<<" "<<vis[i][1]<<endl;
        ok;
    }

     PS:下面的有调试,没什么用,没必要看


    const int maxn = 1e6 + 7;
     
    int a[55][55],b[55][55];
    int vis[2500][2];
    vector<pair<int,int> >vc;
    map<int,int>mp;
    int main()
    {
        TLE;
        int n,m,k=0,cnt,ans;
        memset(b,0,sizeof(b));
        cin>>n>>m;
        Rep;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if( dp[i][j]==1 && dp[i+1][j]==1 && dp[i][j+1]==1 && dp[i+1][j+1]==1 )
                {
                    //vc.push_back(make_pair(i,j));
                    vis[k][0]=i;vis[k++][1]=j;
                    b[i][j]=1;b[i+1][j+1]=1;b[i+1][j]=1;b[i][j+1]=1;
                }    
        /*
        for(int i=0;i<vc.size();i++)
        {
            int x=vc[i].first  ,  y=vc[i].second;
            //cout<<x<<" -------- "<<y<<endl;
            b[x][y]=1; b[x+1][y]=1;  b[x][y+1]=1;   b[x+1][y+1]=1;
        }
        */
        /*
        for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)
        {
            int x=it->first,y=it->second;
            cout<<it->first<<" "<<it->second<<endl;  
            b[x][y]=1; b[x+1][y]=1;  b[x][y+1]=1;   b[x+1][y+1];
        }
        */
        /*
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                cout<<dp[i][j]<<" "<<b[i][j]<<endl;
            }
        */
        for(int i = 1;i <= n;i++)
            for(int j = 1;j <= m;j++)
                if(dp[i][j] != b[i][j])
                {
                    cout<<-1<<endl;     
                    return 0;
                }
        cout<<k<<endl;
        for(int i=0;i<k;i++)
            cout<<vis[i][0]<<" "<<vis[i][1]<<endl;
        /*
        cout<<vc.size()<<endl;
        for(int i=0;i<vc.size();i++)
            cout<<vc[i].first<<" "<<vc[i].second<<endl;    
        */
        /*
        cout<<mp.size()<<endl;
        for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)
            cout<<it->first<<" "<<it->second<<endl;  
        */
        ok;
    }
    所遇皆星河
  • 相关阅读:
    CentOS 7.0关闭默认防火墙启用iptables防火墙
    centos7 启动httpd的时候为什么显示是这样的
    CentOS配置本地yum源/阿里云yum源/163yuan源,并配置yum源的优先级
    Linux如何用yum安装软件或服务
    IE浏览器和Firefox浏览器兼容性问题及解决办法
    Input的size与maxlength属性的区别
    下拉框默认选择数据库取出数据
    登录到 SQL Server 实例
    安装sql server 2008重启失败
    值栈
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/11515215.html
Copyright © 2011-2022 走看看