Corn Fields
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 9623 | Accepted: 5092 |
Description
Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 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.
Input
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)
Output
Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.
Sample Input
2 3 1 1 1 0 1 0
Sample Output
9
Hint
Number the squares as follows:
1 2 3 4
There are four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on two squares (13, 14, or 34), 1 way to plant on three squares (134), and one way to plant on no squares. 4+3+1+1=9.
题目大意:
给你一个N*M的矩阵,矩阵里的元素由0和1组成,1代表肥沃的土地可以种草,0则不可以种草。如下: N=2 M=3 1 1 1 0 1 0 现有若干头牛,请将它们放入有草的地方吃草,注意上下左右不能相邻。 那么问题来了,请问有多少种放法?
分析:
1 #include<iostream> 2 #include<sstream> 3 #include<stdio.h> 4 #include<string> 5 #include<string.h> 6 #include<math.h> 7 #include<time.h> 8 #include<algorithm> 9 10 #define LEN 1000000 11 #define INF 99999 12 #define ALLSTATES 4096 //最大状态 13 14 using namespace std; 15 16 int allstates=0; 17 int n,m; 18 int F[13][ALLSTATES]={0}; //方法数 19 int Matrix[13][13]={0};//土地的样子 20 21 bool andMatrix(int row,int states)//是否和土地兼容 22 { 23 for(int i=1;i<=m;i++) 24 { 25 if(Matrix[row][i]==0)//如果这里是空的 那么肯定不能放牛 26 { 27 if(states&(1<<i-1)) 28 { 29 return false; 30 } 31 } 32 33 } 34 return true; 35 } 36 37 bool linetest(int states)//判断行是否相邻 38 { 39 int i=0; 40 41 while(i<m) 42 { 43 if(states&(1<<i))//如果是1 那么你的左边不应该是1 44 { 45 if(i+1<m && states&(1<<(i+1))){ return false; }//是1返回错误 46 else{i+=2;}//不是1 跳过一格 47 } 48 else{ i++; } 49 } 50 return true; 51 } 52 53 bool upanddown(int upstates,int downstates)//判断上下是否相邻 54 { 55 int i=0; 56 57 while(i<m) 58 { 59 if(upstates&(1<<i) && downstates&(1<<i)) 60 { 61 return false; 62 } 63 else 64 { 65 i++; 66 } 67 68 } 69 return true; 70 } 71 72 int main() 73 { 74 //读入-------------------------------------------- 75 cin>>n>>m; 76 //读入矩阵 77 for(int i=1;i<=n;i++) 78 for(int j=1;j<=m;j++) 79 { 80 cin>>Matrix[i][j]; 81 } 82 //处理-------------------------------------------- 83 allstates=1<<m;//m个格子的所有状态 84 // cout<<andMatrix(1,5)<<endl; 85 // cout<<linetest(5)<<endl; 86 //cout<<allstates; 87 //先处理第一行 88 for(int j=0;j<allstates;j++)//allstates为所有状态 89 { 90 if(andMatrix(1,j) && linetest(j))// 91 { 92 F[1][j]=1; 93 } 94 } 95 //cout<<allstates; 96 //处理后面的行数 97 for(int row=2;row<=n;row++) 98 for(int j=0;j<allstates;j++) 99 { 100 if(andMatrix(row,j)==0 || !linetest(j))//如果j不符合土地直接跳过 101 { 102 continue; 103 } 104 for(int k=0;k<allstates;k++) 105 { 106 if(F[row-1][k] && upanddown(j,k)) 107 { 108 F[row][j]+=F[row-1][k]; 109 } 110 } 111 } 112 //统计所有方法数 113 int ct=0; 114 115 //for(int i=1;i<=n;i++) 116 // { 117 // for(int j=0;j<allstates;j++) 118 // { 119 // cout<<F[i][j]; 120 // } 121 // cout<<endl; 122 // } 123 //cout<<ct; 124 for(int j=0;j<allstates;j++) 125 { 126 ct+=F[n][j]; 127 ct%=100000000; 128 } 129 cout<<ct; 130 131 return 0; 132 }
2015-07-26 13:21:58