hdu_1997_汉诺塔VII

反着考虑

第N个必然在a或者c上。

而第N-1个必须先移到b

顺次考虑下去

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     freopen("output.txt","r",stdin);//从文件output.txt读入
 7     freopen("input.txt","w",stdout);//写入到文件input.txt
 8     int N;
 9     bool flag;
10     int cas;
11     cin>>cas;
12     while(cas--)
13     {
14         cin>>N;
15         int num[4][65]={0};
16         int a=1,b=2,c=3;
17         int i,j,m[4];
18         
19         for(i = 1; i < 4 ; i++) //a b c 对应着1 2 3  对数组初始化
20         {
21             cin>>m[i];           //对应a b c
22             for(j = m[i]; j >= 1; j--) // 数组  从大到小的
23             {
24                 cin>>num[i][j];
25             }
26         }
27 
28         while(1){
29             if(m[a] == N || m[c]== N)
30             {
31                 flag = true;
32                 break;
33             }
34             if(m[b]>0&&num[b][m[b]] == N)
35             {
36                 flag = false;
37                 break;
38             }
39             if(m[a] > 0 && num[a][m[a]] == N)
40             {
41                 N--;
42                 m[a]--;
43                 int t = b;
44                 b = c; 
45                 c = t;
46                 continue;
47             }
48             if(m[c] > 0 && num[c][m[c]] == N)
49             {
50                 N--;
51                 m[c]--;
52                 int    t = b;
53                 b = a; 
54                 a = t;
55                 continue;
56             }
57         }
58         if(flag)
59             cout<<"true"<<endl;
60         else 
61             cout<<"false"<<endl;
62 
63     }
64     return 0;
65 }
原文地址:https://www.cnblogs.com/konkon/p/2480579.html