zoj 2067 - White Rectangles

题目:一个由‘.’和‘#’组成矩形,统计里面‘.‘组成的矩形的个数。

分析:经典dp。计算之前要做很多预处理。

状态:f(i,j)为,以点(i,j)作为右下角顶点的矩形的个数;

首先,统计每个‘.‘元素,所在行以他为结束标志的连续的‘.‘的长度L(i,j);

然后,从点(i,j)向左上运动,计算对应的高度的矩形个数maxL(i,j,k);

转移:f(i,j)= sum(maxL(i,j,k)) { 其中,0 < k ≤ j };

(maxL(i,j,k)为从k到i的每行以j为结束标志的矩形最大公共长度)。

说明:(2011-11-02 12:25)。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char maps[ 105 ][ 105 ];
int  line[ 105 ][ 105 ];
int  minl[ 105 ][ 105 ];
int  rect[ 105 ][ 105 ];

int main()
{
    int n;
    while ( scanf("%d",&n) != EOF ) {
        for ( int i = 1 ; i <= n ; ++ i )
            scanf("%s",&maps[ i ][ 1 ]);

        memset( line, 0, sizeof( line ) );
        memset( rect, 0, sizeof( rect ) );
        for ( int i = 1 ; i <= n ; ++ i )
        for ( int j = 1 ; j <= n ; ++ j )
            if ( maps[ i ][ j ] == '.' )
                line[ i ][ j ] = line[ i ][ j-1 ]+1;

        for ( int i = 1 ; i <= n ; ++ i )
        for ( int j = 1 ; j <= n ; ++ j ) {
            int minL = line[ i ][ j ];
            for ( int k = i ; k >= 1 ; -- k ) {
                if ( minL > line[ k ][ j ] )
                    minL = line[ k ][ j ];
                if ( !minL ) break;
                rect[ i ][ j ] += minL;
            }
        }

        int sum = 0;
        for ( int i = 1 ; i <= n ; ++ i )
        for ( int j = 1 ; j <= n ; ++ j )
            sum += rect[ i ][ j ];

        printf("%d\n",sum);
    }
    return 0;
}
时间: 2024-10-27 01:40:58

zoj 2067 - White Rectangles的相关文章

ZOJ2067 White Rectangles 很好的DP递推啊

题意超级简单,给你一个n*n的图,点代表白色,井号代表黑色,问你白色的矩形一共多少个,一个点也算一个喔~ 一开始是假设dp[i][j]代表 点(i,j)的左上角一共多少个矩形,那么dp[n][n]就是答案了,可是这样直接去找,发现没有直接的状态转移关系可以找,只能重新看一下,然后我去找如何算出矩形个数的 比如是一个连续点的 ,矩形个数是1个,两个连续的点的,矩形个数为3个,三个连续的点的,矩形个数为6个,以此类推,n个连续点的矩形个数为 1 + 2 + 3 + 4 + ....+ n 然后就像这

【HDOJ】1510 White Rectangles

这个题目很好,变形的题目也很多.简单DP. 1 /* 1510 */ 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 6 #define MAXN 105 7 8 char map[MAXN][MAXN]; 9 int dp[MAXN][MAXN]; 10 11 int min(int a, int b) { 12 return a<b ? a:b; 13 } 14 15 int ma

Gazebo機器人仿真學習探索筆記(三)機器人模型

gazebo_models:https://bitbucket.org/osrf/gazebo_models 模型庫下載,可以參考如下命令: ~/Rob_Soft/Gazebo7$ hg clone https://bitbucket.org/osrf/gazebo_models 下載更改目錄下載到指定文件夾中. 模型庫的結構 目錄 配置等可以參考官方文檔,注意model.sdf. 當然也可以將自己制作的模型上傳到庫中,文檔中也有具體說明. code$ hg clone https://[ema

ZOJ (狗狗)1426 Counting Rectangles(暴力)

Counting Rectangles Time Limit: 2 Seconds      Memory Limit: 65536 KB We are given a figure consisting of only horizontal and vertical line segments. Our goal is to count the number of all different rectangles formed by these segments. As an example,

(环形DP) zoj 3310

W - Unrequited Love Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3310 Appoint description:  System Crawler  (2015-04-16) Description Owen had been through unrequited love with N MM for a long t

ZOJ 3780 Paint the Grid Again(隐式图拓扑排序)

Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or white). Leo has a magical brush which can paint any row with black color, or an

ZOJ 3671 Japanese Mahjong III

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3671 Japanese Mahjong III Time Limit: 2 Seconds      Memory Limit: 65536 KB Mahjong is a game of skill, strategy and calculation and involves a certain degree of chance. In this proble

ZOJ 3207 80ers&#39; Memory (F)

80ers' Memory Time Limit: 1 Second      Memory Limit: 32768 KB I guess most of us are so called 80ers, which means that we were born in the 1980's. This group of people shared a lot of common memories. For example, the Saint Seiya, the YoYo ball, the

White Streaks

White Streaks Time limit: 1.0 secondMemory limit: 64 MB The life of every unlucky person has not only black but also white streaks. The Martian Vas-Vas has a calendar in the form of an m × n table; he marks in this calendar days when he had bad luck.