zoj 2338 The Towers of Hanoi Revisited

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<stack>
 4 using namespace std;
 5 stack<int> a[3];
 6 int b[60];
 7 int q=0;
 8 int solve(int from,int to)
 9 {
10     if(a[from].empty())
11     {//-1有错
12         return -1;
13     }
14     if(!a[to].empty())//a[to].top()有可能为空,要先进行判断
15         if(a[from].top()>a[to].top())
16             return -1;
17     a[to].push(a[from].top());
18     a[from].pop();
19     return 0;
20 }
21 void empty()
22 {
23     int i;
24     for(i=0;i<3;i++)
25     {
26         if(!a[i].empty())
27             a[i].pop();
28     }
29 }
30 int main()
31 {
32     int T,n,m,i,j,from,to,get;
33     while(cin>>T)
34     {
35         while(T--)
36         {
37             cin>>n>>m;
38             for(i=n;i>0;i--)
39                 a[0].push(i);
40             for(j=1;j<=m;j++)
41             {
42                 cin>>from>>to;
43                 get=solve(from-1,to-1);
44                 if(get==-1)
45                 {
46                     b[q++]=0-j;
47                     empty();//每次退出时应该把栈的内容清空
48                     while(j<m)//要等输入完成才能退出本次循环
49                     {
50                         j++;
51                         cin>>from>>to;
52                     }
53                     break;
54                 }
55                 if(a[0].empty()&&a[1].empty()&&a[2].top()==1)
56                 {
57                     b[q++]=j;
58                     empty();
59                     while(j<m)
60                     {
61                         j++;
62                         cin>>from>>to;
63                     }
64                     break;
65                 }else if(j==m)
66                 {
67                     b[q++]=0;
68                     empty();
69                 }
70             }
71         }
72         for(i=0;i<q;i++)
73             cout<<b[i]<<endl;
74     }
75     return 0;
76 }
时间: 2024-11-06 09:54:11

zoj 2338 The Towers of Hanoi Revisited的相关文章

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

Acdream 1219 The Towers of Hanoi Revisited(递归汉诺塔问题)

传送门 The Towers of Hanoi Revisited Special Judge Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description You all must know the puzzle named "The Towers of Hanoi". The puzz

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

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 1920 Towers of Hanoi

OJ题目:click here~~ 题目分析:三根柱子 , n个圆盘 .给一个汉诺塔的状态,求将所有盘挪到一个柱子上的最少步数,并给出是最后在哪个柱子上. 从给定状态到目标状态很复杂,但是从目标状态到给定的状态就很容易想了.将一个柱子上i个盘,挪到另一个柱子上,需要pow(2,i) - 1步. 显然,最后在的那个柱子,一定是所给状态下最大盘所在的柱子.接下来考虑第二大的盘,需要移动就移动.--详见代码注释. AC_CODE const int mod = 1000000; int p[10000

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 (四塔问题,线性dp,记忆化搜索)

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

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

[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