AOJ1510 Independent Reserach

Independent Research | Aizu Online Judge

Introduction to Programming Introduction to Algorithms and Data Structures Library of Data Structures Library of Graph Algorithms Library of Computational Geometry Library of Dynamic Programming Library of Number Theory

グリッドが なので,愚直にシュミレーション.そのマスの周り マスに生息している生物の数を数えて,そのマスの誕生と死滅を判断する.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n, c = 0;
int v[5][5][5], v2[5][5][5];
bool a[30], b[30];

int dx[26] = { 1,-1,   0, 0,       0, 0,   1, 1,-1,-1,   1, 1,-1,-1,   0, 0, 0, 0,     1, 1, 1, 1,    -1,-1,-1,-1};
int dy[26] = { 0, 0,  1,-1,    0, 0,   1,-1, 1,-1,   0, 0, 0, 0,     1, 1,-1,-1,   1, 1,-1,-1,   1, 1,-1,-1};
int dz[26] = { 0, 0,  0, 0,       1,-1,    0, 0, 0, 0,     1,-1, 1,-1,   1,-1, 1,-1,   1,-1, 1,-1,   1,-1, 1,-1};

bool can(int x,int y,int z) {
  if(0 <= x && x < 5 && 0 <= y && y < 5 && 0 <= z && z < 5) return true;
  return false;
}

int main() {
  while(cin >> n && n) {
      memset(v, 0, sizeof(v));
      memset(v2, 0, sizeof(v2));

      rep(i, 5) {
          rep(j, 5) {
              string s;
              cin >> s;

              rep(k, 5) {
                  v[i][j][k] = s[k] - '0';
              }
          }
      }

      int m1;
      cin >> m1;

      memset(a, 0, sizeof(a));
      rep(i, m1) {
          int x;
          cin >> x;
          a[x] = true;
      }

      int m2;
      cin >> m2;
      memset(b, 0, sizeof(b));
      rep(i, m2) {
          int x;
          cin >> x;
          b[x] = true;
      }

      rep(q, n) {
          rep(i, 5) rep(j, 5) rep(k, 5) v2[i][j][k] = v[i][j][k];
          rep(i, 5) {
              rep(j, 5) {
                  rep(k, 5) {
                      int sum = 0;

                      rep(l, 26) {
                          int x = i + dx[l];
                          int y = j + dy[l];
                          int z = k + dz[l];

                          if(can(x, y, z)) sum += v[x][y][z];
                      }

                      if(v[i][j][k] == 0) {
                          if(a[sum]) {
                              v2[i][j][k] = 1;
                          }
                      } else {
                          if(!b[sum]) {
                              v2[i][j][k] = 0;
                          }
                      }
                  }
              }
          }

          rep(i, 5) rep(j, 5) rep(k, 5) v[i][j][k] = v2[i][j][k];
      }

      if(c) cout << endl;
      cout << "Case " << c + 1 << ":" << endl;
      c++;

      rep(i, 5) {
          if(i) cout << endl;
          rep(j, 5) {
              rep(k, 5) {
                  cout << v[i][j][k];
              }
              cout << endl;
          }
      }
  }

  return 0;
}
May 10th, 2016