ToJ 2850 corn fields pressure DP

Source: Internet
Author: User

Source: http://acm.tju.edu.cn/toj/showp.php? PID = 1, 2850

N * m land is planted in different locations. Each location can be either planted or unavailable. A scheme must not be planted adjacent to each other. Ask the number of valid schemes. (A bit messy)

Analysis: I have worked as an artillery position. This is a second kill.

 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5  6 const int mo = 100000000; 7 int n, m, size, maxl; 8 int map[15][15]; 9 int s[10000];10 int dp[15][10000];11 void dfs()12 {13     size = 0;14     maxl = 1 << m;15     for (int i = 0; i < maxl; i++)16         if ((i & (i << 1)) == 0) s[size++] = i;17 }18 bool ok(int row, int id)19 {20     int x = s[id], tmp;21     for (int i = m; i > 0; i--){22         tmp = x % 2;23         if (tmp == 1 && map[row][i] == 0) return false;24         x /= 2;25     }26     return true;27 }28 int main()29 {30     while(scanf("%d %d", &n, &m) != EOF)31     {32         for (int i = 1; i <= n; i++)33             for (int j = 1; j <= m; j++)34                 scanf("%d", &map[i][j]);35         memset(dp, 0, sizeof(dp));36         dp[0][0] = 1;37         dfs();38         for (int i = 1; i <= n; i++){39             for (int j = 0; j < size; j++){40                 if (ok(i, j)){41                     for (int k = 0; k < size; k++)42                         if ((s[k] & s[j]) == 0) dp[i][j] = (dp[i][j] + dp[i-1][k]) % mo;43                 }44             }45         }46         int ans = 0;47         for (int i = 0; i < size; i++) ans = (ans + dp[n][i]) % mo;48         printf("%d\n", ans);49     }50     return 0;51 }

 

ToJ 2850 corn fields pressure 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.