zoukankan      html  css  js  c++  java
  • Even Parity uva11464 模拟

    Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

    []   [Go Back]   [Status]  

    Description

     

    D

    Even Parity

    Input: Standard Input

    Output: Standard Output

     

    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 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 #include<time.h>
     5 #include<iostream>
     6 #include<algorithm>
     7 using namespace std;
     8 int a[20][20],n,b[20][20];
     9 long long ab;
    10 int fun1(int x,int y)
    11 {
    12     return b[x][y+1]^b[x][y-1]^b[x-1][y];
    13 }
    14 int fun(int x)
    15 {
    16     if((ab&x)!=ab)return 11111110;
    17     memset(b,0,sizeof(b));
    18     int i,j,ans=0;
    19     for(i=n; i>=1; i--)
    20     {
    21         b[1][i]=x&1;
    22         if(b[1][i]^a[1][i])
    23         {
    24             if(b[1][i])ans++;
    25         }
    26         x>>=1;
    27     }
    28     for(i=2; i<=n; i++)
    29     {
    30         for(j=1; j<=n; j++)
    31         {
    32             b[i][j]=fun1(i-1,j);
    33             if(b[i][j]^a[i][j])
    34             {
    35                 if(b[i][j])
    36                     ans++;
    37                 else return 11111110;
    38             }
    39         }
    40     }
    41     return ans;
    42 }
    43 int main()
    44 {
    45     int T,i,j,k,size,ans;
    46     scanf("%d",&T);
    47     for(i=1; i<=T; i++)
    48     {
    49         ans=11111110;
    50         memset(a,0,sizeof(a));
    51         scanf("%d",&n);
    52         for(j=1; j<=n; j++)
    53             for(k=1; k<=n; k++)
    54                 scanf("%d",&a[j][k]);
    55         size=(1<<n);
    56         ab=0;
    57         for(j=1; j<=n; j++)
    58         {
    59             ab=(ab<<1)+a[1][j];
    60         }
    61         for(j=0; j<size; j++)
    62         {
    63             int y=fun(j);
    64             ans=ans>y?y:ans;
    65         }
    66         if(ans==11111110)ans=-1;
    67         printf("Case %d: %d
    ",i,ans);
    68     }
    69 }
    View Code
  • 相关阅读:
    Linux压缩和解压缩命令(linux必学基础)
    Windows Server 2012 忘记登录密码怎么办?
    linux双网卡双网关设置
    date命令的帮助信息,使用date命令输出数字做为命名标题则不会有重复标题
    linux中history加入显示时间命令代码
    the MTS failed last time时的解决办法
    linux tar.gz zip 解压缩 压缩命令
    如果centos7添加新网卡,系统不识别的解决办法
    Linux常用命令英文全称与中文解释 (pwd、su、df、du等)
    使用动态规划算法解释硬币找零问题
  • 原文地址:https://www.cnblogs.com/ERKE/p/3683810.html
Copyright © 2011-2022 走看看