G:Corn Fields

G:Corn Fields



总时间限制: 
2000ms 
内存限制: 
65536kB
描述

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.


输入
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)
输出
Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.
样例输入
2 3
1 1 1
0 1 0
样例输出
9
提示
Number the squares as follows:
1 2 3


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.
来源
USACO 2006 November Gold


#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

typedef long  ll;
#define rep(i,b,e) for(long i = b ; i < e ; i++)
#define clr(arr) memset(arr,0,sizeof(arr))
ll arr[12];
ll state[ 1 << 12];
ll col;
ll raw;
ll state_num;
ll dp[12][1 << 12];
bool check(ll x)
{
	if( x & ( x << 1 ))
	{
		return false;
	}
	return true;
}

void init() {
	state_num = 0;
	rep(i,0, 1 << col)
	{
		if( check( i ) )
		{
			state[state_num++] = i;
		}
	}
}



int main()
{
	ll m , n ;
	while(cin >> m >> n)
	{
		clr(arr);
		clr(dp);
		clr(state);
		rep(i,0,m)
		{
			ll t = 0;
			rep(j,0,n)
			{
				char temp;
				cin >> temp;
				getchar();
				t = t * 2 + (temp-'0');
			}
			arr[i]=t;
		}
		col = n;
		raw = m;
		init();
		rep(k,0,raw)
		{

			rep(q,0,state_num)
			{
				if((arr[k] | state[q]) == arr[k])
				{
					if(k == 0)
					{
						dp[0][state[q]] = 1;
					}
					else
					{
						rep(p,0,state_num)
						{
							if((arr[k-1] | state[p]) == arr[k-1])
							{
								if(!(state[p]&state[q]))
								{
									dp[k][state[q]] += dp[k-1][state[p]];
								}
							}
						}
					}
				}
			}


		}
		ll ans=0;
		rep(kk,0,1 << col)
		{
			ans = ( ans + dp[raw-1][kk] ) % 100000000;
		}
		cout << ans << endl;
	}

	return 0;
}


你可能感兴趣的:(ACM)