[UVA][DP最大矩形] 10667 - Largest Block@Morris' Blog|PChome Online 人新台
2012-05-16 08:58:38| 人572| 回0 | 上一篇 | 下一篇

[UVA][DP最大矩形] 10667 - Largest Block

0 收藏 0 0 站台

  Largest Block  

The Problem

Consider a n x n chessboard. The term block(r1,c1,r2,c2) denotes the rectangular subset of squares defined by the intersection of rows {r1,r1+1,...,r2} and columns {c1,c1+1,...,c2}.

There are several occupied blocks on the board. We are interested in the largest block (in the sense of maximum area) that can be placed in the free space remaining in the board.

For example, in a chessboard of size 10, if block(2,2,5,3), block(8,3,9,7), and block(3,6,3,8) represent occupied space, then the largest block that can be placed in free space has area 28. This can be visually checked in the following figure:

r\c 1 2 3 4 5 6 7 8 9 10

1











2


X X






3


X X

X X X

4


X X   o   o  &bsp;o   o   o   o   o

5


X X   o   o   o   o   o   o   o

6




  o   o   o   o   o   o   o

7




  o   o   o   o   o   o   o

8



X X X X X


9



X X X X X


10











We are interested only in the area of the largest free block, and not in its particular location. Therefore, each instance of the problem has a unique solution.

The Input

The program first reads the number p of instances of the problem. Each instance is described by the size s of the board, the number b of blocks of occupied space, and the vertices r1,c1,r2,c2, of each block:

p number of problem instances in the file
s (board size) instance #1
b (number of blocks)
r1 c1 r2 c2 (first block)
r1 c1 r2 c2 (second block)
... ...
r1 c1 r2 c2 (n-th block)
s (board size) instance #2
b (number of blocks)
r1 c1 r2 c2 (first block)
r1 c1 r2 c2 (second block)
... ...
r1 c1 r2 c2 (n-th block)
... ... instance #p

Assumptions:

  • 1 <= s <= 100
  • 0 <= b <= 100
  • 1 <= r1 <= r2 <=s
  • 1 <= c1 <= c2 <=s
  • Occupied blocks may overlap.

The Output

For each test case the output consists of a integer indicating the area of the largest block that can be located in the available free squares.

Sample Input

3 10 3 2 2 5 3 8 3 9 7 3 6 3 8 20 1 1 1 1 1 10 2 5 1 5 10 1 5 10 5

Sample Output

28 380 25

最大矩形, DP O(n*n*n)

#include <stdio.h>

int main() {
    int p, s, b;
    int r1, r2, c1, c2, i, j, k;
    scanf("%d", &p);
    while(p--) {
        scanf("%d %d", &s, &b);
        char map[101][101] = {};
        while(b--) {
            scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
            for(i = r1; i <= r2; i++)
                for(j = c1; j <= c2; j++)
                    map[i][j] = 1;
        }
        int length, width, tmp = 0, ans = 0;
        for(i = 1; i <= s; i++) {
            int sum[101] = {};
            for(j = i; j <= s; j++) {
                for(k = 1; k <= s; k++) {
                    sum[k] += !map[j][k];
                    if(k == 1 || tmp != length*width)
                        tmp = 0, length = 0;
                    tmp += sum[k];
                    length++, width = j-i+1;
                    if(tmp == length*width) {
                        if(tmp > ans)
                            ans = tmp;
                    }
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

台: Morris
人(572) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA] 11703 - sqrt log sin
此分上一篇:[UVA] 496 - Simply Subsets

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86