uvalive 11464

Posted 一个_小菜鸟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uvalive 11464相关的知识,希望对你有一定的参考价值。

题意:怎么使最少的0变成1让原矩阵的每个数的上下左右之和为偶数

思路:枚举第一行就可以确定所有的状态

  1 package text;
  2 
  3 import java.awt.List;
  4 import java.io.BufferedReader;
  5 import java.io.IOException;
  6 import java.io.InputStream;
  7 import java.io.InputStreamReader;
  8 import java.io.PrintWriter;
  9 import java.math.BigDecimal;
 10 import java.math.BigInteger;
 11 import java.util.Map;
 12 import java.util.Scanner;
 13 import java.util.StringTokenizer;
 14 
 15 public class Main {
 16     public static void main(String[] args) throws IOException {
 17         InputReader cin = new InputReader(System.in);
 18         int t;
 19         int n;
 20         int cube[][] = new int[50][50];
 21         int now[][] = new int[50][50];
 22         t = cin.nextInt();
 23         for(int p = 1;p<=t;p++){
 24             int ans = -1;
 25             n = cin.nextInt();
 26             for(int i = 1;i<=n;i++)
 27                 for(int j = 1;j<=n;j++)
 28                     cube[i][j] = cin.nextInt();
 29             for(int i = 0;i<1<<n;i++) {
 30                 int tmp = i;
 31                 int tmpans = 0;
 32                 for(int j = 1;j<=n;j++) {
 33                     now[1][j] = tmp&1;
 34                     tmp/=2;
 35                 }
 36                 boolean flag = true;
 37                 for(int j = 1;j<=n;j++)
 38                     if(cube[1][j]==0&&now[1][j]==1)
 39                         tmpans++;
 40                     else if(cube[1][j]==1&&now[1][j]==0)
 41                         flag = false;
 42                 for(int j = 2;j<=n;j++)
 43                     for(int k = 1;k<=n;k++)
 44                             now[j][k] = 0;
 45                 for(int j = 2;j<=n;j++) {
 46                     for(int k = 1;k<=n;k++) {
 47                         int s = 0,nowj = j-1;
 48                         if(nowj>=2) s+=now[nowj-1][k];
 49                         if(k>=2) s+=now[nowj][k-1];
 50                         if(k<n) s+=now[nowj][k+1];
 51                         now[j][k] = s%2;
 52                         if(now[j][k]==0&&cube[j][k]==1) {
 53                             flag = false;
 54                             break;
 55                         }
 56                         if(now[j][k]==1&&cube[j][k]==0)
 57                             tmpans++;
 58                     }
 59                     if(!flag)
 60                         break;
 61                 }
 62                 if(flag&&(tmpans<ans||ans==-1))
 63                     ans = tmpans;
 64             }
 65             if(ans==-1)
 66                 System.out.printf("Case %d: -1\n",p);
 67             else 
 68                 System.out.printf("Case %d: %d\n",p,ans);
 69         }
 70     }
 71 }
 72 class InputReader {      
 73     public BufferedReader reader;      
 74     public StringTokenizer tokenizer;      
 75       
 76     public InputReader(InputStream stream) {      
 77         reader = new BufferedReader(new InputStreamReader(stream), 32768);      
 78         tokenizer = new StringTokenizer("");      
 79     }      
 80     
 81     
 82     private void eat(String s) {      
 83         tokenizer = new StringTokenizer(s);      
 84     }      
 85       
 86     public String nextLine() {       
 87         try {      
 88             return reader.readLine();      
 89         } catch (Exception e) {      
 90             return null;      
 91         }      
 92     }      
 93       
 94     public boolean hasNext() {      
 95         while (!tokenizer.hasMoreTokens()) {      
 96             String s = nextLine();      
 97             if (s == null)      
 98                 return false;      
 99             eat(s);      
100         }      
101         return true;      
102     }      
103       
104     public String next() {      
105         hasNext();      
106         return tokenizer.nextToken();      
107     }      
108       
109     public int nextInt() {      
110         return Integer.parseInt(next());      
111     }      
112       
113     public int[] nextInts(int n) {      
114         int[] nums = new int[n];      
115         for (int i = 0; i < n; i++) {      
116             nums[i] = nextInt();      
117         }      
118         return nums;      
119     }      
120       
121     public long nextLong() {      
122         return Long.parseLong(next());      
123     }      
124       
125     public double nextDouble() {      
126         return Double.parseDouble(next());      
127     }      
128       
129     public BigInteger nextBigInteger() {      
130         return new BigInteger(next());      
131     }      
132 }      

 

以上是关于uvalive 11464的主要内容,如果未能解决你的问题,请参考以下文章

Uva 11464 偶数矩阵

UVa 11464 偶数矩阵

UVA 11464 偶数矩阵

uva11464 Even Parity

Uva11464 Even Parity

枚举暴力UVA11464 Even Parity