POJ 1920 Towers of Hanoi

OJ题目:click here~~

题目分析:三根柱子 , n个圆盘 。给一个汉诺塔的状态,求将所有盘挪到一个柱子上的最少步数,并给出是最后在哪个柱子上。

从给定状态到目标状态很复杂,但是从目标状态到给定的状态就很容易想了。将一个柱子上i个盘,挪到另一个柱子上,需要pow(2,i) - 1步。 显然,最后在的那个柱子,一定是所给状态下最大盘所在的柱子。接下来考虑第二大的盘,需要移动就移动。……详见代码注释。

AC_CODE

const int mod = 1000000;
int p[100002] , s[100002];
int   main(){
      int n  , i , j , k  ,a , x[4] ;
      s[0] = 1;
      for(i = 1;i <= 100000;i++)
         s[i] = 2*s[i - 1] , s[i] %= mod;
      while(cin >> n){
          scanf("%d%d%d",&x[1],&x[2],&x[3]);
          for(i = 1;i <= 3;i++)
          for(j = 0;j < x[i];j++){//记录所给状态每个盘在哪个柱子
            scanf("%d",&a);
            p[a] = i;
          }
          int now = p[n];
          int need = p[n - 1];
          int ans = 0;
          for(i = n - 1;i > 0;i-- , need = p[i]){
            if(need != now){//此刻的状态与需要的状态不一样,则需要移动
                ans += s[i - 1];//ans += (s[i - 1] - 1 + 1)
                ans %= mod;
                now = 6 - need - now;//盘i以上的所0有盘,先要挪到除这两个以外的第三个柱子上。
            }
          }
          cout << p[n] << endl << ans << endl;//最后所在的柱子,一定是最大盘在的柱子
      }
      return 0 ;
}

POJ 1920 Towers of Hanoi,码迷,mamicode.com

时间: 2024-08-09 19:49:46

POJ 1920 Towers of Hanoi的相关文章

POJ 1958 Strange Towers of Hanoi (线性dp,记忆化搜索)

JQuery工具方法. (1)$.isNumeric(obj) 此方法判断传入的对象是否是一个数字或者可以转换为数字. isNumeric: function( obj ) { // parseFloat NaNs numeric-cast false positives (null|true|false|"") // ...but misinterprets leading-number strings, particularly hex literals ("0x...&

POJ 1958 Strange Towers of Hanoi (四塔问题,线性dp,记忆化搜索)

题目分析:四柱汉诺塔.由于题目已经给出了求解方法,直接写代码即可.下面总结一下,四塔问题. 感谢这篇文章的作者,点这里就到,总结的很好.直接贴过来~ 四塔问题:设有A,B,C,D四个柱子(有时称塔),在A柱上有由小到大堆放的n个盘子. 今将A柱上的盘子移动到D柱上去.可以利用B,C柱作为工作栈用,移动的规则如下: ①每次只能移动一个盘子. ②在移动的过程中,小盘子只能放到大盘子的上面. 设计并实现一个求解四塔问题的动态规划算法,并分析时间和空间复杂性. 算法思想: 用如下算法移动盘子(记为Fou

poj 1958 Strange Towers of Hanoi

Strange Towers of Hanoi Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2678   Accepted: 1742 Description Background Charlie Darkbrown sits in another one of those boring Computer Science lessons: At the moment the teacher just explains

POJ 1958 Strange Towers of Hanoi 解题报告

Strange Towers of Hanoi 大体意思是要求\(n\)盘4的的hanoi tower问题. 总所周知,\(n\)盘3塔有递推公式\(d[i]=dp[i-1]*2+1\) 令\(f[i]\)为4塔转移步骤. \(f[i]=min(f[i],f[k]*2+d[i-k])\) 即先以4塔以上面的\(k\),再以3塔移\(i-k\),最后以4塔移动回去. 可以推广到\(n\)盘\(m\)塔 2018.5.26 原文地址:https://www.cnblogs.com/ppprseter

Strange Towers of Hanoi (POJ1958)

Strange Towers of Hanoi (POJ1958) n个盘子4座塔的Hanoi问题至少需要多少步?(1<=n<=12) 分析: n盘3塔: \(d[n] = 2*d[n-1]+1\) => \(d[n] = 2^n - 1\) 前n-1盘子 A -> B 第n盘子 A -> C 前n-1盘子 B -> C n盘4塔:\(f[n] = min_{1\leq i<n}\{2*f[i] + d[n-i]\}\) 把i个盘子 A->B (四塔模式)

URAL 2029 Towers of Hanoi Strike Back 汉诺塔,从初始状态到任意给出状态需要的次数

F - Towers of Hanoi Strike Back Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 2029 Description The Tower of Hanoi puzzle was invented by French mathematician édouard Lucas in the second half

The Towers of Hanoi Revisited---(多柱汉诺塔)

Description You all must know the puzzle named "The Towers of Hanoi". The puzzle has three pegs and N discs of different radii, initially all disks are located on the first peg, ordered by their radii - the largest at the bottom, the smallest at

SGU 202 The Towers of Hanoi Revisited (DFS+预处理)

题目描述: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2338 The Towers of Hanoi Revisited Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge You all must know the puzzle named ??The Towers of Hanoi??. The puzzle has thre

[CareerCup] 3.4 Towers of Hanoi 汉诺塔

3.4 In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from top to bottom (i.e., each disk sits on top of an e