hdu3779Railroad(记忆化搜索|dp)

Railroad

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 828    Accepted Submission(s): 334


Problem Description
A train yard is a complex series of railroad tracks for storing, sorting, or loading/unloading railroad cars. In this problem, the railroad tracks are much simpler, and we are only interested in combining two trains into one.
hdu3779Railroad(记忆化搜索|dp)_第1张图片
Figure 1: Merging railroad tracks.


The two trains each contain some railroad cars. Each railroad car contains a single type of products identified by a positive integer up to 1,000,000. The two trains come in from the right on separate tracks, as in the diagram above. To combine the two trains, we may choose to take the railroad car at the front of either train and attach it to the back of the train being formed on the left. Of course, if we have already moved all the railroad cars from one train, then all remaining cars from the other train will be moved to the left one at a time. All railroad cars must be moved to the left eventually. Depending on which train on the right is selected at each step, we will obtain different arrangements for the departing train on the left. For example, we may obtain the order 1,1,1,2,2,2 by always choosing the top train until all of its cars have been moved. We may also obtain the order 2,1,2,1,2,1 by alternately choosing railroad cars from the two trains.

To facilitate further processing at the other train yards later on in the trip (and also at the destination), the supervisor at the train yard has been given an ordering of the products desired for the departing train. In this problem, you must decide whether it is possible to obtain the desired ordering, given the orders of the products for the two trains arriving at the train yard.
 

Input
The input consists of a number of cases. The first line contains two positive integers N 1 N 2 which are the number of railroad cars in each train. There are at least 1 and at most 1000 railroad cars in each train. The second line contains N1 positive integers (up to 1,000,000) identifying the products on the first train from front of the train to the back of the train. The third line contains N 2 positive integers identifying the products on the second train (same format as above). Finally, the fourth line contains N 1+N 2 positive integers giving the desired order for the departing train (same format as above).

The end of input is indicated by N 1 = N 2 = 0.
 

Output
For each case, print on a line  possible if it is possible to produce the desired order, or  not possible if not.
 

Sample Input
 
   
3 3 1 2 1 2 1 1 1 2 1 1 2 1 3 3 1 2 1 2 1 2 1 1 1 2 2 2 0 0
 

Sample Output
 
   
possible not possible
题意:
两条路上火车进站,问进站的顺序是否满足已给序列。
思路:
之前觉得很简单,果断深搜,也果断TLE。后来看了别人代码。结果自己状态找的不优。
记忆化搜索代码:
#include 
#include
#include
using namespace std;
const int N=1010;
bool ans[N][N];
int a[N],b[N],c[N*2];
int n,m,t;
void dfs(int n1,int n2)
{
    if(ans[n1][n2]) return ;
    if(n1>n||n2>m) return ;
    if(n1==n&&n2==m)
    {
        ans[n1][n2]=1;
        return;
    }
    ans[n1][n2]=1;
    if(c[n1+n2+1]==a[n1+1])
        dfs(n1+1,n2);
    if(c[n1+n2+1]==b[n2+1])
        dfs(n1,n2+1);
}
int main()
{
    int i,j,k;
    while(scanf("%d%d",&n,&m),n&&m)
    {
        memset(ans,0,sizeof(ans));
        for(i=1; i<=n; i++) scanf("%d",&a[i]);
        for(i=1; i<=m; i++) scanf("%d",&b[i]);
        for(i=1; i<=n+m; i++) scanf("%d",&c[i]);
        dfs(0,0);
        if(ans[n][m]) puts("possible");
        else puts("not possible");
    }
    return 0;
}


dp代码:
#include
#include
#include
#include
using namespace std;
const int maxn=2005;
int a[maxn],b[maxn],c[maxn];
int dp[maxn][maxn];

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)
            break;
        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]);

            memset(dp,0,sizeof dp);
            dp[0][0]=1;
            for(int i=0;i<=n;i++)
                for(int j=0;j<=m;j++)
            {
                if(i>0&&c[i+j]==a[i]&&dp[i-1][j])
                    dp[i][j]=1;
                if(j>0&&c[i+j]==b[j]&&dp[i][j-1])
                   dp[i][j]=1;
            }
            if(dp[n][m])
                puts("possible");
            else puts("not possible");
    }
    return 0;
}


 
  
 
  

你可能感兴趣的:(hdu3779Railroad(记忆化搜索|dp))