uva----(10794) A Different Task
A Different Task |
---|
Here we have made your task little bit difficult by making the problem more flexible. Here the disks can be in any peg initially.
If more than one disk is in a certain peg, then they will be in a valid arrangement (larger disk will not be on smaller ones). We will give you two such arrangements of disks. You will have to find out the minimum number of moves, which will transform the first arrangement into the second one. Of course you always have to maintain the constraint that smaller disks must be upon the larger ones.
Input
The input file contains at most 100 test cases. Each test case starts with a positive integer N ( 1N60), which means the number of disks. You will be given the arrangements in next two lines. Each arrangement will be represented by N integers, which are 1, 2 or 3. If the i-th ( 1iN) integer is 1, you should consider that i-th disk is on Peg-A. Input is terminated by N = 0. This case should not be processed.
Output
Output of each test case should consist of a line starting with `Case #: ' where # is the test case number. It should be followed by the minimum number of moves as specified in the problem statement.
Sample Input
3
1 1 1
2 2 2
3
1 2 3
3 2 1
4
1 1 1 1
1 1 1 1
0
Sample Output
Case 1: 7
Case 2: 3
Case 3: 0
代码:
1 #include<cstdio>
2 const int maxn =70;
3 int n,start[maxn],finish[maxn];
4 long long Func(int *p,int i,int final)
5 {
6 if(i==0) return 0;
7 if(p[i]==final) return Func(p,i-1,final);
8 return Func(p,i-1,6-p[i]-final)+(1LL<<(i-1));
9 }
10 int main()
11 {
12 int kase=0;
13 while(scanf("%d",&n)==1&&n)
14 {
15 for(int i=1;i<=n;i++)
16 scanf("%d",&start[i]);
17 for(int i=1;i<=n;i++)
18 scanf("%d",&finish[i]);
19 int k=n;
20 while(k>=1 && start[k]==finish[k])k--;
21
22 long long ans=0;
23 if(k>=1)
24 {
25 int other=6-start[k]-finish[k];
26 ans =Func(start,k-1,other)+Func(finish,k-1,other)+1;
27 }
28 printf("Case %d: %lldn",++kase,ans);
29 }
30 }
Problem setter: Md. Kamruzzaman
Special Thanks: Derek Kisman (Alternate Solution), Shahriar Manzoor (Picture Drawing)
Miguel Revilla 2004-12-10
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 猿思考系列8——缓存的套路也就这些
- 猿思考系列9——一文获取隐藏逻辑挖掘办法
- 猿蜕变系列1——春天的故事
- 猿蜕变系列2——一文搞懂spring的花式DI
- 猿蜕变系列3——SpringMVC之初体验
- 猿蜕变4——一文获取web框架正确学习套路
- 猿蜕变系列5——一文搞懂Controller的花式编写
- 猿蜕变系列6——一文掌握springMVC必会技巧
- ModuleNotFoundError: No module named ‘__main__.XX‘;
- 猿蜕变系列7——也说说springMVC上传姿势
- 猿蜕变系列8——一文搞懂Interceptor操纵姿势
- 猿蜕变9——一文搞定SpringMVC的RESTFul套路
- 猿蜕变10——一文学会ORM的正确打开姿势
- 猿蜕变11——一文掌握mybatis全局玩儿法
- 猿蜕变12——一文搞定mybatis花式玩法