pku1920 Towers of Hanoi 河内塔问题

给出河内塔的中间状态,求将所有盘子摞到一起的的最小步数。
先贴程序
 1 import java.util.*;
 2 import java.io.*;
 3 public class Main {
 4 
 5     /**
 6      * @param args
 7      */
 8     static StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
 9     static final int nextInt() throws Exception
10     {
11         in.nextToken();
12         return (int)in.nval;
13     }
14     public static void main(String[] args) throws Exception{
15         
16         int num=nextInt();
17         int step[]=new int[num+1];
18         int id[]=new int[num+1];
19         step[1]=1;
20         for(int i=2;i<=num;i++)
21             step[i]= (step[i-1]<<1)%1000000;
22         ArrayList<Integer> data[]=new ArrayList[3];
23         for(int i=0;i<3;i++)
24             data[i]=new ArrayList<Integer>();
25         int a=nextInt(),b=nextInt(),c=nextInt();
26         while((a--)!=0)
27             id[nextInt()]=0;
28         while((b--)!=0)
29             id[nextInt()]=1;
30         while((c--)!=0)
31             id[nextInt()]=2;
32         int s=id[num],e=s,mid=0,total=0;
33         for(int i=num;i>=1;)
34         {
35             if(s!=e)
36             {
37                 total=(step[i]+total)%1000000;
38                 e=mid;
39             }
40             s=id[--i];
41             mid=3-s-e;
42         }
43         System.out.println(id[num]+1);
44         System.out.println(total);
45         
46         
47         
48     }
49 
50 }
51 


posted on 2010-11-01 02:31 yzhw 阅读(177) 评论(0)  编辑 收藏 引用 所属分类: combination mathdata struct


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

公告

统计系统

留言簿(1)

随笔分类(227)

文章分类(2)

OJ

最新随笔

搜索

积分与排名

最新评论

阅读排行榜