[POJ3254]Corn Fields

[POJ3254]Corn Fields

试题描述

Farmer John has purchased a lush new rectangular pasture composed of \(M\) by \(N\) \((1 \le M \le 12; 1 \le N \le 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.

\(N \times M\)($ \le 12$)的棋盘

每个格子有两种状态,可以放牧和不可以放牧,可以放牧用 1 表示,否则用 0 表示

在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻

问有多少种放牛方案

一头牛都不放也是一种方案,答案对 \(100000000\) 取模

输入

Line \(1\): Two space-separated integers: \(M\) and \(N\)

Lines \(2\)..\(M+1\): Line \(i+1\) describes row \(i\) of the pasture with \(N\) space-separated integers indicating whether a square is fertile (1 for fertile, 0 for infertile)

输出

Line \(1\): One integer: the number of ways that FJ can choose the squares modulo \(100,000,000\).

输入示例
2 3
1 1 1
0 1 0
输出示例
9
数据规模及约定

见“试题描述

题解

轮廓线 dp,直接写吧。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
#define rep(i, s, t) for(int i = (s); i <= (t); i++)
#define dwn(i, s, t) for(int i = (s); i >= (t); i--)

int read() {
    int x = 0, f = 1; char c = getchar();
    while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
    while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}

#define maxn 15
#define maxs 4096
#define MOD 100000000

int n, m, f[maxn][maxn][maxs];
bool Map[maxn][maxn];

#define fnxt f[j==m?i+1:i][j==m?1:j+1]

int main() {
    n = read(); m = read();
    rep(i, 1, n) rep(j, 1, m) Map[i][j] = read();
    
    int all = (1 << m) - 1;
    f[1][1][0] = 1;
    rep(i, 1, n) rep(j, 1, m) rep(s, 0, all) {
        int up = s >> m - 1 & 1, lft = s & 1;
        if(!up && (j == 1 || !lft) && Map[i][j]) (fnxt[(s<<1&all)|1] += f[i][j][s]) %= MOD;
        (fnxt[s<<1&all] += f[i][j][s]) %= MOD;
    }
    
    int ans = 0;
    rep(s, 0, all) (ans += f[n+1][1][s]) %= MOD;
    printf("%d\n", ans);
    
    return 0;
}

转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/7997410.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值