题目链接:http://poj.org/problem?id=3254
题目大意:给你一个n*m的矩阵,其中1代表可以放牛,0代表不能放牛,并且每两头牛之间不能相邻,问有多少种放牛的方案。1头牛也不放也表示一种方案。
思路是很清楚的,就是每一列用一个二进制数表示状态,先预处理出所有仅满足两头牛不相邻 的状态,然后再和给的地图做&运算。可是一开始数组开反了,可POJ一直报的WA,一直改啊改。。。。。/尴尬
/*#include <iostream>
#include <cstring>
#define mo 100000000
using namespace std;
int cur[20],state[600];
long long dp[600][20];
int n,m,tot;
bool judge(int x)
{
if(x & (x<<1)) return false;
return true;
}
void init()
{
tot=0;
memset(dp,0,sizeof(dp));
memset(cur,0,sizeof(cur));
memset(state,0,sizeof(state));
for(int i=0;i<(1<<m);i++)
{
if(judge(i)) state[++tot]=i;
}
}
bool fit(int x, int k)
{
if (x & cur[k]) return false;
return true;
}
int main()
{
cin>>n>>m;
init();
for(int i=1;i<=n;i++)
{
int t;
for(int j=1;j<=m;j++)
{
cin>>t;
if(t==0) cur[i]+=(1<<(m-j));
}
}
for(int i=1;i<=tot;i++)
{
if(fit(state[i], 1)) dp[1][i]=1;
}
for(int r=2;r<=n;r++)
{
for(int k=1;k<=tot;k++)
{
if(!fit(state[])) continue;
for(int j=1;j<=tot;j++)
{
if(cur[r-1]& state[j]) continue;
if(state[k] & state[j]) continue;
dp[r][k] = (dp[r][k] + dp[r-1][j])% mo;
}
}
}
for (int i=1; i<=n; i++)
{
for (int j=1; j<=tot; j++)
cout<<dp[i][j]<<' ';#include <iostream>
#include <cstring>
#define mo 100000000
using namespace std;
int cur[15],state[600];
long long dp[600][15];
int n,m,tot;
bool judge(int x) //满足条件返回ture
{
if(x & x<<1) return false;
return true;
}
void init()
{
tot=0;
memset(dp,0,sizeof(dp));
for(int i=0;i<(1<<n);i++)
{
if(judge(i)) state[tot++]=i;
}
}
int main()
{
int t;
while(cin>>m>>n)
{
for(int i=0;i<m;i++)
{
cur[i]=0;
for(int j=0;j<n;j++)
{
cin>>t;
if(t==0) cur[i]+=1<<(n-j-1);
}
}
init();
for(int i=0;i<tot;i++)
{
if(!(state[i]&cur[0])) dp[0][i]=1;
}
for(int r=1;r<m;r++)
{
for(int k=0;k<tot;k++)
{
if(cur[r]&state[k]) continue;
for(int j=0;j<tot;j++)
{
if(cur[r-1]&state[j]) continue;
if(state[k] & state[j]) continue;
dp[r][k] = (dp[r][k] + dp[r-1][j])%mo;
}
}
}
long long ans=0;
for(int i=0;i<tot;i++)
ans = (ans+dp[m-1][i])%mo;
cout<<ans<<endl;
}
return 0;
}
cout<<endl;
}
int ans=0;
for(int i=1;i<=tot;i++)
ans = (ans+dp[n][i])%mo;
cout<<ans<<endl;
return 0;
}
*/
#include <iostream>
#include <cstring>
#define mo 100000000
using namespace std;
int cur[15],state[600];
long long dp[15][600];
int n,m,tot;
bool judge(int x)
{
if(x & x<<1) return false;
return true;
}
void init()
{
tot=0;
memset(dp,0,sizeof(dp));
for(int i=0;i<(1<<n);i++)
{
if(judge(i)) state[tot++]=i;
}
}
int main()
{
int t;
while(cin>>m>>n)
{
for(int i=0;i<m;i++)
{
cur[i]=0;
for(int j=0;j<n;j++)
{
cin>>t;
if(t==0) cur[i]+=1<<(n-j-1);
}
}
init();
for(int i=0;i<tot;i++)
{
if(!(state[i]&cur[0])) dp[0][i]=1;
}
for(int r=1;r<m;r++)
{
for(int k=0;k<tot;k++)
{
if(cur[r]&state[k]) continue;
for(int j=0;j<tot;j++)
{
if(cur[r-1]&state[j]) continue;
if(state[k] & state[j]) continue;
dp[r][k] = (dp[r][k] + dp[r-1][j])%mo;
}
}
}
long long ans=0;
for(int i=0;i<tot;i++)
ans = (ans+dp[m-1][i])%mo;
cout<<ans<<endl;
}
return 0;
}