zoukankan      html  css  js  c++  java
  • HDU1264 Counting Squares 简单Hash

    Counting Squares

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 405    Accepted Submission(s): 212


    Problem Description
    Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in the range 0 to 100. For example, the line
    5 8 7 10
    specifies the rectangle who's corners are(5,8),(7,8),(7,10),(5,10).
    If drawn on graph paper, that rectangle would cover four squares. Your job is to count the number of unit(i.e.,1*1) squares that are covered by any one of the rectangles given as input. Any square covered by more than one rectangle should only be counted once.
     

    Input
    The input format is a series of lines, each containing 4 integers. Four -1's are used to separate problems, and four -2's are used to end the last problem. Otherwise, the numbers are the x-ycoordinates of two points that are opposite corners of a rectangle.
     

    Output
    Your output should be the number of squares covered by each set of rectangles. Each number should be printed on a separate line.
     

    Sample Input
    5 8 7 10 6 9 7 8 6 8 8 11 -1 -1 -1 -1 0 0 100 100 50 75 12 90 39 42 57 73 -2 -2 -2 -2
     

    代码入下:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include <time.h>
    #include <conio.h>
    
    // 计算图形的面积,由于放在了Hash专题,想啊想,用Hash法怎么去做 
    // 将每一个图形单元看做是一个Hash对象,即用数组模拟一个二维的空间 
    
    int hash[105][105];
    
    int main()
    {
        int x1=1, y1, x2, y2,cnt= 0, flag;
        while(scanf( "%d%d%d%d", &x1, &y1, &x2, &y2 ))
        {
            flag= cnt= 0;
            memset( hash, 0, sizeof( hash ) );
            while( ( x1!= -1&& y1!= -1&& x2!= -1&& y2!= -1 ) )
            {
                if( x1== -2&& y1== -2&& x2== -2&& y2== -2 )
                {
                    flag= 1;
                    break;
                }
                if( x1> x2 )
                    x1^= x2^= x1^= x2;
                if( y1> y2 )
                    y1^= y2^= y1^= y2;
                for( int i= x1; i< x2; ++i )
                {
                    for( int j= y1; j< y2; ++j )
                        hash[i][j]= 1;
                }
                scanf( "%d%d%d%d", &x1, &y1, &x2, &y2 );
            }
            for( int i= 0; i< 100; ++i )
            {
                for( int j= 0; j< 100; ++j )
                    if( hash[i][j] )
                        cnt++;
            }   
            printf( "%d\n", cnt );
            if( flag )
                break;
        }
        return 0;
    }
    

      初看为一道几何题,但是既然加在了Hash题列中,当然考虑到是用Hash来搞定这道题目,题中给定的数据范围并不大,所以该题我们能够用二维数组来模拟二维平面,取四个角的一个点来Hash该点的状态。
  • 相关阅读:
    深度学习三巨头Hinton,Bengio,LeCunn共摘本年度ACM图灵奖(ACM A.M. Turing Award)
    【我为之而活的三种激情】by 伯特兰·罗素
    遥感高光谱分类文献阅读:Going Deeper with Contextual CNN for Hyperspectral Image Classification
    遥感高光谱分类文献阅读:Exploring Hierarchical Convolutional Features for Hyperspectral Image Classification
    大乘百法明门论笔记
    太宰治【人间失格】
    论文笔记:Accurate Causal Inference on Discrete Data
    因果推断(Causal Inference)概要
    阿毗达摩基本概念
    我们必须知道,我们终将知道。
  • 原文地址:https://www.cnblogs.com/Lyush/p/2058887.html
Copyright © 2011-2022 走看看