UVALive 6914 Maze Mayhem 轮廓线dp

Maze Mayhem

题目连接:

(https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4926)

题意

n*m的格子,给你一个k,表示你最多可以放的障碍数,问从(1,1)不能到(n,m)
的放置障碍的方案数

题解:

轮廓线dp,1表示可以走到这里。转移有3种:走这里,不放障碍;不走这里,放障碍;放障碍

再次写轮廓线,感觉有点熟练了起来

代码

//#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <bitset>
#include <string>
#include <time.h>
using namespace std;
long double esp=1e-11;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define cle(a) while(!a.empty())a.pop()
#define mem(p,c) memset(p,c,sizeof(p))
#define mp(A, B) make_pair(A, B)
#define pb push_back
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
typedef long long int LL;
const long double PI = acos((long double)-1);
const LL INF=0x3f3f3f3f3f3f3f3fll;
const int MOD =1000000007ll;
const int maxn=2000100;

const int HASH=30007;
const int STATE=1<<15;
struct HASHMAP
{
    int head[HASH],next[STATE],sz;
    LL state[STATE];
    LL dp[STATE];
    void init()
    {
        sz=0;
        memset(head,-1,sizeof(head));
    }
    void push(LL st,LL k,LL ans)
    {
        int i;
        st=(st<<7)+k;
        int h=st%HASH;
        for(i=head[h];i!=-1;i=next[i])//这里要注意是next
          if(state[i]==st)
          {
              dp[i]=(dp[i]+ans)%MOD;
              //dp[i]+=ans;
              return;
          }
        state[sz]=st;
        dp[sz]=ans;
        next[sz]=head[h];
        head[h]=sz++;
    }
}hm[2];
int m,f;
int mask[10];
void decode(int st)
{
	st>>=7;
	for(int x=0;x<m;x++)
		mask[x]=st>>x&1;
}
void dfs(int st,int k,int t,LL ans)
{
	if(t==m)
	{
		hm[f^1].push(st,k,ans);
		return ;
	}
	if((t&&(st>>(t-1)&1))||mask[t])
		dfs(st|1<<t,k,t+1,ans);
	else
		dfs(st,k,t+1,ans);
	if(k)dfs(st,k-1,t+1,ans);
}
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("inlay.in", "r", stdin);
    //freopen("out.txt", "w", stdout);
    //::iterator iter;                  %I64d
    //for(int x=1;x<=n;x++)
    //for(int y=1;y<=n;y++)
    //scanf("%d",&a);
    //printf("%d\n",ans);
    int T;
    scanf("%d",&T);
    for(int gg=1;gg<=T;gg++)
	{
		int n,k,get=0x7f;
		scanf("%d%d%d",&n,&m,&k);
		hm[1].init();
		hm[1].push(1,k,1);
		f=1;
		for(int x=1;x<=n;x++)
		{

			hm[f^1].init();
			for(int y=0;y<hm[f].sz;y++)
			{
				decode(hm[f].state[y]);
				dfs(0,hm[f].state[y]&get,0,hm[f].dp[y]);
			}
			f^=1;
			/*for(int y=0;y<hm[f].sz;y++)
			{
				decode(hm[f].state[y]);
				for(int z=0;z<m;z++)putchar('0'+mask[z]);printf(" %d %d\n",hm[f].state[y]&get,hm[f].dp[y]);
			}*/
		}
		LL ans=0;
		for(int y=0;y<hm[f].sz;y++)
		{
			decode(hm[f].state[y]);
			if(mask[m-1]==0)
				ans=(ans+hm[f].dp[y])%MOD;
		}
		printf("Case #%d: %lld\n",gg,ans);
	}
    return 0;
} //

posted @ 2016-08-03 21:08  femsub  阅读(219)  评论(0编辑  收藏  举报