Codeforces Round #141 (Div. 2) C. Fractal Detector(神奇的状压DP)(好题)

C. Fractal Detector
time limit per test
4 seconds
memory limit per test
256 megabytes
standard input
standard output

Little Vasya likes painting fractals very much.

He does it like this. First the boy cuts out a 2 × 2-cell square out of squared paper. Then he paints some cells black. The boy calls the cut out square a fractal pattern. Then he takes a clean square sheet of paper and paints a fractal by the following algorithm:

  1. He divides the sheet into four identical squares. A part of them is painted black according to the fractal pattern.
  2. Each square that remained white, is split into 4 lesser white squares, some of them are painted according to the fractal pattern. Each square that remained black, is split into 4 lesser black squares.

In each of the following steps step 2 repeats. To draw a fractal, the boy can make an arbitrary positive number of steps of the algorithm. But he need to make at least two steps. In other words step 2 of the algorithm must be done at least once. The resulting picture (the square with painted cells) will be a fractal. The figure below shows drawing a fractal (here boy made three steps of the algorithm).

One evening Vasya got very tired, so he didn't paint the fractal, he just took a sheet of paper, painted a n × m-cell field. Then Vasya paint some cells black.

Now he wonders, how many squares are on the field, such that there is a fractal, which can be obtained as described above, and which is equal to that square. Square is considered equal to some fractal if they consist of the same amount of elementary not divided cells and for each elementary cell of the square corresponding elementary cell of the fractal have the same color.


The first line contains two space-separated integers n, m (2 ≤ n, m ≤ 500) — the number of rows and columns of the field, correspondingly.

Next n lines contain m characters each — the description of the field, painted by Vasya. Character "." represents a white cell, character "*" represents a black cell.

It is guaranteed that the field description doesn't contain other characters than "." and "*".


On a single line print a single integer — the number of squares on the field, such that these squares contain a drawn fractal, which can be obtained as described above.

Sample test(s)
6 11
4 4

The answer for the first sample is shown on the picture below. Fractals are outlined by red, blue and green squares.

The answer for the second sample is 0. There is no fractal, equal to the given picture.

大致题意:行列500的黑白方格内有多少个分形图形 ,分形图形的定义在题目中给了

思路:拿到这个题目完全没思路啊,反正知道是dp,然后把每个分形图形都搞成一个状态,首先需要xy坐标,然后还需要一个基本分形图形,一共有1<<4种基本分形图形(全黑和全白也算),然后这样还不够,还要搞一个状态表示是多少个基本分形嵌套过来的,就是k阶分形。所以用四维状压dp可以做:bool dp[N][N][1<<4][10];


//GNU C++	Accepted	3742 ms	47700 KB
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
using namespace std;
typedef long long ll;

const int N = 550;
int n,m;
char mp[N][N];
bool dp[N][N][1<<4][10];
int main()
        dp[i][j][(1<<4)-1][0] = (mp[i][j] == '*');
        for(int mask = 0;mask < (1<<4)-1;mask++) dp[i][j][mask][0] = (mp[i][j] == '.');
    for(int mask = 0;mask < (1<<4);mask++)
        int d = (1<<(k-1));
        REP(x,n) REP(y,m)
            int nx[4]={x,x,x+d,x+d},ny[4]={y,y+d,y,y+d};
            bool flag = 1;
            for(int p = 0;p <= 3;p++)
                int xx = nx[p],yy = ny[p];
                if(xx > n || yy > m)
                    flag = 0;
                if(mask & (1<<p)) flag &= dp[xx][yy][(1<<4)-1][k-1];
                else flag &= dp[xx][yy][mask][k-1];
            dp[x][y][mask][k] = flag;
    int ans = 0;
    REP(x,n) REP(y,m) for(int mask = 0;mask < (1<<4);mask++)
    for(int k = 2;k <= 9;k++) ans += dp[x][y][mask][k];

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kalilili/article/details/46821721
个人分类: 状压DP
想对作者说点什么? 我来说一句