额..... 题目太长我就不贴了,就是给你一个二维数组,a[i][j]代表(i,j)位置木块的高度,然后让你求 i x j 这个范围内所有木块的表面积之和,具体如下图
Input
The first line contains an integer
T
indicating the total number of test cases.
First line of each test case is a line with two integers n,m .
The n lines that follow describe the array of Nyanko-san's blueprint, the i -th of these lines has m integers ci,1,ci,2,...,ci,m , separated by a single space.
1≤T≤50
1≤n,m≤50
0≤ci,j≤1000
First line of each test case is a line with two integers n,m .
The n lines that follow describe the array of Nyanko-san's blueprint, the i -th of these lines has m integers ci,1,ci,2,...,ci,m , separated by a single space.
1≤T≤50
1≤n,m≤50
0≤ci,j≤1000
Output
For each test case, please output the number of glass units you need to collect to meet Nyanko-san's requirement in one line.
Sample Input
2 3 3 1 0 0 3 1 2 1 1 0 3 3 1 0 1 0 0 0 1 0 1
Sample Output
其实这道题,,,比赛的时候看了一段时间,结果没想到怎么做,不过很快就被队长一发解决了~我知道要扫描每个坐标的上下左右和顶部,不过我真的没想到,,原来扫描完直接加上高度差就是面积啊(@_@),第一次这样求表面积... 一道水题吧... QAQ30 20
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e3 + 5;
int main()
{
int a[N][N];
int t,n,m;
cin >> t;
while(t--)
{
cin >> n >> m;
memset(a,0,sizeof(a));
int sum = 0;
int i,j;
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
{
cin >> a[i][j];
if(a[i][j] > 0) //顶部
sum ++;
}
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
{
if(a[i][j] > a[i][j-1]) //左边
sum += a[i][j]-a[i][j-1];
}
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
{
if(a[i][j] > a[i][j+1]) //右边
sum += a[i][j]-a[i][j+1];
}
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
{
if(a[i][j] > a[i-1][j]) //上边
sum += a[i][j]-a[i-1][j];
}
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
{
if(a[i][j] > a[i+1][j]) //下边
sum += a[i][j]-a[i+1][j];
}
cout << sum << "\n";
}
return 0;
}