HDU3779 Railroad JAVA版题解+深搜
来源:互联网 发布:淘宝金币抵钱卖家亏吗 编辑:程序博客网 时间:2024/06/04 18:46
Railroad
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 70 Accepted Submission(s): 24
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 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 N2 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
解析:
这道题可以用动态规划或者直接回溯深搜,要注意边界条件:当两个列车车厢全部都搜索完毕后,搜索结束,输出结果,不多说,上代码
package ACM;import java.util.Scanner;public class Qre15_07_17_B {static int m,n;static int[] a=new int[1010],b=new int[1010],c=new int[2020];public static void main(String[] args){Scanner in = new Scanner(System.in);m=in.nextInt();n=in.nextInt();while(m+n!=0){a=new int[1010];b=new int[1010];c=new int[2020];for(int i=1;i<=m;i++)a[i]=in.nextInt();for(int j=1;j<=n;j++)b[j]=in.nextInt();for(int k=1;k<=m+n;k++)c[k]=in.nextInt();if(dfs(m,n))System.out.println("possible");else System.out.println("not possible");m=in.nextInt();n=in.nextInt();}}static boolean dfs(int i,int j){if(i==0&&j==0)return true;if(i>0&&a[i]==c[i+j]&&dfs(i-1,j))return true;if(j>0&&b[j]==c[i+j]&&dfs(i,j-1))return true;return false;}}
- HDU3779 Railroad JAVA版题解+深搜
- HDU3779(Railroad)
- hdu3779 Railroad 记忆化搜索
- hdu3779
- "railroad diagram"
- hdu3779---记忆化搜索
- [DFS/动态规划DP] HDU/HOJ 3779 Railroad 暴力解 Zipper加强版
- poj 1394 Railroad
- poj 1394 Railroad
- HDU:3779 Railroad
- Railroad - HDU 3779 dp
- HDU 3779 Railroad
- LA 4888 Railroad
- HDU 3779 Railroad
- code hunt 题解一 (java 版)
- code hunt 题解二(java版)
- code hunt 题解三 (java版)
- code hunt 题解四(java 版)
- CSS盒子模型
- Codeforces Round #309 (Div. 1) C. Love Triangles 二分图
- 儿子和女儿——解释器和编译器的区别与联系
- Android 4.0以后不能在主线程能进行网络访问
- TCP协议中的三次握手和四次挥手(图解)
- HDU3779 Railroad JAVA版题解+深搜
- 最早延迟时间优先运行的实现
- 51单片机学习笔记【二】——按键实验
- Linux磁盘分区-格式化-加载-卸载
- Fans-rt 任务局部变量特性设计方案(需求分解)
- HDOJ题目3729 I'm Telling the Truth(二分图)
- c语言结构体赋值,并输出各种类型变量的值
- 【基础知识】【中缀转逆波兰(后缀)表达式】
- Basic Internetworking