hdu 3779

简单记忆化搜索

View Code
#include<stdio.h>
#include<string.h>

int as[1005],bs[1005],cs[2005],ok,n,m;
char hash[1005][1005];

void dfs(int x,int y,int z)
{
    if(hash[x][y]||ok)return;
    if(z==n+m)ok = 1;
    hash[x][y] = 1;
    
    if(as[x]==cs[z]&&x<n)dfs(x+1,y,z+1);
    if(ok)return;
    if(bs[y]==cs[z]&&y<m)dfs(x,y+1,z+1);
}

int main()
{
    while(scanf("%d%d",&n,&m)==2)
    {
        if(!(n+m))break;
        
        for(int i = 0; i < n; ++ i) scanf("%d",as+i);
        for(int i = 0; i < m; ++ i) scanf("%d",bs+i);
        for(int i = 0; i < n + m; ++ i) scanf("%d",cs+i);
        
        memset(hash,0,sizeof(hash));
        ok = 0;
        
        dfs(0,0,0);
        
        if(ok)puts("possible");
        else puts("not possible");
    }return 0;    
}

posted on 2012-04-27 16:19  aigoruan  阅读(141)  评论(0)    收藏  举报

导航