Luo Gu P1879 [USACO06NOV] solution to a problem in corn fields Corn Fields

P1879 [USACO06NOV] Corn Field Corn Fields

Title Description

Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't be planted. Canny FJ knows that the cows dislike eating close to each other, so when choosing which squares to plant, he avoids choosing squares that are adjacent; no two chosen squares share an edge. He has not yet made the final choice as to which squares to plant.

Being a very open-minded man, Farmer John wants to consider all possible options for how to choose the squares for planting. He is so open-minded that he considers choosing no squares as a valid option! Please help Farmer John determine the number of ways he can choose the squares to plant.

Farmer John bought a new pasture a new rectangular piece of pasture is divided into M rows and N columns (1 ≤ M ≤ 12; 1 ≤ N ≤ 12), each cell is a square of land. John intends to Gerry planted on a few delicious pasture grass for his cows to enjoy.

Unfortunately, some quite barren land can not be used grass. And, an exclusive piece of grass cows like the feeling, so John would not choose two adjacent land, that is to say, no two have a common edge grass.

John wants to know, if you do not consider the total number of blocks in the grass, then, a total number of species for planting program he chose? (Of course, the new completely abandoned ranch is also a program)

Input Format

The first line: two integers M and N, separated by spaces.

2 through M + 1 rows: each row comprising N integers separated by a space, described in the state of each plot. I + 1-described land line i-th row, are all an integer of 0 or 1, then 1 is, showing sufficient fertile land, 0 indicates not suitable grass land.

Output Format

An integer that ranch allocation scheme is divided by the total number of 100,000,000.

Sample input and output

Input # 1

2 3
1 1 1
0 1 0

Output # 1

9

[Thinking]

DP-like pressure
-like pressure DP entry title
need only consider the left and right and up and down four directions, so it is quite easy

When pressed with [DP like it? ]

With shaped pressure DP when general data are particularly small range
so that the range will be able to see the data

[Title] effect

Not given sequence and contradictions, not up and down and contradictions, and not about contradictions
Evaluating the number of
first contradiction means that this program there are grass areas and barren land where overlap occurs
two rear means there is no up and down the grass suffer the

[Core] ideas

First deal with a given situation barren land
then enumerate every situation that may arise
to determine whether he has a case of the grass around the adjacent
mark what
then is the DP process
to enumerate what are marked in the following loop inside Out

[Complete code]

#include<iostream>
#include<cstdio>
#define int long long 
using namespace std;
const int mo = 1e9;
const int Max = 15;
int a;
int f[Max];//这一行草地的情况 
int ff[Max][5000]; //第i行选j会有多少种方案 
bool s[5000];//左右合不合法 
int read()
{
    int sum = 0,fg = 1;
    char c = getchar();
    while(c < '0' || c > '9')
    {
        if(c == '-')fg = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9')
    {
        sum = (sum * 10) + c - '0';
        c = getchar();
    }
    return sum * fg;
}

signed main()
{
    int m = read(),n = read();
    for(register int i = 1;i <= m;++ i)
        for(register int j = 1;j <= n;++ j)
            a = read(),f[i] = (f[i] << 1) + a;
    int MM = (1 << n);
    for(register int i = 0;i < MM;++ i)
        s[i] = ((i & (i << 1)) == 0) && ((i & (i >> 1)) == 0);
    ff[0][0] = 1;//没有是一定成立的qwq 
    for(register int i = 1;i <= m;++ i)//枚举到了第几行 
        for(register int j = 0;j < MM;++ j)//枚举第i行选什么 
            if(s[j] && (j & f[i]) == j)//如果枚举到选择的j是不会出现左右相邻而且不会在这一行不该出现草的地方出现草 
                for(register int k = 0;k < MM;++ k)//枚举i-1行选的什么 
                    if((k & j) == 0)//这两行没有相邻的 
                        ff[i][j] = (ff[i][j] + ff[i - 1][k]) % mo;
    int M = 0;
    for(register int i = 0;i < MM;++ i)
        M += ff[m][i],M %= mo;
    cout << M << endl;
    return 0; 
}

Guess you like

Origin www.cnblogs.com/acioi/p/11746803.html