hdu---(3779)Railroad(记忆化搜索/dfs)
Railroad
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 572 Accepted Submission(s): 228
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.
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.
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
//#define LOCAL
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=;
int aa[maxn],bb[maxn],cc[maxn<<];
int mat[maxn][maxn];
int n1,n2;
bool flag;
void dfs(int lena,int lenb,int lenc)
{
if(lena==n1+&&lenb==n2+){
flag=;
return ;
}
if(mat[lena][lenb]) return ;
if(!flag&&aa[lena]==cc[lenc]){
mat[lena][lenb]=;
dfs(lena+,lenb,lenc+);
}
if(!flag&&bb[lenb]==cc[lenc]){
mat[lena][lenb]=;
dfs(lena,lenb+,lenc+);
}
}
void input(int n,int a[])
{
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
}
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
while(scanf("%d%d",&n1,&n2)&&n1+n2!=)
{
input(n1,aa);
aa[n1+]=-;
input(n2,bb);
bb[n2+]=-;
input(n1+n2,cc);
flag=;
memset(mat,,sizeof(mat));
dfs(,,);
if(flag)printf("possible\n");
else printf("not possible\n");
}
return ;
}
hdu---(3779)Railroad(记忆化搜索/dfs)的更多相关文章
- 记忆化搜索(DFS+DP) URAL 1223 Chernobyl’ Eagle on a Roof
题目传送门 /* 记忆化搜索(DFS+DP):dp[x][y] 表示x个蛋,在y楼扔后所需要的实验次数 ans = min (ans, max (dp[x][y-i], dp[x-1][i-1]) + ...
- 记忆化搜索(DFS+DP) URAL 1501 Sense of Beauty
题目传送门 /* 题意:给了两堆牌,每次从首部取出一张牌,按颜色分配到两个新堆,分配过程两新堆的总数差不大于1 记忆化搜索(DFS+DP):我们思考如果我们将连续的两个操作看成一个集体操作,那么这个操 ...
- HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)
题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...
- hdu1978How many ways (记忆化搜索+DFS)
Problem Description 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下: 1.机器人一开始在棋盘的起始点并有起始点所标 ...
- P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 记忆化搜索dfs
题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定奶牛必须遵 ...
- hdu 4826(dp + 记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4826 思路:dp[x][y][d]表示从方向到达点(x,y)所能得到的最大值,然后就是记忆化了. #i ...
- POJ1088滑雪(记忆化搜索+DFS||经典的动态规划)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 84297 Accepted: 31558 Description M ...
- hdu 4345 Permutation 记忆化搜索
思路:实际上求的是和小于等于n的质数的种类数!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorit ...
- POJ 1198 / HDU 1401 Solitaire (记忆化搜索+meet in middle)
题目大意:给你一个8*8的棋盘,上面有四个棋子,给你一个初始排布,一个目标排布,每次移动,可以把一个棋子移动到一个相邻的空位,或者跨过1个相邻的棋子,在保证棋子移动不超过8次的情况下,问能否把棋盘上的 ...
随机推荐
- 关于header('location:url')的一些说明,php缓冲区
网上搜索header('location:url')的用法,得到如下三个结论: 1. location和“:”号间不能有空格,否则会出错. 2. 在用header前不能有任何的输出. 3. heade ...
- TeeChart曲线平滑 Line.Smoothed
需要注意的是,在加载点之前,需要设置Smoothed属性为false 等点加载完成之后,再设置Smoothed属性为true, //如果直接设置Smoothed为true再去加载点的话,曲线就完全不显 ...
- Run_Product Example Form - Oracle Forms 6i
I have already posted in my previous post Running Reports Using Run_Product to run reports in Oracle ...
- Virtualenv: 一个Python环境管理工具(windown版本)
1.安装virtualenv 在安装virtualenv之前,我们需要安装至少有一个版本的python:因为virtualenv是python的一个第三方模块,必须基于python环境才能安装: 如果 ...
- JSON的操作
今天遇到了一个要解析JSON的需求.在http://stackoverflow.com/questions/1826727/how-do-i-parse-json-with-ruby-on-rails ...
- 新知识Tom大叔
http://www.cnblogs.com/TomXu/archive/2011/12/15/2284752.html http://www.cnblogs.com/TomXu/archive/20 ...
- 强制性签出被人没有签入的文件(在.net开发vs中)
灵感,是天才的女神.她并不步履蹒跚地走过,而是在空中像乌鸦那么警觉地飞过的,她没有什么剽带给诗人抓握,她的头是一团烈火,她溜得快,像那些白里带红的鹤,教猎人见了无可奈何.——巴尔扎克(上海网站建设) ...
- iOS - OC NSValue 值
前言 @interface NSValue : NSObject <NSCopying, NSSecureCoding> 将任意数据类型包装成 OC 对象 1.比较两个 NSValue 类 ...
- Linux_常用命令_03_磁盘/挂载_信息查看
1. 1.1. mount 不带参数的话,显示的是 当前已经挂载的情况 1.2. df 不带参数的话,硬盘分区状况查询 2. 2.1. cat /proc/partitions 2.2. fdisk ...
- Python学习(14)模块一
目录 Python 模块 import语句 from ... import 语句 from ... import * 语句 定位模块 PYTHONPATH变量 命名空间和变量 dir()函数. glo ...