现在的位置: 首页 > 综合 > 正文

POJ1222

2013年09月01日 ⁄ 综合 ⁄ 共 2660字 ⁄ 字号 评论关闭

题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=1222

 

  1. import java.util.Scanner;
  2. public class Main {
  3.     int times;
  4.     int[][] puzzle;
  5.     static final int length = 6;
  6.     static final int width = 5;
  7.     int[][] arr;
  8.     int temp;
  9.     int req;
  10.     public Main() {
  11.         Scanner scan = new Scanner(System.in);
  12.         times = scan.nextInt();
  13.         for (int t = 0; t < times; t++) {
  14.             puzzle = new int[width][length];
  15.             arr = new int[width][length];
  16.             for (int i = 0; i < width; i++) {
  17.                 for (int j = 0; j < length; j++) {
  18.                     puzzle[i][j] = scan.nextInt();
  19.                 }
  20.             }
  21.             force();
  22.             System.out.println("PUZZLE #" + (t + 1));
  23.             for (int i = 0; i < width; i++) {
  24.                 for (int j = 0; j < length; j++) {
  25.                     System.out.print(arr[i][j] + " ");
  26.                 }
  27.                 System.out.println();
  28.             }
  29.         }
  30.     }
  31.     //穷举第一行数据,推导下面的数据...
  32.     //判断最后一行是否符合要求
  33.     //6列需要最多计算2^6=64次
  34.     public void force() {
  35.         boolean result = false;
  36.         do {
  37.             result = search();
  38.             if (result) {
  39.                 return;
  40.             }
  41.         } while (plus(arr[0], 0));
  42.     }
  43.     public boolean search() {
  44.         for (int i = 1; i < width; i++) {
  45.             for (int j = 0; j < length; j++) {
  46.                 req = puzzle[i - 1][j];
  47.                 temp = 0;
  48.                 if (j - 1 >= 0) {
  49.                     temp += arr[i - 1][j - 1];
  50.                 }
  51.                 if (j + 1 < length) {
  52.                     temp += arr[i - 1][j + 1];
  53.                 }
  54.                 if (i - 2 >= 0) {
  55.                     temp += arr[i - 2][j];
  56.                 }
  57.                 temp += arr[i - 1][j];
  58.                 if (req != (temp % 2)) {
  59.                     arr[i][j] = 1;
  60.                 } else {
  61.                     arr[i][j] = 0;
  62.                 }
  63.             }
  64.         }
  65.         for (int i = 0; i < length; i++) {
  66.             req = puzzle[width - 1][i];
  67.             temp = 0;
  68.             if (i - 1 >= 0) {
  69.                 temp += arr[width - 1][i - 1];
  70.             }
  71.             temp += arr[width - 2][i];
  72.             temp += arr[width - 1][i];
  73.             if (i + 1 < length) {
  74.                 temp += arr[width - 1][i + 1];
  75.             }
  76.             if (req != (temp % 2)) {
  77.                 return false;
  78.             }
  79.         }
  80.         return true;
  81.     }
  82.     public boolean plus(int[] a, int b) {
  83.         if (b == length) {
  84.             return false;
  85.         } else if (a[b] == 0) {
  86.             a[b]++;
  87.             return true;
  88.         } else {
  89.             a[b] = 0;
  90.             return plus(a, ++b);
  91.         }
  92.     }
  93.     public static void main(String[] args) {
  94.         new Main();
  95.     }
  96. }

抱歉!评论已关闭.