poj1741,[POJ3254]Corn Fields

 2023-11-19 阅读 24 评论 0

摘要:[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

[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.

poj1741,\(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 100000000int 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

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/179708.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息