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的提交方式不同??? 数和运算符各开一个栈. 表达式从左到右扫一遍,将数存成大数,遇到数压在 数的栈,运算符压在 运算符的栈,每当遇到右括 ...
随机推荐
- .net 内存分配及垃圾回收总结
生存期垃圾回收器 目前有很多种类型的垃圾回收器.微软实现了一种生存期垃圾回收器(Generation Garbage Collector). 生存期垃圾回收器将内存分为很多托管堆,每一个托管堆 ...
- XSS的原理分析与解剖[转http://www.freebuf.com/articles/web/40520.html]
0×01 前言: <xss攻击手法>一开始在互联网上资料并不多(都是现成的代码,没有从基础的开始),直到刺的<白帽子讲WEB安全>和cn4rry的<XSS跨站脚本攻击剖析 ...
- 【热门收藏】iOS开发人员必看的精品资料(100个)——下载目录
iPhone.iPad产品风靡全球,巨大的用户群刺激着iOS软件开发需求,然而国内人才缺口很大,正处于供不应求的状态,ios开发前景大好.我们整理了51CTO下载中心100份热门的ios开发资料,做了 ...
- 014--VS2013 C++ c++定时动画
资源图片 //全局变量HBITMAP girl[7];HDC mdc, hdc;int num; //--------------------------------------------InitI ...
- 28335timer
/*****************************************************************************Copyright: 2014,TkaiFi ...
- 事后分析报告(M1阶段)
我们的项目是自选项目,一款名为备忘录锁屏MemoryDebris的软件. 因为我们组成员在此之前都没有接触过安卓开发,于是在第一阶段花了很大的时间和精力学习安卓.又花费了较长一段时间设计软件与研究安卓 ...
- 搭建SpringMVC+MyBatis开发框架五
建立web结构 1.在webapp目录下新建css.img和js文件夹,删除默认的index.jsp文件:  2.在WEB-INF文件夹下建立一个page文件夹,然后在page下新建一个index. ...
- mysql 存储过程 -- 游标的使用(备忘)
BEGIN ; DECLARE f_ratio FLOAT DEFAULT 0.8; ); ); DECLARE i_statDate DATE; DECLARE i_accumulateCount ...
- vi中正则表达式的使用
在当前行中删除从aa到zz的所有字符 :s/aa.*zz//在整个文件用and代替所有的&字符:1,$s/&/and/g在每一行的首行插入字符串new:1,$s/^/new/g在第二行 ...
- 【桌面程序搞界面再也不怕了】:迅雷BOLT入门(一)开篇 附程序和源码
本来想多蛤一下前因后果,突然意兴阑珊不想多说啦,直接帖效果吧. 这个是用迅雷BOLT把原来写的一个IE拦截器的界面重写了一下.界面效果是直接从单位的大屏系统改过来的,其中文本框部分,还请设计大屏的小姑 ...