Railroad

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 10   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

  1. 3 3
  2. 1 2 1
  3. 2 1 1
  4. 1 2 1 1 2 1
  5. 3 3
  6. 1 2 1
  7. 2 1 2
  8. 1 1 1 2 2 2
  9. 0 0

Sample Output

  1. possible
  2. not possible

Source

HDOJ开放式进阶训练(11)——全程测试
 
题解:记忆化搜索,记录状态
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. int n,m;
  8. int a[],b[],c[];
  9. bool vis[][];
  10. bool dfs(int x,int y,int k)
  11. {
  12. if (k==n+m+) return ;
  13. if(vis[x][y]) return ;
  14. vis[x][y]=;
  15. if (x<=n && a[x]==c[k] && dfs(x+,y,k+))
  16. return ;
  17. if (y<=m && b[y]==c[k] && dfs(x,y+,k+))
  18. return ;
  19.  
  20. return ;
  21. }
  22. int main()
  23. {
  24. while(~scanf("%d%d",&n,&m))
  25. {
  26. if (n== && m==) break;
  27. for(int i=;i<=n;i++) scanf("%d",&a[i]);
  28. for(int i=;i<=m;i++) scanf("%d",&b[i]);
  29. for(int i=;i<=n+m;i++)scanf("%d",&c[i]);
  30. memset(vis,,sizeof(vis));
  31. if (dfs(,,)) printf("possible\n");
  32. else printf("not possible\n");
  33. }
  34. return ;
  35. }

HDU 3779 Railroad(记忆化搜索)的更多相关文章

  1. 不要62 hdu 2089 dfs记忆化搜索

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中 ...

  2. hdu 1078(dfs记忆化搜索)

    题意:容易理解... 思路:我开始是用dfs剪枝做的,968ms险过的,后来在网上学习了记忆化搜索=深搜形式+dp思想,时间复杂度大大降低,我个人理解,就是从某一个点出发,前面的点是由后面的点求出的, ...

  3. hdu 4826(dp + 记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4826 思路:dp[x][y][d]表示从方向到达点(x,y)所能得到的最大值,然后就是记忆化了. #i ...

  4. hdu---(3779)Railroad(记忆化搜索/dfs)

    Railroad Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. POJ 1198 / HDU 1401 Solitaire (记忆化搜索+meet in middle)

    题目大意:给你一个8*8的棋盘,上面有四个棋子,给你一个初始排布,一个目标排布,每次移动,可以把一个棋子移动到一个相邻的空位,或者跨过1个相邻的棋子,在保证棋子移动不超过8次的情况下,问能否把棋盘上的 ...

  6. hdu 4345 Permutation 记忆化搜索

    思路:实际上求的是和小于等于n的质数的种类数!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorit ...

  7. POJ1088 滑雪题解+HDU 1078(记忆化搜索DP)

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  8. hdu1078 记忆化搜索

    /* hdu 1078 QAQ记忆化搜索 其实还是搜索..因为里面开了一个数组这样可以省时间 (dp[x][y]大于0就不用算了直接返回值) */ #include<stdio.h> #i ...

  9. HDU 1429 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...

随机推荐

  1. [转]linux内核分析笔记----内存管理

    转自:http://blog.csdn.net/Baiduluckyboy/article/details/9667933 内存管理,不用多说,言简意赅.在内核里分配内存还真不是件容易的事情,根本上是 ...

  2. ThreadLocal的设计理念与作用

    转自:http://www.iteye.com/topic/103804 首先,ThreadLocal 不是用来解决共享对象的多线程访问问题的,一般情况下,通过ThreadLocal.set() 到线 ...

  3. python---自动群发邮件

    生活中我们经常发送邮件,那么我们能不能用Python写一个自动发送邮件的功能呢?答案是肯定的!!! 开始实现功能之前我们需要开启我们邮箱的 IMAP/SMTP功能,我们先了解一下什么是IMAP/SMT ...

  4. 20145313张雪纯 《Java程序设计》第3周学习总结

    20145313张雪纯 <Java程序设计>第3周学习总结 教材学习内容总结 4.1.1 定义类 package four; class Clothes{ String color; ch ...

  5. ARTS Week 001

    Algorithm Leetcode 1. Two Sum Given an array of integers, return indices of the two numbers such tha ...

  6. 【转载】解决window.showModalDialog 模态窗口中location 打开新窗口问题

    来源: <http://bibipear.blog.sohu.com/143449988.html> 在我们的项目中,通常会用到showModalDialog 打开一个模态的子窗口,但是在 ...

  7. LeetCode——Unique Paths

    Question A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram be ...

  8. Graph_Master(连通分量_E_Hungry+Tarjan)

    hdu_4685 终于来写了这题的解题报告,没有在昨天A出来有点遗憾,不得不说数组开大开小真的是阻碍人类进步的一大天坑. 题目大意:给出n个王子,m个公主,只要王子喜欢,公主就得嫁(这个王子当得好霸道 ...

  9. ng2 quickstart

    1.下载 git clone https://github.com/angular/quickstart.git quickstart-angular 2.安装模块 npm install 3.启动 ...

  10. 在.Net中进行SQL Server数据库备份与还原操作实用类

    #region 类说明 //----------------------------------------------------------------------------- // // 项目 ...