Time Limit: 1 second
Memory Limit: 128 MB
【问题描述】
一座城市建立在规则的n×m网格上,并且网格均由1×1正方形构成。在每个网格上都可以有一个建筑,建筑由若干个1×1×1的立方体搭建而成(也就是所有建筑的底部都在同一平面上的)。几个典型的城市模型如下图所示:
现在给出每个网格上建筑的高度,即每个网格上建筑由多少个立方体搭建而成,要求这个建筑模型的表面积是多少。
【输入格式】
输入文件3d.in的第1行包含2个正整数n和m,为城市模型的长与宽。 接下来n行,每行m个数字字符,描述了网格每个格子高度(可见所有建筑高度都大等于0且小等于9)。
【输出格式】
输出文件3d.out包含一个非负整数,为城市模型的表面积。
【数据规模】
20%的数据满足:n, m≤10; 40%的数据满足:n, m≤100; 100%的数据满足:n, m≤1000。
Sample Input1
3 3
111
212
111
Sample Output1
38
Sample Input2
3 4
1000
0010
0000
Sample Output2
12
【题目链接】:http://noi.qz5z.com/viewtask.asp?id=u106
【题解】
看样例2可以知道,地面也算进去了;
所以遇到一个高度为0的格子,答案就递增2(上面和底面);
如果4个侧面有高于其他格子的部分,则也会露出来;递增相应答案就好;边界的侧面肯定会露出来;
【完整代码】
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
void rel(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void rei(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
const int MAXN = 1e3+100;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);
int n,m;
int a[MAXN][MAXN];
int main()
{
//freopen("F:\rush.txt","r",stdin);
memset(a,0,sizeof(a));
rei(n);rei(m);
rep1(i,1,n)
rep1(j,1,m)
{
char ch;
cin >> ch;
a[i][j] = ch-'0';
}
LL ans = 0;
rep1(i,1,n)
rep1(j,1,m)
if (a[i][j]>0)
{
ans+=2;
rep1(k,1,4)
{
int ti = i+dx[k],tj = j+dy[k];
if (a[i][j]>a[ti][tj])
ans+=(a[i][j]-a[ti][tj]);
}
}
cout << ans<<endl;
return 0;
}