题目链接:acm.hdu.edu.cn/showproblem.php?pid=5538

2 3 3 1 0 0 3 1 2 1 1 0 3 3 1 0 1 0 0 0 1 0 1
30 20Figure 2: A top view and side view image for sample test case 1.
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int t,n,m,k,ans;
int a[110][110];
scanf("%d",&t);
while (t--)
{
memset(a,0,sizeof(a));
scanf("%d%d",&n,&m);
k=0;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
if (a[i][j]>0)
k++;
}
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
ans=a[i][j]-a[i][j+1];
if (ans>0)
k+=ans;
}
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
ans=a[i][j]-a[i][j-1];
if (ans>0)
k+=ans;
}
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
ans=a[i][j]-a[i-1][j];
if (ans>0)
k+=ans;
}
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
ans=a[i][j]-a[i+1][j];
if (ans>0)
k+=ans;
}
}
printf ("%d\n",k);
}
return 0;
}hdu 5538 House Building(长春现场赛——水题)
原文:http://blog.csdn.net/qiqi_skystar/article/details/51164038