首页 > 代码库 > HDU 3779 Railroad(记忆化搜索)
HDU 3779 Railroad(记忆化搜索)
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.
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.
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<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,m; int a[1005],b[1005],c[2010]; bool vis[1005][1005]; bool dfs(int x,int y,int k) { if (k==n+m+1) return 1; if(vis[x][y]) return 0; vis[x][y]=1; if (x<=n && a[x]==c[k] && dfs(x+1,y,k+1)) return 1; if (y<=m && b[y]==c[k] && dfs(x,y+1,k+1)) return 1; return 0; } int main() { while(~scanf("%d%d",&n,&m)) { 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(vis,0,sizeof(vis)); if (dfs(1,1,1)) printf("possible\n"); else printf("not possible\n"); } return 0; }
HDU 3779 Railroad(记忆化搜索)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。