给出河内塔的中间状态,求将所有盘子摞到一起的的最小步数。
先贴程序
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