Poj 3254 corn fields-type DP

Source: Internet
Author: User

Link: http://poj.org/problem? Id = 3254

A m * n field. The size of each small area is 1*1. It can be marked as 1. It cannot be marked as 0, in addition, plants cannot be planted in two adjacent places at the same time. The problem is how many different planting schemes are available (none in all regions is also a planting scheme)

Idea: This is the first pressure DP question, from the first line to the last line, each row uses an N-bit binary number to indicate the state 1 of the row. 1 indicates that plants are planted at the location, and 0 indicates that no plants are planted at the location. Because each line of land only affects the planting of adjacent land, the status of each line can be obtained using the state of the previous line.

MATERIALS: http://www.doc88.com/p-771373748581.html

Code:

# Include <iostream> # include <set> # include <map> # include <queue> # include <cstring> # include <string> # include <algorithm> # include <cstdio> # define mod 100000000 using namespace STD; int DP [15] [40000]; bool judge (int x) {If (X & (x <1) = 0) return 1; return 0 ;} // The function is used to determine whether plants are planted on an adjacent land in the I state. If not, 1int main () {int row, Col, X; int field [15] is returned. memset (field, 0, sizeof (field); memset (DP, 0, sizeof (DP); scanf ("% d", & Row, & col ); for (INT I = 0; I <row; I ++) {for (Int J = 0; j <Col; j ++) {scanf ("% d ", & X); field [I] * = 2; field [I] + = x;} // cout <field [I] <Endl ;} for (INT I = 0; I <(1 <col); I ++) {If (field [0] & I) = I & judge (I) DP [0] [I] = 1 ;}for (INT I = 1; I <row; I ++) {for (Int J = 0; j <(1 <col); j ++) {If (field [I] & J) = J & judge (j) {for (int K = 0; k <(1 <col); k ++) {If (K & J) = 0 & judge (k) {DP [I] [J] + = DP [I-1] [k]; DP [I] [J] % = mod ;}}} int ans = 0; For (INT I = 0; I <(1 <col ); I ++) {ans + = DP [row-1] [I]; ans % = MOD;} printf ("% d \ n", ANS); Return 0 ;}


Poj 3254 corn fields-type DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.