zoukankan      html  css  js  c++  java
  • HDU Railroad (记忆化)

    Railroad

    Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 16   Accepted Submission(s) : 13
    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.
    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 N1 N2 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 N2 positive integers identifying the products on the second train (same format as above). Finally, the fourth line contains N1+N2 positive integers giving the desired order for the departing train (same format as above).
    The end of input is indicated by N1 = N2 = 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
     
    Source
    HDOJ开放式进阶训练(11)——全程测试
     
     
     
     
    #include<stdio.h>
    #include<string.h>
    
    const int maxn=1010;
    
    int n,m,flag;
    int a[maxn],b[maxn],c[2*maxn];
    int res[maxn][maxn];
    
    void DFS(int x,int y,int z){
        if(flag)
            return ;
        if(res[x][y]!=0)
            return ;
        if(z==n+m&& z!=0){
            flag=1;
            return ;
        }
        if(x<n && a[x]==c[z])
            DFS(x+1,y,z+1);
        if(y<m && b[y]==c[z])
            DFS(x,y+1,z+1);
        res[x][y]=1;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        while(scanf("%d%d",&n,&m)){
            if(n==0 && m==0)
                break;
            int i;
            for(i=0;i<n;i++)
                scanf("%d",&a[i]);
            for(i=0;i<m;i++)
                scanf("%d",&b[i]);
            for(i=0;i<n+m;i++)
                scanf("%d",&c[i]);
            memset(res,0,sizeof(res));
            flag=0;
            DFS(0,0,0);
            if(flag)
                printf("possible\n");
            else
                printf("not possible\n");
        }
        return 0;
    }
  • 相关阅读:
    Python正课48 —— 匿名函数及其应用
    Python正课47 —— 面向过程编程思想
    Python正课46 —— 二分法
    Python正课45 —— 函数的递归调用
    Python正课44 —— 生成式
    Python正课43 —— 三元表达式
    SQL数据库操作命令大全
    css 高度写死 下滚动时ios 滚动不协调处理
    记录:js删除数组中某一项或几项的几种方法
    本次存储存数组对象
  • 原文地址:https://www.cnblogs.com/jackge/p/2988687.html
Copyright © 2011-2022 走看看