LA 2031
Mr. White, a fat man, now is crazy about a game named ``Dance, Dance, Revolution". But his dance skill is so poor that he could not dance a dance, even if he dances arduously every time. Does ``DDR" just mean him a perfect method to squander his pounds? No way. He still expects that he will be regarded as ``Terpsichorean White" one day. So he is considering writing a program to plan the movement sequence of his feet, so that he may save his strength on dancing. Now he looks forward to dancing easily instead of sweatily.
``DDR" is a dancing game that requires the dancer to use his feet to tread on the points according to the direction sequence in the game. There are one central point and four side points in the game. Those side points are classified as top, left, bottom and right. For the sake of explanation, we mark them integers. That is, the central point is 0, the top is 1, the left is 2, the bottom is 3, and the right is 4, as the figure below shows:

At the beginning the dancer's two feet stay on the central point. According to the direction sequence, the dancer has to move one of his feet to the special points. For example, if the sequence requires him to move to the top point at first, he may move either of his feet from point 0 to point 1 (Note: Not both of his feet). Also, if the sequence then requires him to move to the bottom point, he may move either of his feet to point 3, regardless whether to use the foot that stays on point 0 or the one that stays on point 1.
There is a strange rule in the game: moving both of his feet to the same point is not allowed. For instance, if the sequence requires the dancer to the bottom point and one of his feet already sta ys on point 3, he should stay the very foot on the same point and tread again, instead of moving the other one to point 3.
After dancing for a long time, Mr. White can calculate how much strength will be consumed when he moves from one point to another. Moving one of his feet from the central point to any side points will consume 2 units of his strength. Moving from one side point to another adjacent side point will consume 3 units, such as from the top point to the left point. Moving from one side point to the opposite side point will consume 4 units, such as from the top point to the bottom point. Yet, if he stays on the same point and tread again, he will use 1 unit.
Assume that the sequence requires Mr. White to move to point 1
2
2
4. His feet may stays on (point 0, point 0)
(0, 1)
(2, 1)
(2, 1)
(2, 4). In this couple of integers, the former number represents the point of his left foot, and the latter represents the point of his right foot. In this way, he has to consume 8 units of his strength. If he tries another pas, he will have to consume much more strength. The 8 units of strength is the least cost.
Input
The input file will consist of a series of direction sequences. Each direction sequence contains a sequence of numbers. Ea ch number should either be 1, 2, 3, or 4, and each represents one of the four directions. A value of 0 in the direction sequence indicates the end of direction sequence. And this value should be excluded from the direction sequence. The input file ends if the sequence contains a single 0.
Output
For each direction sequence, print the least units of strength will be consumed. The result should be a single integer on a line by itself. Any more white spaces or blank lines are not allowable.
Sample Input
1 2 2 4 0
1 2 3 4 1 2 3 3 4 2 0
0
Sample Output
8
22 dp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> using namespace std; vector<int> g;
int cost[][];
int dp[][][]; void init() {
for(int i = ; i <= ; ++i) cost[][i] = ;
cost[][] = cost[][] = cost[][] = cost[][] = ;
cost[][] = cost[][] = cost[][] = cost[][] = ; cost[][] = cost[][] = ;
cost[][] = cost[][] = ;
} void solve() {
memset(dp[], -, sizeof(dp[]));
memset(dp[], -, sizeof(dp[]));
dp[][][] = ; int now = ,last = ;
for(int i = ; i < g.size(); ++i) {
for(int j = ; j <= ; ++j) {
for(int k = ; k <= ; ++k) {
if(dp[last][j][k] != -) {
if(j == g[i] || k == g[i]) {
dp[now][j][k] = dp[now][j][k] == - ?
dp[last][j][k] + :
min(dp[now][j][k], dp[last][j][k] + );
} else {
dp[now][g[i]][k] = dp[now][g[i]][k] == - ?
dp[last][j][k] + cost[j][g[i]] :
min(dp[now][g[i]][k], dp[last][j][k] + cost[j][g[i]]);
dp[now][j][g[i]] = dp[now][j][g[i]] == - ?
dp[last][j][k] + cost[k][g[i]] :
min(dp[now][j][g[i]], dp[last][j][k] + cost[k][g[i]]);
} }
}
}
swap(now,last);
memset(dp[now], -, sizeof(dp[now]));
} int ans = -;
for(int i = ; i <= ; ++i) {
for(int j = ; j <= ; ++j) {
if(dp[last][i][j] == -) continue;
ans = ans == - ? dp[last][i][j] : min(ans, dp[last][i][j]);
}
}
printf("%d\n",ans);
} int main()
{
//freopen("sw.in", "r", stdin); int ch;
init();
while(~scanf("%d", &ch) && ch) {
g.clear();
g.push_back(ch);
while(scanf("%d",&ch) && ch) {
g.push_back(ch);
} solve();
}
return ;
}
LA 2031的更多相关文章
- [LA] 2031 Dance Dance Revolution
Dance Dance Revolution Time limit: 3.000 seconds Mr. White, a fat man, now is crazy about a game nam ...
- leggere la nostra recensione del primo e del secondo
La terra di mezzo in trail running sembra essere distorto leggermente massima di recente, e gli aggi ...
- Le lié à la légèreté semblait être et donc plus simple
Il est toutefois vraiment à partir www.runmasterfr.com/free-40-flyknit-2015-hommes-c-1_58_59.html de ...
- Mac Pro 使用 ll、la、l等ls的别名命令
在 Linux 下习惯使用 ll.la.l 等ls别名的童鞋到 mac os 可就郁闷了~~ 其实只要在用户目录下建立一个脚本“.bash_profile”, vim .bash_profile 并输 ...
- Linux中的动态库和静态库(.a/.la/.so/.o)
Linux中的动态库和静态库(.a/.la/.so/.o) Linux中的动态库和静态库(.a/.la/.so/.o) C/C++程序编译的过程 .o文件(目标文件) 创建atoi.o 使用atoi. ...
- Mac OS使用ll、la、l等ls的别名命令
在linux下习惯使用ll.la.l等ls别名的童鞋到mac os可就郁闷了-- 其实只要在用户目录下建立一个脚本“.bash_profile”,并输入以下内容即可: alias ll='ls -al ...
- .Uva&LA部分题目代码
1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...
- 获取在线人数 CNZZ 和 51.la
string Cookies = string.Empty; /// <summary> /// 获取在线人数 (51.la统计器) /// </summary> /// &l ...
- BNU OJ 33691 / LA 4817 Calculator JAVA大数
留着当个模板用,在BNU上AC,在LA上RE……可能是java的提交方式不同??? 数和运算符各开一个栈. 表达式从左到右扫一遍,将数存成大数,遇到数压在 数的栈,运算符压在 运算符的栈,每当遇到右括 ...
随机推荐
- java 复习
整型: byte 1 short 2 int 4 long 80b1001 1_233_32 1341414141414Ljava 没有无符号类型浮点型:float 4 double 812.2f 无 ...
- RAP开发入门-搭建RAP开发环境(一)
ps:补充 RAP (Remote Application Platform) 官网地址eclipse.org/rap 1.下载IDE http://www.eclipse.org/downloads ...
- <转载>编程珠玑-位排序(bitsort)
转载:http://www.cnblogs.com/shuaiwhu/archive/2011/05/29/2065039.html 维护版权 在<编程珠玑>一书上,有一题是将一堆不 ...
- Karaf 基于 osgi
Karaf是Apache旗下的一个开源项目.Karaf同时也是一个基于OSGi的运行环境,Karaf提供了一个轻量级的OSGi容器,可以用于部署各种组件,应用程序.Karaf提供了很多特性用于帮助开发 ...
- iOS-添加测试设备Identifier
第一步:确认你的设备已经连接 第二步:点击xcode上"Windows"标签,选择"Devices" 第三步:在弹出的左框选择你要添加的设备.在右边框里可以找到 ...
- HTML/CSS的学习过程一览
HTML/CSS的学习过程一览 说明 调试工具使用的是Google Chrome浏览器,其余浏览器出现的问题,这锅我不背[傲娇脸 可以使用浏览器查看源代码 网页列表 HTML_CSS_1 HTML基本 ...
- 初探Xamarin
Xamarin是一个基于mono的商业项目,收费,而且贼贵.官网地址是:http://xamarin.com/ 就我个人理解,收费的Xamarin提供一个for visual studio 2010/ ...
- 华为p7怎么打开usb调试模式
在应用程序列表中选择[设置]进入系统设置菜单,点击[关于手机]  2.在"版本号"上面连续点击七次:  3.现在返回"设置"界面,发现多了一个"开 ...
- CSS中盒子模型和position(一)
今天遇到几个css中的重要的知识点,记得这些都是以前看过的:margin.padding.border和position.可是用起来还是有很多的问题,以前自己看过去总是懒得记录,等到用起来了都不知道自 ...
- solr简易安装配置
之前弄了段时间的lucene,昨天下午开始学solr,准备用到项目中,在网上找了一些教程,有的不是讲得太复杂,就是讲得不在点上,花了不少冤枉时间.有的一上来就花过半的篇幅大讲特讲“3H”,(what, ...