VJ 122106 C-House Building

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 1000+10,INF=1000000;
int maze[MAXN][MAXN],d[MAXN][MAXN],sign[MAXN][MAXN];                    //maze为保存input的数组,d为保存每个位置表面积的数组,sign为标记数组,判断是否进行dfs
int dx[4]={0,-1,0,1},dy[4]={1,0,-1,0};                                  //四方向遍历的数组
int squ=0,m,n;                                                          //使用全局变量是因为很多main函数中的变量在调用函数中也需要使用。
void dfs(int x,int y)                                                   //dfs函数作用是改变原来d数组中的表面积值。从而直接在main函数中累加出结果
{
    sign[x][y]=0;                                                       //一开始将sign[x][y]归0,没有标记数组程序会停止工作。注: d数组减去表面积的值是依赖于周围
    for(int i=0;i<4;i++)                                                //的maze值,但是dfs在同一个位置只能执行一次。
    {
        int nx=x+dx[i],ny=y+dy[i];
        if(nx>=0&&nx<m&&ny>=0&&ny<n)                                    //判断是否越界。
        {
            if(maze[x][y]<=maze[nx][ny]) d[x][y]-=maze[x][y];           //如果比周围的要小,等于说那一个方向的面积全部被覆盖了。
            else if(maze[x][y]>maze[nx][ny]) d[x][y]-=maze[nx][ny];     //如果比周围的要高,等于说那一个方向的面积需要去掉周围的高度值
            if(sign[nx][ny]==INF)                                       //只有未标记的maze需要dfs
            dfs(nx,ny);
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&m,&n);
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
            scanf("%d",&maze[i][j]);
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
            {
                d[i][j]=4*maze[i][j]+(maze[i][j]?1:0);                                             //初始值为maze值乘侧面积也就是4,以及顶部的一块(分0和非0讨论)
                sign[i][j]=INF;                                                                               //标记初始全为INF
            }
        squ=0;
        dfs(0,0);
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
            squ+=d[i][j];                                                                                       //计算剩下的表面积
        printf("%d\n",squ);
    }
    return 0;

}

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
 
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  
big flat ground, so he drew a blueprint of his house, and found some building materials to build.

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  
rows and  
columns on the ground, an intersection of a row and a column is a
 
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
   .
Which    
indicates the height of his house on the square of
 -th
row and  -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
 
indicating the total number of test cases.

First line of each test case is a line with two integers
 .

The  
lines that follow describe the array of Nyanko-san‘s blueprint, the
 -th
of these lines has  
integers        ,
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 

本题目一开始没有头绪,整个的思路是比较乱的,样例我是数出结果的,甚至一开始还想过直接一整面一整面地求解,显然是不现实的。

然后想到了遍历,但具体几个方向遍历也有点问题,四个方向遍历肯定是需要的,但是否要向上遍历也是个问题。后来仔细想了一下,向上遍历无非就是再加4个侧面。

可能会觉得如果加上一块在上面,被挡住的侧面不就不同了么。实际上,四方向遍历改变的状态已经包括了挡住侧面的考虑。即为高度差。

一开始停止工作了,以为是数组越界了,实际上是没有加标记导致程序不停地执行bfs函数然后自然崩了,一开始我没加标记数组是因为我觉得加了标记数组后状态无法转移

到之前经过的状态会导致缺面积。实际上还是思路不清,每遍历到一个位置,改变的表面积是从初始值经过对周围砖块的高度判定来改变自身那一方向的面积的,也就是说周围的砖块仍然要对自身判断,而且没必要对同一方向的自己判断两次。所以,标记数组是必要的。

所以啊。做题之前一定要有整体的思路,再用代码实现。而且对不可能的方法要进行排除换别的方法。

别想着边想边敲了,我这个菜鸟。

时间: 2024-11-05 01:14:54

VJ 122106 C-House Building的相关文章

(转载)解决AndroidStudio导入项目在 Building gradle project info 一直卡住

源地址http://blog.csdn.net/yyh352091626/article/details/51490976 Android Studio导入项目的时候,一直卡在Building gradle project info这一步,主要原因还是因为被墙的结果.gradle官网虽然可以访问,但是速度连蜗牛都赶不上... 解决办法主要有两种,一是直接下载gradle离线包,二是修改项目的gradle-wrapper.properties里的gradle版本为自己电脑已有的版本. 离线包下载导

ZOJ 3820 Building Fire Stations

Building Fire Stations Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on ZJU. Original ID: 382064-bit integer IO format: %lld      Java class name: Main Special Judge Marjar University is a beautiful and peaceful place. There a

hdu 5033 Building(斜率优化)

Building Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1237    Accepted Submission(s): 350 Special Judge Problem Description Once upon a time Matt went to a small town. The town was so sma

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

Building Block HDU - 2818 (并查集)

Building Block HDU - 2818 题意:搬砖...每一次可以把a所在的那一堆放到b所在的那一堆上面,问第x号砖下面有几块砖. 记录一下到根节点的距离(dw),以及根节点上方有几块砖(up). 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn=30010; 5 int f[maxn],up[maxn],dw[maxn]; 6 7 int gf(int x){ 8 if(x==f[x]){ 9

JVM building

http://hg.openjdk.java.net/jdk7/jdk7/raw-file/tip/README-builds.html#ant http://hg.openjdk.java.net/jdk8/jdk8/raw-file/tip/README-builds.html Building OpenJDK 9 on Ubuntu http://www.tuicool.com/articles/v2aaEv 图文解析在Linux下搭建Hotspot JVM源码调试环境 | How to

Building Maintainable Software-java篇之Keep Architecture Components Balanced

Building encapsulation boundaries is a crucial skill in software architecture. -George H. Fairbanks in Just Enough Architecture Guideline: ? Balance the number and relative size of top-level components in your code. ? Do this by organizing source cod

2015ACM/ICPC亚洲区长春站 L hdu 5538 House Building

House Building Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 145    Accepted Submission(s): 123 Problem Description Have you ever played the video game Minecraft? This game has been one of t

ACM学习历程—BNUOJ3685 Building for UN(构造)

The United Nations has decided to build a new headquarters in Saint Petersburg, Russia. It will have a form of a rectangular parallelepiped and will consist of several rectangular floors, one on top of another. Each floor is a rectangular grid of the