POJ 3254 Corn Fields state compression DP (C ++/Java)

Source: Internet
Author: User

POJ 3254 Corn Fields state compression DP (C ++/Java)

 

Question:

Where a farmer has n rows and m columns, each grid uses 1 to indicate that grass can be planted, but 0 cannot. Cattle can only be placed on grass, but not on adjacent grass. Ask how many methods are there.

Ideas:

Status compressed DP.

Binary numbers can be used to indicate grazing conditions and determine whether the conditions are met.

There are two restrictions for this question:

1. Lawn restriction.

2. Adjacent restrictions.

 

For lawn restrictions, because 1 can be used to plant lawns during input.

For "11110" lawn analysis, only the last one cannot grow grass. The result is 00001. (Why? If this parameter is not used, we can cite the inverse examples)

Suppose there is a status 10101 which is not adjacent, but 10101 & 00001! = 0 indicates a conflict.

 

There are two adjacent restrictions: the limit of the same row and the limit of the upper and lower rows.

The limit of the same row can be removed from the adjacent conditions at the beginning, and the matching conditions are saved in the array, which helps reduce the number of states. This solves the problem.

The upper and lower lines can be connected to each other.

For example, (assuming grass can be planted)

10101 & 00100! = 0 is also conflicted.

 

OK.

 

C ++:

 

# Include
 
  
# Include
  
   
Const int mod = 100000000; const int MAXN = 1 <12; int map [20], status [MAXN], dp [20] [MAXN]; int len; int main () {int n, m; while (~ Scanf (% d, & n, & m) {for (int I = 0; I <n; I ++) {for (int j = 0; j <m; j ++) {int temp; scanf (% d, & temp); if (! Temp) map [I] = map [I] | (1 <(m-j-1);} len = 0; int tot = 1 <m; // All statuses for (int I = 0; I <tot; I ++) {// shifts left and right can be if (I & (I> 1 )) = 0) status [len ++] = I;} // initialize the first line of memset (dp, 0, sizeof (dp); for (int I = 0; I
   
    

 

 

 

 

JAVA:

import java.util.Scanner;public class Main {final static int mod = 100000000;final static int MAXN = 1 << 12;static int[] map = new int[20];static int[] status = new int[MAXN];static int[][] dp = new int[20][MAXN];static int len;public static void main(String[] args) {Scanner cin = new Scanner(System.in);int n, m;while (cin.hasNext()) {n = cin.nextInt();m = cin.nextInt();init(n,m);for (int i = 0; i < n; i++)for (int j = 0; j < m; j++) {int temp = cin.nextInt();if (temp ==0)map[i] = (map[i] | (1 << (m - j - 1)));}int tot = 1 << m;len = 0;for (int i = 0; i < tot; i++)if ((i & (i << 1)) == 0) {status[len++] = i;}for (int i = 0; i < len; i++) {if ((map[0] & status[i]) == 0)dp[0][i] = 1;}for (int i = 1; i < n; i++) {for (int j = 0; j < len; j++) {if ((map[i - 1] & status[j]) != 0)continue;for (int k = 0; k < len; k++) {if ((map[i] & status[k]) != 0)continue;if ((status[j] & status[k]) != 0)continue;dp[i][k] = (dp[i][k] + dp[i - 1][j]) % mod;}}}int ans = 0;for (int i = 0; i < len; i++) {ans = (ans + dp[n - 1][i]) % mod;}System.out.println(ans);}}public static void init(int n,int m){int tot=1<
     

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.