VJ组队赛->House Building(5538)

来源:互联网 发布:Java毫秒和日期的换算 编辑:程序博客网 时间:2024/05/17 04:35

额..... 题目太长我就不贴了,就是给你一个二维数组,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.

1T50
1n,m50
0ci,j1000
 
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
23 31 0 03 1 21 1 03 31 0 10 0 01 0 1

Sample Output
3020
其实这道题,,,比赛的时候看了一段时间,结果没想到怎么做,不过很快就被队长一发解决了~我知道要扫描每个坐标的上下左右和顶部,不过我真的没想到,,原来扫描完直接加上高度差就是面积啊(@_@),第一次这样求表面积... 一道水题吧... QAQ

#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;}


原创粉丝点击