zoukankan      html  css  js  c++  java
  • uva 11464

    We have a grid of size N x N. Each cell of the grid initially contains a zero(0) or a one(1). 
    The parity of a cell is the number of 1s surrounding that cell. A cell is surrounded by at most 4 cells (top, bottom, left, right).

    Suppose we have a grid of size 4 x 4: 

    1

    0

    1

    0

    The parity of each cell would be

    1

    3

    1

    2

    1

    1

    1

    1

    2

    3

    3

    1

    0

    1

    0

    0

    2

    1

    2

    1

    0

    0

    0

    0

    0

    1

    0

    0

    For this problem, you have to change some of the 0s to 1s so that the parity of every cell becomes even. We are interested in the minimum number of transformations of 0 to 1 that is needed to achieve the desired requirement.

     
    Input

    The first line of input is an integer T (T<30) that indicates the number of test cases. Each case starts with a positive integer N(1≤N≤15). Each of the next N lines contain N integers (0/1) each. The integers are separated by a single space character.

    Output

    For each case, output the case number followed by the minimum number of transformations required. If it's impossible to achieve the desired result, then output -1 instead.

    Sample Input                             Output for Sample Input

    3
    3
    0 0 0
    0 0 0
    0 0 0
    3
    0 0 0
    1 0 0
    0 0 0
    3
    1 1 1
    1 1 1
    0 0 0
     

    Case 1: 0 
    Case 2: 3 
    Case 3: -1


     

     1 /*
     2 把尽量少的0变成1,使得每个元素上下左右的元素(存在的话)之和均为偶数
     3 */
     4 #include <iostream>
     5 #include <string>
     6 #include <cstdio>
     7 using namespace std;
     8 
     9 const int maxn=16;
    10 const int INF=100000000;
    11 int T,n,ans;
    12 int A[maxn][maxn],B[maxn][maxn];
    13 int min(int a,int b){ return a<b?a:b;}
    14 
    15 int fun(string s)
    16 {
    17     int cnt=0,i,j,sum;
    18     for(i=0;i<n;i++) B[0][i]=s[i]-'0';
    19     for(i=1;i<n;i++)
    20         for(j=0;j<n;j++) B[i][j]=A[i][j];
    21     for(i=0;i<n;i++)
    22     {
    23         for(j=0;j<n;j++)
    24         {
    25             sum=0;
    26             if(i>0) sum+=B[i-1][j];
    27             if(j>0) sum+=B[i][j-1];
    28             if(j<n-1) sum+=B[i][j+1];
    29             if(i<n-1 && sum%2==0 && B[i+1][j]==1) return INF;
    30             if(i<n-1 && sum%2==1 && B[i+1][j]==0) B[i+1][j]='1';
    31             if(i==n-1 && sum%2==1) return INF;
    32         }
    33     }
    34     for(i=0;i<n;i++)
    35     {
    36         for(j=0;j<n;j++)
    37             if(B[i][j]!=A[i][j]) cnt++;
    38     }
    39     return cnt;
    40 }
    41 void dfs(string s,int i)
    42 {
    43     if(i==n)
    44     {
    45         ans=min(ans,fun(s));
    46         return ;
    47     }
    48     if(s[i]=='0')
    49     {
    50         dfs(s,i+1);
    51         s[i]='1';
    52         dfs(s,i+1);
    53         s[i]='0';
    54     }
    55     else dfs(s,i+1);
    56 }
    57 void solve()
    58 {
    59     ans=INF;
    60     string s="";
    61     for(int i=0;i<n;i++) s+=A[0][i]+'0';
    62     dfs(s,0);
    63     if(ans==INF) ans=-1;
    64     printf("%d
    ",ans);
    65 }
    66 int main()
    67 {
    68     int icase=0;
    69     scanf("%d",&T);
    70     while(T--)
    71     {
    72         scanf("%d",&n);
    73         for(int i=0;i<n;i++)
    74         {
    75             for(int j=0;j<n;j++)
    76                 scanf("%d",&A[i][j]);
    77         }
    78         printf("Case %d: ",++icase);
    79         solve();
    80     }
    81     return 0;
    82 }
  • 相关阅读:
    网络相关命令
    jmeter压力测试接口
    mysql+mycat+centos7
    centos7开放端口
    mysql主从库配置文件
    读写分离
    Memcached与Redis的区别和选择
    mybatis 学习
    redis 轻松入门
    swagger 框架使用
  • 原文地址:https://www.cnblogs.com/xiong-/p/3765795.html
Copyright © 2011-2022 走看看