uva 101 The Blocks Problem(模拟栈)
2014-07-17 16:47
344 查看
直接转题解:
题目:给你n个方块,有四种操作:
1.move a onto b,把a和b上面的方块都放回原来位置,然后把a放到b上面;
2.move a over b,把a上面的放回原处,然后把a放在b所在的方块堆的上面;
3.pile a onto b,把b上面的放回原来位置,然后把a和a上面的方块整体放到b上面;
4.pile a over b,把a和a上面的方块整体放到b所在堆的上面。
分析:模拟,数据结构。观察操作,如果是move就是先把a上面的还原,如果是onto就是先把b上面的还原。
然后,就是移动一堆到另一堆的上面(单个也认为是一堆)。所以设置两个基础操作:
1.将a上面的还原init_place(a);
2.将a和上面的(可以没有上面的)放到b上面pile_a_to_b(a,b)。
那么上述的四组操作就变成下面了:
1.move a onto b,init_place(a);init_place(b);pile_a_to_b(a,b);
2.move a over b,init_place(a);pile_a_to_b(a,b);
3.pile a onto b,init_place(b);pile_a_to_b(a,b);
4.pile a over b,pile_a_to_b(a,b)。
利用两个操作轻松解决。具体实现时设置一个place数组记录每个编号的方块对应的堆。
注意:如果a和b已经在一堆中就不要操作,此时认为不用移动,否则会WA。
代码:
题目:给你n个方块,有四种操作:
1.move a onto b,把a和b上面的方块都放回原来位置,然后把a放到b上面;
2.move a over b,把a上面的放回原处,然后把a放在b所在的方块堆的上面;
3.pile a onto b,把b上面的放回原来位置,然后把a和a上面的方块整体放到b上面;
4.pile a over b,把a和a上面的方块整体放到b所在堆的上面。
分析:模拟,数据结构。观察操作,如果是move就是先把a上面的还原,如果是onto就是先把b上面的还原。
然后,就是移动一堆到另一堆的上面(单个也认为是一堆)。所以设置两个基础操作:
1.将a上面的还原init_place(a);
2.将a和上面的(可以没有上面的)放到b上面pile_a_to_b(a,b)。
那么上述的四组操作就变成下面了:
1.move a onto b,init_place(a);init_place(b);pile_a_to_b(a,b);
2.move a over b,init_place(a);pile_a_to_b(a,b);
3.pile a onto b,init_place(b);pile_a_to_b(a,b);
4.pile a over b,pile_a_to_b(a,b)。
利用两个操作轻松解决。具体实现时设置一个place数组记录每个编号的方块对应的堆。
注意:如果a和b已经在一堆中就不要操作,此时认为不用移动,否则会WA。
代码:
#include <cstdio> #include <cstring> using namespace std; int block[25][25]; int top[25]; int place[25]; void Init() { for (int i = 0; i < 25; i++) { block[i][0] = i; top[i] = 0; place[i] = i; } } void InitPlace(int a) { int pos = place[a]; while (block[pos][top[pos]] != a) { int tmp = block[pos][top[pos]--]; place[tmp] = tmp; block[tmp][++top[tmp]] = tmp; } } void Pile(int a, int b) { int tmp[25]; int tmptop = -1; int posa = place[a], posb = place[b]; while (block[posa][top[posa]] != a) tmp[++tmptop] = block[posa][top[posa]--]; place[a] = posb; block[posb][++top[posb]] = a; top[posa]--; while (tmptop >= 0) { place[tmp[tmptop]] = posb; block[posb][++top[posb]] = tmp[tmptop--]; } } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); #endif // LOCAL int n; while (scanf("%d", &n) == 1) { Init(); char q1[5], q2[5]; int a, b; while (scanf("%s", q1)) { getchar(); if (strcmp(q1, "quit") == 0) break; scanf("%d %s %d",&a, q2, &b); //printf("%s %d %s %d\n", q1, a, q2, b); if ( place[a] == place[b] )//debug continue; if (strcmp(q1, "move") == 0) { if (strcmp(q2, "onto") == 0) { InitPlace(a); InitPlace(b); Pile(a, b); } if (strcmp(q2, "over") == 0) { InitPlace(a); Pile(a, b); } } if (strcmp(q1, "pile") == 0) { if (strcmp(q2, "onto") == 0) { InitPlace(b); Pile(a, b); } if (strcmp(q2, "over") == 0) { Pile(a, b); } } } for (int i = 0; i < n; i++) { printf("%d:", i); for (int j = 0; j <= top[i]; j++) printf(" %d", block[i][j]); printf("\n"); } } return 0; }
相关文章推荐
- Uva-101-The Blocks Problem
- uva 101 - The Blocks Problem
- uva 101 - The Blocks Problem
- UVa 101 - The Blocks Problem
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
- UVa 101 The Blocks Problem
- Uva 101 - The Blocks Problem
- UVa-101 The Blocks Problem(栈模拟)
- uva 101 the blocks problem
- uva_101 - The Blocks Problem
- UVA101 The Blocks Problem(模拟)
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
- UVA - 101 The Blocks Problem
- Uva 101 The blocks problem
- uva 101 The Blocks Problem
- uva 101 - The Blocks Problem
- STL-E The Blocks Problem (UVA - 101)
- UVa系列——101/The Blocks Problem
- UVA-101 The Blocks Problem 栈模拟
- UVa 101-The Blocks Problem