随笔 - 6, 文章 - 0, 评论 - 3, 引用 - 0
数据加载中……

UVA_101

1. move a onto b
在將a搬到b上之前,先將a和b上的積木放回原來的位置(例如:1就放回1的最開始位罝)
2. move a over b
在將a搬到b所在的那堆積木之上之前,先將a上的積木放回原來的位罝(b所在的那堆積木不動)
3. pile a onto b
將a本身和其上的積木一起放到b上,在搬之前b上方的積木放回原位
4. pile a over b
將a本身和其上的積木一起搬到到b所在的那堆積木之上

  1 #include <vector>
  2 #include <list>
  3 #include <map>
  4 #include <set>
  5 #include <deque>
  6 #include <queue>
  7 #include <stack>
  8 #include <bitset>
  9 #include <algorithm>
 10 #include <functional>
 11 #include <numeric>
 12 #include <utility>
 13 #include <sstream>
 14 #include <iostream>
 15 #include <iomanip>
 16 #include <cstdio>
 17 #include <cmath>
 18 #include <cstdlib>
 19 #include <cctype>
 20 #include <string>
 21 #include <cstring>
 22 #include <cstdio>
 23 #include <cmath>
 24 #include <cstdlib>
 25 #include <ctime>
 26 
 27 using namespace std;
 28 
 29 #define M 26
 30 int main()
 31 {
 32     int n;
 33     int g[M][M];
 34     int p[M];
 35     string com1,com2;
 36     int a,b;
 37     while (cin>>n)
 38     {
 39         for (int i=0;i<n;i++)
 40         {
 41             p[i]=i;
 42             g[i][0]=i;
 43             for (int j=1;j<n;j++)
 44             {
 45                 g[i][j]=-1;
 46             }
 47         }
 48         while (1)
 49         {
 50             cin>>com1;
 51             if (com1==”quit”) break;
 52             cin>>a>>com2>>b;
 53             if (p[a]==p[b]||a==b) continue;
 54             if (com1==”move”)
 55             {
 56                 if (com2==”onto”)
 57                 {
 58                     int pa,pb;
 59                     int i=0;
 60                     while (g[p[a]][i]!=-1)
 61                     {
 62                         int t=g[p[a]][i];
 63                         if (t==a)
 64                         {
 65                             pa=i;
 66                             i++;
 67                             break;
 68                         }
 69                         i++;
 70                     }
 71                     while (g[p[a]][i]!=-1)
 72                     {
 73                         int t=g[p[a]][i];
 74                         int j=0;
 75                         while (g[t][j]!=-1)
 76                         {
 77                             j++;
 78                         }
 79                         g[t][j]=t;
 80                         p[t]=t;
 81                         g[p[a]][i]=-1;
 82                         i++;
 83                     }
 84 //remove above a
 85                     i=0;
 86                     while (g[p[b]][i]!=-1)
 87                     {
 88                         int t=g[p[b]][i];
 89                         if (t==b)
 90                         {
 91                             pb=i;
 92                             i++;
 93                             break;
 94                         }
 95                         i++;
 96                     }
 97                     while (g[p[b]][i]!=-1)
 98                     {
 99                         int t=g[p[b]][i];
100                         int j=0;
101                         while (g[t][j]!=-1)
102                         {
103                             j++;
104                         }
105                         g[t][j]=t;
106                         p[t]=t;
107                         g[p[b]][i]=-1;
108                         i++;
109                     }
110 //remove above b
111 
112 //put a above b
113                     g[p[b]][++pb]=a;
114                     g[p[a]][pa]=-1;
115                     p[a]=p[b];
116 
117                 }
118                 else if (com2==”over”)
119                 {
120                     int pa,pb;
121                     int i=0;
122                     while (g[p[a]][i]!=-1)
123                     {
124                         int t=g[p[a]][i];
125                         if (t==a)
126                         {
127                             pa=i;
128                             i++;
129                             break;
130                         }
131                         i++;
132                     }
133                     while (g[p[a]][i]!=-1)
134                     {
135                         int t=g[p[a]][i];
136                         int j=0;
137                         while (g[t][j]!=-1)
138                         {
139                             j++;
140                         }
141                         g[t][j]=t;
142                         p[t]=t;
143                         g[p[a]][i]=-1;
144                         i++;
145                     }
146 //remove above a
147 
148                     i=0;
149                     while (g[p[b]][i]!=-1) i++;
150                     g[p[b]][i]=a;
151                     g[p[a]][pa]=-1;
152                     p[a]=p[b];
153                 }
154             }
155             else if (com1==”pile”)
156             {
157                 if (com2==”onto”)
158                 {
159                     int i=0,pb;
160                     while (g[p[b]][i]!=-1)
161                     {
162                         int t=g[p[b]][i];
163                         if (t==b)
164                         {
165                             pb=i;
166                             i++;
167                             break;
168                         }
169                         i++;
170                     }
171                     while (g[p[b]][i]!=-1)
172                     {
173                         int t=g[p[b]][i];
174                         int j=0;
175                         while (g[t][j]!=-1)
176                         {
177                             j++;
178                         }
179                         g[t][j]=t;
180                         p[t]=t;
181                         g[p[b]][i]=-1;
182                         i++;
183                     }
184 
185                     i=0;
186                     int t=p[a];
187                     while (g[t][i]!=a) i++;
188                     while (g[t][i]!=-1)
189                     {
190                         int tt=g[t][i];
191                         p[tt]=p[b];
192                         g[p[b]][++pb]=tt;
193                         g[t][i]=-1;
194                         i++;
195                     }
196                 }
197                 else if (com2==”over”)
198                 {
199 //printf(”%d %d\n”,a,b);
200 //printf(”%d %d\n”,p[a],p[b]);
201                     int j=0;
202                     while (g[p[b]][j]!=-1) j++;
203 
204                     int i=0,t=p[a];
205                     while (g[t][i]!=a) i++;
206                     while (g[t][i]!=-1)
207                     {
208                         int tt=g[t][i];
209                         p[tt]=p[b];
210                         g[p[b]][j++]=tt;
211                         g[t][i]=-1;
212                         i++;
213                     }
214                 }
215             }
216         }
217         for (int i=0;i<n;i++)
218         {
219             printf(”%d:”,i);
220             int j=0;
221             while (g[i][j]!=-1)
222             {
223                 printf(” %d”,g[i][j]);
224                 j++;
225             }
226             printf(”\n”);
227         }
228     }
229     return 0;
230 }
231 

posted on 2008-10-07 23:50 水牛♂Toto 阅读(511) 评论(2)  编辑 收藏 引用

评论

# re: UVA_101  回复  更多评论   

这个要编译多久-_-
2012-02-02 20:30 | twq

# re: UVA_101  回复  更多评论   

这个题目你居然能写出200多行。也是醉了
2014-11-30 15:10 |

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