zoj 3466 The Hive II

小hh插頭dp http://www.notonlysuccess.com/index.php/plug-dp-complete/ 的第二題。

這題比第一題難處理一點,但是方法是一樣的,用輪廓線掃一遍就好了,每個可行格子有且僅有兩個插頭,要分奇偶格子處理



兩條輪廓線就像這樣,然後層層掃描即可

Run ID Submit Time Judge Status Problem ID Language Run Time(ms) Run Memory(KB) User Name
3065240 2012-09-28 01:02:51 Accepted 3466 C++ 2470 2748 xym
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<iterator>
#include<stack>
#define LL long long
using namespace std;
int n,m;
int mov[2][4]={1,5,9,13,0,4,8,12};
int num[8]={0,1,1,2,1,2,2,3};
bool gp[10][10];
LL dp[5][1<<16];
char s[10];
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		memset(gp,0,sizeof(gp));
		for(int i=0;i<m;i++)
		{
			scanf("%s",s);
			gp[s[0]-'A'+1][s[1]-'A'+1]=1;
		}
		memset(dp,0,sizeof(dp));
		dp[4][0]=1;
		int sum=1<<16;
		for(int i=1;i<=n;i++)
		{
			for(int j=0;j<sum;j++)
			{
				if(j<(1<<15))
					dp[0][j]=dp[4][(j<<1)];
				else dp[0][j]=0;
			}
			for(int j=1;j<=4;j++)
				for(int sta=0;sta<sum;sta++)
					dp[j][sta]=0;
			for(int j=1;j<=4;j++)
			{
				for(int sta=0;sta<sum;sta++)
				{
					int k=(sta>>mov[0][j-1])&7;
					int st1=sta^(k<<mov[0][j-1]);
					if(gp[i][j*2])
					{
						if(k==0)
							dp[j][sta]=dp[j-1][sta];
						continue;
					}
					else
					{
						for(int x=0;x<8;x++)
						{
							if(num[k]+num[x]==2)
							{
								dp[j][sta]+=dp[j-1][st1^(x<<mov[0][j-1])];
							}
						}
					}
				}
			}
			for(int j=0;j<sum;j++)
				dp[0][j]=0;
			for(int j=0;j<sum;j++)
			{
				if(j<(1<<15))
					dp[0][j<<1]=dp[4][j];
			}
			for(int j=1;j<=4;j++)
				for(int sta=0;sta<sum;sta++)
					dp[j][sta]=0;
			for(int j=1;j<=4;j++)
			{
				for(int sta=0;sta<sum;sta++)
				{
					int k=(sta>>mov[1][j-1])&7;
					int st1=(sta^(k<<mov[1][j-1]));
					if(gp[i][j*2-1])
					{
						if(k==0)
							dp[j][sta]=dp[j-1][sta];
						continue;
					}
					else
					{
						for(int x=0;x<8;x++)
						{
							if(num[k]+num[x]==2)
							{
								dp[j][sta]+=dp[j-1][st1^(x<<mov[1][j-1])];
							}
						}
					}
				}
			}
		}
		cout<<dp[4][0]<<endl;
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章