A city is built on the top of a rectangular n × m grid where all the grid cells are equal squares. Each of the n·m grid cells can serve as a foundation of a single building in the city. A building is represented as a number of 1 × 1 × 1 cubes stacked on the top of each other. The cube that lays in the foundation of a building entirely occupies a single cell on the grid. It is clear that adjacent buildings can share a wall or a part of it. Typical cities can be seen on the image below.
![](https://i-blog.csdnimg.cn/blog_migrate/cb6c684837f43bbbfa374643abfb25f2.png)
The King of Berland has a 3D model of the capital city in his office. This model was made on a special 3D-printer out of plastic. It represents a layout of the capital city, but the scale is smaller, so it's very convenient for the King to examine the model without having to visit the city itself. The King is bored though because the model is colorless, so he wants to paint the model. To calculate the exact amount of required paint he should know the total area of the model's surface.
You have to help the King and write a program that will calculate the required surface area of the given model. While calculating the surface area you should count not only the side surfaces, but also the areas of the top and bottom facets.
The model is given to you as n × m matrix of digits. A digit in the j-th position of the i-th row stands for the height of the building with its foundation in cell (i, j) of the model. If the corresponding digit is equal to "0", it means there is no building built on the top of this cell.
The first line of input contains a pair of integers n, m (1 ≤ n, m ≤ 100), where n — amount of rows in the given grid, m — amount of columns. The following n lines contain the description of the model. These n lines contain m digits each representing heights of the buildings. It's guaranteed that the given matrix contains at least one non-zero digit.
Output the only positive integer — surface area of the model.
3 3 111 212 111
38
3 4 1000 0010 0000
12
The first sample test corresponds to the leftmost picture from the problem statement.
大致题意就是 给出立方体的形状 求该几何体的表面积
我的思想是 (1)每次读入矩阵的时候 同时计数有多少个小立方体 记为sum
(2)用for循环去判断小立方体之间重合的有多少块 (三个方向都要判断) 记为ans
(3)运用数学公式 sum*6-ans*2 (每个小立方体的基础面积为6 一块重合面积使两方都各少一块)
下面放一下代码
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
int x[1005][1005];
char y[1005][1005];
int main()
{
int m,n;
char a;
int sum=0;
int ans=0;
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
cin >> n >> m;
memset(x,0,sizeof x);
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)//读入矩阵
{
cin>>a;
a-='0';
x[i][j]=a;
}
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)//对小方块进行计数
{
sum=sum+x[i][j];
}
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)//横向 如果有重合部分就计数
{
ans+=min(x[i][j],x[i][j+1]);//重合部分算两者中小的面积
}
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
{
ans+=min(x[i+1][j],x[i][j]);
}
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
if (x[i][j]>1)
ans+=(x[i][j]-1);//高度大于1时 竖直方向的重合部分 为高度减一
int h=sum*6-ans*2;//每一块基础表面积为6 减去重合部分的 *2
cout << h <<endl;
return 0;
}
前一次交题的时候卡在 test 10 想了好久不知道错哪里了 后来 一个学长给了一个他的样例
我测试了一下 发现...我的行列标记反了 唉( ̄m ̄)
以后 再遇到矩阵题目要注意一下了...