ACM ICPC 2010-2011, NEERC, Southern Subregional Contest Problem B. 3D City Model

来源:互联网 发布:备份数据库怎么还原 编辑:程序博客网 时间:2024/06/06 15:39

Problem B. 3D City Model

 

Input file: input.txt

 

Output file: output.txt

 

Time limit: 1 second

 

Memory limit: 256 megabytes

 

A city is built on the top of a rectangular n m grid where all the grid cells are equal squares. Each of then 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 thej-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 (1n; m 100), where n — amount of rows in the given grid, m — amount of columns. The followingn lines contain the description of the model. These n lines contain mdigits 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.txt

output.txt

3 3

38

111

 

212

 

111

 

 

 

3 4

12

1000

 

0010

 

0000

 

 

 

Note

 

 

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

    题意:给出一个n*m的矩阵,每个元素代表不同的高度,问组成的立方体表面积有多大。

    解题思路:刚开始的时候我竟然想到了用DFS写,就是一层一层的判断连通块,进而计算表面积,后来写着写着就发现,根本就没必要用DFS。首先直接计算上表面积和下表面积,因为一个立体几何,侧面是等价的,而且上表面积与下表面积相等(这个题不考虑竖直方向的空心)。然后我再一层一层开循环判断,判断每块四个方向是否大于零即可,每上一层,所有元素减一。另外,我被这个输入样例坑得很惨,他是连着输入的,我以为是排版的问题。一直没有意识到这里,WA了很多次,才发现。下面是我的代码

#include <stdio.h>#include <memory.h>#define maxn 105int arr[maxn][maxn],m,n;int flag[maxn][maxn];int main(){    freopen("input.txt","r",stdin);    freopen("output.txt","w",stdout);    //freopen("in.txt","r",stdin);    int ud=0,max=0;    while(~scanf("%d%d",&m,&n))    {        getchar();        for(int i=1; i<=m; i++)        {            for(int j=1; j<=n; j++)            {                char temp;                temp=getchar();                arr[i][j]=temp-'0';                if(arr[i][j]>0) ud++;                max=max>arr[i][j]?max:arr[i][j];            }            getchar();        }        long long sum=2*ud;        for(int i=0; i<max; i++)        {            memset(flag,0,sizeof(flag));            for(int i=1; i<=m; i++)                for(int j=1; j<=n; j++)                    if(arr[i][j]>0) flag[i][j]=1;            for(int i=1; i<=m; i++)                for(int j=1; j<=n; j++)                {                    if(flag[i][j])                    {                        if(flag[i-1][j]==0) sum++;                        if(flag[i][j-1]==0) sum++;                        if(flag[i+1][j]==0) sum++;                        if(flag[i][j+1]==0) sum++;                    }                }            for(int i=1; i<=m; i++)                for(int j=1; j<=n; j++)                    arr[i][j]--;        }        printf("%lld\n",sum);    }    return 0;}

阅读全文
0 0