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 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...
随机推荐
- Ubuntu16.04安装印象笔记
Nixnote 是一个 Evernote 开源客户端,原名 Nevernote.Evernote 是一个著名的笔记等个人资料整理和同步软件, 因为 Evernote 没有 Linux 下的官方版本,因 ...
- .NET Core + EF 报nuget包不兼容
错误信息如下: 严重性 代码 说明 项目 文件 行 禁止显示状态错误 NU1107 Microsoft.EntityFrameworkCore 中检测到版本冲突.直接安装/引用 Microsoft.E ...
- Autowire
Field userService in com.demo.web.Controller.HomeController required a single bean, but 2 were found ...
- 20145335郝昊《java程序设计》第2次实验报告
20145335郝昊<java程序设计>第2次实验报告 实验名称 Java面向程序设计,采用TDD的方式设计有关实现复数类Complex. 理解并掌握面向对象三要素:封装.继承.多态. 运 ...
- LeetCode (236):Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [翻译]解读CSS中的长度单位
测量,在WEB设计上是非常重要的.在CSS中有至少10种不同的测量单位.每种单位都有其独特的作用,使用它们,可以使页面,在各种设备上,很好的工作.一旦你熟悉了所有这些单位,你可以更准确地设定元素的大小 ...
- NOIP树上问题总结
这几年考了好几次树上问题: NOIP2012 疫情控制(二分答案+倍增+贪心) NOIP2013 货车运输(最大生成树+倍增) NOIP2014 联合权值(勉强算作树形dp的傻逼题) NOIP2015 ...
- Mac Homebrew安装php56 到phpstorm过程问题汇总
Mac自带版本是php5.5,本来是用homebrew安装xdebug 命令:brew install php55-xdebug 但是安装之后使用phpstorm还是有问题.php -v 并没有显示有 ...
- Web前端可以转行做游戏吗?
作者:ManfredHu 链接:http://www.manfredhu.com/2018/03/15/31-laya-game-tips/index.html 声明:版权所有,转载请保留本段信息,谢 ...
- 2018 ICPC北京 H ac自动机
n=40的01串,求有多少m=40的01串中包含它,包含的定义是存在子串有至多一个字符不相同 600组n=15的数据 15组n=40的数据,所以我们只能支持n^5的算法. 陷入两个比较有意思的坑: 1 ...