# Minecraft我的世界（矩阵处理）- HDU 5538

Problem Description

Have you ever played the video game Minecraft? This game has been one of the world's most popular game in recent years. The world of Minecraft is made up of lots of 1×1×1 blocks in a 3D map. Blocks are the basic units of structure in Minecraft, there are many types of blocks. A block can either be a clay, dirt, water, wood, air, ... or even a building material such as brick or concrete in this game.

Figure 1: A typical world in Minecraft. Nyanko-san is one of the diehard fans of the game, what he loves most is to build monumental houses in the world of the game. One day, he found a flat ground in some place. Yes, a super flat ground without any roughness, it's really a lovely place to build houses on it. Nyanko-san decided to build on a n×m big flat ground, so he drew a blueprint of his house, and found some building materials to build.

Nyanko是这个游戏的铁粉，他最喜欢做的事情就是建大房子。一天，他发现了一个平坦的陆地，这是一个超级大的平地，在这里建造房子实在是再好不过了。Nyanko决定建造一个nxm大小的平地，他绘制了房子的蓝图，并且找到了一些建造材料。

While everything seems goes smoothly, something wrong happened. Nyanko-san found out he had forgotten to prepare glass elements, which is a important element to decorate his house. Now Nyanko-san gives you his blueprint of house and asking for your help. Your job is quite easy, collecting a sufficient number of the glass unit for building his house. But first, you have to calculate how many units of glass should be collected.

There are n rows and m columns on the ground, an intersection of a row and a column is a 1×1 square,and a square is a valid place for players to put blocks on. And to simplify this problem, Nynako-san's blueprint can be represented as an integer array ci,j(1≤i≤n,1≤j≤m). Which ci,j indicates the height of his house on the square of i-th row and j-th column. The number of glass unit that you need to collect is equal to the surface area of Nyanko-san's house(exclude the face adjacent to the ground).

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.

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

`30 20 `

Figure 2: A top view and side view image for sample test case 1.

（小编注：郭大仙初来乍到~

```#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int maze[55][55];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int m,n,ans=0;
cin>>m>>n;
//！！！每次都要对地图清零
memset(maze,0,sizeof(maze));
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&maze[i][j]);
}
}
for(int i=0;i<=m;i++)
{
//i、j从0开始，可以免去对第一列(行)的特判
for(int j=0;j<=n;j++)
{
//加上maze[i][j]这个方块的下侧表面积
ans+=abs(maze[i][j]-maze[i+1][j]);
//加上本方块的右侧面积
ans+=abs(maze[i][j]-maze[i][j+1]);
//上表面积的计算
if(maze[i][j]) ans++;
}
}
cout<<ans<<endl;
}
return 0;
}```

205 篇文章31 人订阅

0 条评论

## 相关文章

1.6K3

2294

4432

78111

1943

1517

2948

3197

1552

### O(1)效率的表面模糊算法优化。

很久没有写文章了，主要是最近一段时间没有以前那么多空暇空间，内存和CPU占用率一致都很高，应前几日群里网友的要求，今天发个表面模糊的小程序来找回之前...

2466