SRM328 D1E-D2M LightsCube

TopCoder Statistics - Problem Statement

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.

一辺が$N$の立方体の中に色$i$の明かりが与えられる.明かりは位置ステップ事に隣接している場所に広がる.広がる場所に重なりがあった場合は,番号が若い方が優先される.最終的に色$i$の光は何個あるか.愚直にやる.番号が若い順にやりたいので,最初のqueueに番号順に入れていく.後は順番に更新していけばよい.

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
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <queue>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define INF 1<<30
#define mp make_pair

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

vector<string> split(const string &str, char delim) {
  vector<string> res;
  size_t current = 0, found;
  while((found = str.find_first_of(delim, current)) != string::npos) {
      res.push_back(string(str, current, found - current));
      current = found + 1;
  }
  res.push_back(string(str, current, str.size() - current));
  return res;
}

int f(string s) {
  stringstream ss;
  ss << s;

  int x;
  ss >> x;

  return x;
}

int d[50][50][50], n;
int dx[6] = {1,-1, 0, 0, 0, 0};
int dy[6] = {0, 0, 1,-1, 0, 0};
int dz[6] = {0, 0, 0, 0, 1,-1};

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

class LightsCube {
  public:
  vector <int> count(int N, vector <string> lights) {
      n = N;
      memset(d, -1, sizeof(d));
      queue<PI> que;
      rep(i, lights.size()) {
          vector<string> ret = split(lights[i], ' ');

          int x = f(ret[0]);
          int y = f(ret[1]);
          int z = f(ret[2]);

          d[x][y][z] = i;
          que.push(mp(mp(x, y), z));
      }

      while(que.size()) {
          PI p = que.front(); que.pop();

          int x = p.first.first;
          int y = p.first.second;
          int z = p.second;

          rep(i, 6) {
              int nx = x + dx[i];
              int ny = y + dy[i];
              int nz = z + dz[i];

              if(can(nx, ny, nz) && d[nx][ny][nz] == -1) {
                  d[nx][ny][nz] = d[x][y][z];
                  que.push(mp(mp(nx, ny), nz));
              }
          }
      }

      vector<int> v(lights.size());
      rep(i, N) {
          rep(j, N) {
              rep(k, N) {
                  v[d[i][j][k]]++;
              }
          }
      }

      return v;
  }
};
Nov 19th, 2016