hdu3779 Railroad 记忆化搜索

http://acm.hdu.edu.cn/showproblem.php?pid=3779

记忆化搜索...没其他说的了

#include<cstdio>
#include<cstring>

#define N 1002

int hash[N][N];
int a[N],b[N],c[2*N];
int n,m;

int dfs(int a_pos,int b_pos,int c_pos)
{
	if(a_pos>n&&b_pos>m)
		return 1;
	if(hash[a_pos][b_pos]!=-1)
		return hash[a_pos][b_pos];
	if(a_pos<=n&&a[a_pos]==c[c_pos])
	{
		if(dfs(a_pos+1,b_pos,c_pos+1))
		{
			hash[a_pos+1][b_pos]=1;
			return 1;
		}
		hash[a_pos+1][b_pos]=0;
	}
	if(b_pos<=m&&b[b_pos]==c[c_pos])
	{
		if(dfs(a_pos,b_pos+1,c_pos+1))
		{
			hash[a_pos][b_pos+1]=1;
			return 1;
		}
		hash[a_pos][b_pos+1]=0;
	}
	return 0;
}

int main()
{
	while(scanf("%d%d",&n,&m)==2&&(n||m))
	{
		memset(hash,-1,sizeof(hash));
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		for(int i=1;i<=m;i++)
			scanf("%d",&b[i]);
		for(int i=1;i<=n+m;i++)
			scanf("%d",&c[i]);
		if(dfs(1,1,1))
			printf("possible\n");
		else
			printf("not possible\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值