POJ 2330 - Advanced ASCII Cubes

http://poj.org/problem?id=2330

概要

\(N \times M\) の長方形の各マスにいくつの立方体が積み重なっているかを示す行列 \(A_{i, j}\) が与えられるので、 それを ASCII 表現したものを答える。

制約

解法

がんばって左奥のほうから順に立方体を描いていくだけ。

 1 #include <cstdio>
 2 #include <string>
 3 #include <vector>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8   int N, M;
 9   scanf("%d %d", &N, &M);
10   vector<vector<int> > v(N, vector<int>(M));
11   int H = 0;
12   for (int i = 0; i < N; i++) {
13     for (int j = 0; j < M; j++) {
14       scanf("%d", &v[i][j]);
15       H = max(H, 3*v[i][j]+1 + (N-i)*2);
16     }
17   }
18   const int W = 4*M+1 + 2*N;
19   vector<string> view(H, string(W, '.'));
20   for (int i = 0; i < N; i++) {
21     for (int j = 0; j < M; j++) {
22       for (int k = 0; k < v[i][j]; k++) {
23         const int left = (N-i-1)*2 + 4*j;
24         const int right = left + 4;
25         const int below = H-1-(N-1-i)*2 - 3*k;
26         const int above = below - 3;
27         view[above][left] = view[above][right] = view[below][left] = view[below][right] = '+';
28         for (int y = above+1; y < below; y++) {
29           view[y][left] = view[y][right] = '|';
30           for (int x = left+1; x < right; x++) {
31             view[y][x] = ' ';
32           }
33         }
34         for (int x = left+1; x < right; x++) {
35           view[above][x] = view[below][x] = '-';
36         }
37 
38         view[above-1][left+1] = view[above-1][right+1] = '/';
39         for (int x = left+2; x < right+1; x++) {
40           view[above-1][x] = ' ';
41         }
42         view[above-2][left+2] = view[above-2][right+2] = '+';
43         for (int x = left+3; x < right+2; x++) {
44           view[above-2][x] = '-';
45         }
46 
47         view[below-1][right+1] = '/';
48         view[below-2][right+2] = '+';
49         view[below-3][right+2] = view[below-4][right+2] = '|';
50         view[below-2][right+1] = view[below-3][right+1] = ' ';
51       }
52     }
53   }
54   for (int i = 0; i < H; i++) {
55     puts(view[i].c_str());
56   }
57   return 0;
58 }
poj/2330.cc