码迷,mamicode.com
首页 > Windows程序 > 详细

Shaping Regions(dfs)

时间:2015-05-01 22:22:40      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

 Shaping Regions

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 124  Solved: 39
[Submit][Status][Web Board]

Description

N opaque rectangles (1 <= N <= 1000) of various colors are placed on a white sheet of paper whose size is A wide by B long. The rectangles are put with their sides parallel to the sheet‘s borders. All rectangles fall within the borders of the sheet so that different figures of different colors will be seen.

The coordinate system has its origin (0,0) at the sheet‘s lower left corner with axes parallel to the sheet‘s borders.

 

Input

This problem includes multiple cases. The first line of input is a integer T represents the number of cases. 
For each case: The order of the input lines dictates the order of laying down the rectangles. The first input line is a rectangle "on the bottom".
Line 1: A, B, and N, space separated (1 <= A,B <= 10,000)
Lines 2-N+1: Five integers: llx, lly, urx, ury, color: the lower left coordinates and upper right coordinates of the rectangle whose color is `color‘ (1 <= color <= 2500) to be placed on the white sheet. The color 1 is the same color of white as the sheet upon which the rectangles are placed.

 

Output

For each case, output corresponding results separately.
The output of each case should contain a list of all the colors that can be seen along with the total area of each color that can be seen (even if the regions of color are disjoint), ordered by increasing color. Do not display colors with no area.

 

Sample Input

1
20 20 3
2 2 18 18 2
0 8 19 19 3
8 0 10 19 4

Sample Output

1 91
2 84
3 187
4 38

HINT

 

Note that the rectangle delineated by 0,0 and 2,2 is two units wide and two high. Here‘s a schematic diagram of the input:


11111111111111111111

33333333443333333331

33333333443333333331

33333333443333333331

33333333443333333331

33333333443333333331

33333333443333333331

33333333443333333331

33333333443333333331

33333333443333333331

33333333443333333331

33333333443333333331

11222222442222222211

11222222442222222211

11222222442222222211

11222222442222222211

11222222442222222211

11222222442222222211

11111111441111111111

11111111441111111111


The ‘4‘s at 8,0 to 10,19 are only two wide, not three (i.e., the grid contains a 4 and 8,0 and a 4 and 8,1 but NOT a 4 and 8,2 since this diagram can‘t capture what would be shown on graph paper).

 

在矩形中涂色覆盖问每种颜色最后有多少块

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL long long
const double pi = acos(-1.0);
#define Len 200005
#define mod 19999997
const int INF = 0x3f3f3f3f;
#define exp 1e-6
int seq[1001][5];
int col[1001],col_num[2501];
int A,B;
int n;
long long dfs(int begin,int a,int b,int c,int d)
{
    if(a >= c || b >= d) return 0;
    for(int i = begin; i <= n; i++)
    {
        int x1=seq[i][1],y1=seq[i][2],x2=seq[i][3],y2=seq[i][4];
        if(! (a>=x2 || b>=y2 || c<=x1 || d<=y1) )
        {
            if(a < x1 && c > x1)
            {
                return dfs(i, a, b, x1, d)+dfs(i, x1, b, c, d);
            }
            else if(a < x2 && c > x2)
            {
                return dfs(i, a, b,x2, d) + dfs(i,x2, b, c, d);
            }
            else
                return dfs(i, a, b, c, y1) + dfs(i, a,y2, c, d);
        }
    }
    return ((c - a) * (d - b));
}
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d%d%d",&A,&B,&n);
        seq[0][1] = seq[0][2] = 0;
        seq[0][3] = A,seq[0][4] =B,col[0] = 1;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d%d%d%d",&seq[i][1],&seq[i][2],&seq[i][3],&seq[i][4],&col[i]);
        }
        memset(col_num, 0 ,sizeof(col_num));
        for(int i = n; i >= 0; i--)
        {
            col_num[col[i]] += dfs(i+1,seq[i][1],seq[i][2],seq[i][3],seq[i][4]);
        }
        for(int i = 1; i <= 2500; i++)
        {
            if(col_num[i] > 0)
            printf("%d %d\n",i,col_num[i]);
        }
 
    }
    return 0;
 
}
 
/**************************************************************
    Problem: 1589
    User: aking2015
    Language: C++
    Result: Accepted
    Time:16 ms
    Memory:1516 kb
****************************************************************/

 

 

Shaping Regions(dfs)

标签:

原文地址:http://www.cnblogs.com/yuyixingkong/p/4471221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!