Description
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 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
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 此题还算不错的题目,可以当记忆化搜索的入门题目,不是经常做搜索题目。导致对以前的记忆化搜索生疏起来,根本思想就是观察每种情况的自清况可不可能重复,如果会的化,那么 可以通过记忆化搜索来剪枝。
/* * 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; }