热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

CF:3DCityModel(小思维)问题解析和代码实现

本文通过解析CF:3DCityModel问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。

链接:http://codeforces.com/gym/101246/problem/B

B. 3D City Model
time limit per test
1 second
memory limit per test
256 megabytes
input
input.txt
output
output.txt

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.

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.

Input

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

Output the only positive integer — surface area of the model.

Examples
input

3 3
111
212
111

output

38

input

3 4
1000
0010
0000

output

12

Note

The first sample test corresponds to the leftmost picture from the problem statement.

题意:一座n*m的城市,给出俯视图各个点的楼层数,求出整体的表面积。

思路:每一个点,通过它前后左右与他本身的高度差计算即可。

# include
# include
# include
# include
using namespace std;
char a[105][105];
int main()
{int n, m;freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);scanf("%d%d",&n,&m);int ans &#61; 0, cnt &#61; 0;memset(a, 0, sizeof(a));for(int i&#61;1; i<&#61;n; &#43;&#43;i){scanf("%s",a[i]&#43;1);for(int j&#61;1; j<&#61;m; &#43;&#43;j)a[i][j] -&#61; &#39;0&#39;;}for(int i&#61;1; i<&#61;n; &#43;&#43;i)for(int j&#61;1; j<&#61;m; &#43;&#43;j){if(a[i][j])&#43;&#43;cnt;ans &#43;&#61; max(a[i][j]-a[i][j&#43;1], 0);ans &#43;&#61; max(a[i][j]-a[i&#43;1][j], 0);ans &#43;&#61; max(a[i][j]-a[i][j-1], 0);ans &#43;&#61; max(a[i][j]-a[i-1][j], 0);}ans &#43;&#61; (cnt<<1);printf("%d\n", ans);return 0;
}



转:https://www.cnblogs.com/junior19/p/6729954.html



推荐阅读
author-avatar
luosj
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有