【题解】立体图

题目描述

  小渊是个聪明的孩子,他经常会给周围的小朋友们将写自己认为有趣的内容。最近,他准备给小朋友们讲解立体图,请你帮他画出立体图。

  小渊有一块面积为m×n的矩形区域,上面有m×n个边长为1的格子,每个格子上堆了一些同样大小的积木(积木的长宽高都是1),小渊想请你打印出这些格子的立体图。我们定义每个积木为如下格式,并且不会做任何翻转旋转,只会严格以这一种形式摆放:

Failed to load picture

  每个顶点用1个加号“+”表示,长用3个“-”表示,宽用1个“/”,高用两个“|”表示。字符“+”、“-”、“/”、“|”的ASCII码分别为43,45,47,124。字符“.”(ASCII码46)需要作为背景输出,即立体图里的空白部分需要用“.”来代替。立体图的画法如下面的规则:

  若两块积木左右相邻,图示为:

Failed to load picture

  若两块积木上下相邻,图示为:

Failed to load picture

  若两块积木前后相邻,图示为:

Failed to load picture

  立体图中,定义位于第(m,1)的格子(即第m行第1列的格子)上面自底向上的第一块积木(即最下面的一块积木)的左下角顶点为整张图最左下角的点。

 

输入格式

  第一行有用空格隔开的2个整数m和n,表示有m×n个格子(1≤m,n≤50)。

  接下来的m行,是一个m×n的矩阵,每行有n个用空格隔开的整数,其中第i行第j列上的整数表示第i行第j列的个子上摞有多少个积木(1≤每个格子上的积木数≤100)。

 

输出格式

  包含题目要求的立体图,是一个K行L列的字符串矩阵,其中K和L表示最少需要K行L列才能按规定输出立体图。

 

输入样例

3 4

2 2 1 2

2 2 1 1

3 2 1 2

输出样例

Failed to load picture
 

题解

  按照“从前往后,从右往左,从上往下”的顺序排即可,具体参考程序。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>

#define MAXM 51
#define MAXN 51

using namespace std;

int m, n;
int a[MAXM][MAXN];
int maxx = 4, maxy;
char ans[350][350];

int main()
{
    //Pretreatment
    cin >> m >> n;
    maxy = n * 4 + m * 2 + 1;
    for(register int i = 1; i <= m; i++)
    {
        maxx += 2;
        for(register int j = 1; j <= n; j++)
        {
            cin >> a[i][j];
            if(maxx < a[i][j] * 3 + 3) maxx = a[i][j] * 3 + 3;
        }
    }
    for(register int i = 1; i <= maxx; i++)
    {
        for(register int j = 1; j <= maxy; j++)
        {
            ans[i][j] = '.';
        }
    }
    int cntii, cntjj;
    // 从前往后,从右往左,从上往下
    for(register int i = m; i >= 1; i--)
    {
        for(register int j = n; j >= 1; j--)
        {
            cntii = 0; 
            for(register int ii = maxx - (m - i) * 2 - a[i][j] * 3 - 2; ii <= maxx - (m - i) * 2; ii++)
            {
                cntii++;
                cntjj = 0;
                for(register int jj = (m - i) * 2 + (j - 1) * 4 + 1; jj <= (m - i) * 2 + (j - 1) * 4 + 7; jj++)
                {
                    cntjj++;
                    if(cntii == 1)
                    {
                        if(cntjj < 3 || ans[ii][jj] != '.') continue;
                        if(cntjj == 3 || cntjj == 7)
                        {
                            ans[ii][jj] = '+';
                        }
                        else
                        {
                            ans[ii][jj] = '-';
                        }
                    }
                    else if(cntii == 2)
                    {
                        if(cntjj == 1 || ans[ii][jj] != '.') continue;
                        if(cntjj == 2 || cntjj == 6)
                        {
                            ans[ii][jj] = '/';
                        }
                        else if(cntjj == 7)
                        {
                            ans[ii][jj] = '|';
                        }
                        else
                        {
                            ans[ii][jj] = ' ';
                        }
                    }
                    else if(cntii % 3 == 0)
                    {
                        if(ans[ii][jj] != '.') continue;
                        if(cntjj == 1 || cntjj == 5)
                        {
                            ans[ii][jj] = '+';
                        }
                        else if(cntjj == 6)
                        {
                            if(ii == maxx - (m - i) * 2 ) break;
                            ans[ii][jj] = ' ';
                        }
                        else if(cntjj == 7)
                        {
                            if(ii == maxx - (m - i) * 2 ) break;
                            ans[ii][jj] = '|';
                        }
                        else
                        {
                            ans[ii][jj] = '-';
                        }
                    }
                    else if(cntii % 3 == 1)
                    {
                        if(ans[ii][jj] != '.') continue;
                        if(cntjj == 1 || cntjj == 5)
                        {
                            ans[ii][jj] = '|';
                        }
                        else if(cntjj == 7)
                        {
                            ans[ii][jj] = '+';
                        }
                        else
                        {
                            ans[ii][jj] = ' ';
                        }
                    }
                    else
                    {
                        if(ans[ii][jj] != '.') continue;
                        if(cntjj == 1 || cntjj == 5)
                        {
                            ans[ii][jj] = '|';
                        }
                        else if(cntjj == 6)
                        {
                            ans[ii][jj] = '/';
                        }
                        else if(cntjj == 7)
                        {
                            if(ii == maxx - (m - i) * 2 - 1) break;
                            ans[ii][jj] = '|';
                        }
                        else
                        {
                            ans[ii][jj] = ' ';
                        }
                    }
                }
            }
        }
    }
    for(register int i = 1; i <= maxx; i++)
    {
        for(register int j = 1; j <= maxy; j++)
        {
            putchar(ans[i][j]);
        }
        if(i < maxx) putchar('\n');
    }
    return 0;
}
参考程序

猜你喜欢

转载自www.cnblogs.com/kcn999/p/10846142.html
今日推荐