POJ Railroad ——记忆化搜索
来源:互联网 发布:淘宝企业店铺认证流程 编辑:程序博客网 时间:2024/06/01 09:18
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 31 2 12 1 11 2 1 1 2 13 31 2 12 1 21 1 1 2 2 20 0
Sample Output
possiblenot possible此题还算不错的题目,可以当记忆化搜索的入门题目,不是经常做搜索题目。导致对以前的记忆化搜索生疏起来,根本思想就是观察每种情况的自清况可不可能重复,如果会的化,那么可以通过记忆化搜索来剪枝。/* * File: main.cpp * Author: hit-acm * * Created on 2012年7月23日, 下午1:10 */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int m, n;const int MAX = 1001;int a[MAX];int b[MAX];int c[2 * MAX];bool color[MAX][MAX];/* * */bool flag;void DFS(int x, int y, int i) { if (color[x][y]) { return; } if (flag == true) { return; } if (i >= m + n) { flag = true; return; } if (x < n && a[x] == c[i]) { DFS(x + 1, y, i + 1); } if (y < m && b[y] == c[i]) { DFS(x, y + 1, i + 1); } color[x][y] = true;}int main() { while (scanf("%d%d", &n, &m) && (m || n)) { for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int j = 0; j < m; j++) { scanf("%d", &b[j]); } for (int i = 0; i < m + n; i++) { scanf("%d", &c[i]); } flag = false; memset(color, 0, sizeof (color)); DFS(0, 0, 0); if (flag) { printf("possible\n"); } else { printf("not possible\n"); } } return 0;}
- POJ Railroad ——记忆化搜索
- hdu3779 Railroad 记忆化搜索
- 记忆化搜索——POJ 1351
- 记忆化搜索——POJ 1088
- 【记忆化搜索】POJ
- dp——记忆化搜索——poj 1088
- POJ 1579 Function Run Fun——记忆化搜索
- POJ-1088 记忆化搜索
- poj-3280-记忆化搜索
- poj 1143 记忆化搜索
- poj 1179 记忆化搜索
- poj 1664 记忆化搜索
- poj 1170 记忆化搜索
- poj 3249 记忆化搜索
- POJ 1661 记忆化搜索
- poj 1088(记忆化搜索)
- poj 3373(记忆化搜索)
- poj 1088 记忆化搜索
- org.eclipse.core.runtime.PlatformObject所在的包
- micro usb mini usb
- 怎么用短链接做压力测试
- l消除og4j警告
- hibernate 存储过程事务
- POJ Railroad ——记忆化搜索
- hdu 2544 最短路
- 【IPC通信】Posix消息队列的属性设置
- Linux ./configure --prefix命令
- Collections.sort 重写compare()方法
- 分享一个php ajax实现文章顶/踩 实例
- 一些常用的webservice
- 素数筛选
- 套接字中一些最好记住的代码模块