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

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 end of input is indicated by N1 = N2 = 0.
Output
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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int n,m;
int a[],b[],c[];
bool vis[][];
bool dfs(int x,int y,int k)
{
if (k==n+m+) return ;
if(vis[x][y]) return ;
vis[x][y]=;
if (x<=n && a[x]==c[k] && dfs(x+,y,k+))
return ;
if (y<=m && b[y]==c[k] && dfs(x,y+,k+))
return ; return ;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if (n== && m==) break;
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=m;i++) scanf("%d",&b[i]);
for(int i=;i<=n+m;i++)scanf("%d",&c[i]);
memset(vis,,sizeof(vis));
if (dfs(,,)) printf("possible\n");
else printf("not possible\n");
}
return ;
}
HDU 3779 Railroad(记忆化搜索)的更多相关文章
- 不要62 hdu 2089 dfs记忆化搜索
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中 ...
- hdu 1078(dfs记忆化搜索)
题意:容易理解... 思路:我开始是用dfs剪枝做的,968ms险过的,后来在网上学习了记忆化搜索=深搜形式+dp思想,时间复杂度大大降低,我个人理解,就是从某一个点出发,前面的点是由后面的点求出的, ...
- hdu 4826(dp + 记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4826 思路:dp[x][y][d]表示从方向到达点(x,y)所能得到的最大值,然后就是记忆化了. #i ...
- hdu---(3779)Railroad(记忆化搜索/dfs)
Railroad Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- POJ 1198 / HDU 1401 Solitaire (记忆化搜索+meet in middle)
题目大意:给你一个8*8的棋盘,上面有四个棋子,给你一个初始排布,一个目标排布,每次移动,可以把一个棋子移动到一个相邻的空位,或者跨过1个相邻的棋子,在保证棋子移动不超过8次的情况下,问能否把棋盘上的 ...
- hdu 4345 Permutation 记忆化搜索
思路:实际上求的是和小于等于n的质数的种类数!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorit ...
- POJ1088 滑雪题解+HDU 1078(记忆化搜索DP)
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- hdu1078 记忆化搜索
/* hdu 1078 QAQ记忆化搜索 其实还是搜索..因为里面开了一个数组这样可以省时间 (dp[x][y]大于0就不用算了直接返回值) */ #include<stdio.h> #i ...
- HDU 1429 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...
随机推荐
- Docker+.Net Core 的那些事儿-1.准备工作
1.下载centos 地址:https://www.centos.org/download/ 我使用的是DVD ISO,这么做的目的是为了在之后的docker填坑的路上,方便使用centos中Fire ...
- spray-json
spray-json是一个轻量级的,简介的和高效的使用Scala实现的json 它拥有以下特征: 一个简单不可变的模型的json语言元素 一个高效的json解析器 可选择既紧凑又漂亮的json到str ...
- 独立开发一个App是一种怎样的体验?
(本文原文是我在知乎上写的一篇回答:独立开发一个 App 是一种怎样的体验?) UWP业余开发者表示,非常愿意做一股清流.在喧闹无比的iOS和Android平台,为了给应用造势,太多人费尽心思,勾心斗 ...
- QML Image Element
QML Image Element The Image element displays an image in a declarative user interface More... Image元 ...
- Visual C++的DLL
动态链接库 (DLL) 是作为共享函数库的可执行文件. 动态链接提供了一种方法,使进程可以调用不属于其可执行代码的函数. 函数的可执行代码位于一个 DLL 中,该 DLL 包含一个或多个已被编译.链接 ...
- [转]tomcat之一:指定tomcat运行时JDK版本
今天在做项目的时候,主管让我在本机上启动多个tomcat且指定不同的jdk环境.因为在企业的项目中个,对于同一个服务器中有多个jdk和tomcat,所以就需要手动指定不同的jdk. 在网上找了很多资料 ...
- NOIP2019前的训练记录
\(April\):肛多项式,学\(FWT\)一个小时无果后背了六个公式,看来证明又得咕很久了
- 根据iframe获取window
今天使用layui弹出窗口,需要将函数写在弹出的窗口,但是按钮事件是在父层窗口绑定的,这样就要在父层窗口调用子层窗口的函数. 子层函数与父层函数 function topup() { console. ...
- 优雅的处理你的Java异常
本文介绍 本文仅按照业务系统开发角度描述异常的一些处理看法.不涉及java的异常基础知识,可以自行查阅 <Java核心技术 卷I> 和 <java编程思想> 可以得到更多的基础 ...
- Junit中的setup和teardown方法
setup需要@before注解,实现测试前的初始化工作 teardown需要@after注解,测试完成后垃圾回收等后续工作