原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=5538
一:题意
一个n*m的二维数组,a[n][m]代表此处的高,求整个表面积。
二:AC代码
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
int dir[4][2] = { 0,1,-1,0,0,-1,1,0 };
int a[55][55];
int n, m;
bool judge(int x, int y)
{
if (x < 0 || x >= n || y < 0 || y >= m)
return 0;
return 1;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
scanf("%d", &a[i][j]);
int ans = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (a[i][j] != 0)
{
ans++;
for (int k = 0; k < 4; k++)
{
int ii = i + dir[k][0];
int jj = j + dir[k][1];
if (judge(ii, jj))
{
if (a[ii][jj] < a[i][j])
ans += (a[i][j] - a[ii][jj]);
}
else
ans += a[i][j];
}
}
}
}
printf("%d\n", ans);
}
return 0;
}