UVa 101 - The Blocks Problem解题报告

来源:互联网 发布:网络红歌大全 编辑:程序博客网 时间:2024/06/01 20:27

模拟类型的题,很麻烦。特别考验细心程度。虽然调试了很久,但是倒也学会了不少的调试技巧。

注意:无需考虑将积木放回原位时原位被占的情况,因为没有任何一个操作可以把积木放在空的位置上,因此也不可能出现在第i个位置上,第i个积木的下面有其它积木的情况。

思路:用一个二维数组模拟积木堆,用结构数组pos存储每个积木的x,y坐标,每个积木的代号正好与结构数组的下标形成一一映射。再用len数组存储存储每个堆的长度。接着就是模拟四个操作了。


//101 - The Blocks Problem#include <iostream>#include <cstring>#include <assert.h>using namespace std;struct coord{int x, y;};int stacks[30][30], len[30];coord pos[30];void over(char *, int, int);void onto(char *, int, int);void back(int);void print(int);int main(){//freopen("data.txt", "r", stdin);int n;scanf("%d", &n);getchar();for(int i = 0; i < n; i++)//初始化stacks[i][0] = i, len[i] = 1, pos[i].x = i, pos[i].y = 0;char str[50];while (cin.getline(str, 50)){if(str[0] == 'q')break;char a[5], b[5];int x1, x2;sscanf(str, "%[^ ] %d %[^ ] %d", a, &x1, b, &x2);//if(pos[x1].x == pos[x2].x)continue;if(strcmp(b, "onto") == 0)onto(a, x1, x2);elseover(a, x1, x2);//print(n);}print(n);return 0;}void onto(char *s, int x1, int x2){if(strcmp(s, "move") == 0){back(x1);//清除上层的积木back(x2);const int  W1 = pos[x1].x, W2 = pos[x2].x, H1 = pos[x1].y, H2 = pos[x2].y;stacks[W2][len[W2]++] = x1;//增加积木和长度len[W1]--;//减少长度stacks[W1][len[W1]] = -1;//删除积木pos[x1].x = pos[x2].x;//更新位置pos[x1].y = pos[x2].y + 1;}else{back(x2);const int  W1 = pos[x1].x, W2 = pos[x2].x, H1 = pos[x1].y, H2 = pos[x2].y, max = len[W1];for(int i = H1; i < max; i++){stacks[W2][len[W2]++] = stacks[W1][i];//增加积木pos[stacks[W1][i]].x = pos[x2].x;//更新位置pos[stacks[W1][i]].y = H2 + i - H1 + 1;//更新高度len[W1]--;//stacks[W1][i] = -1;//删除积木}}}void over(char *s, int x1, int x2){if(strcmp(s, "move") == 0){back(x1);const int  W1 = pos[x1].x, W2 = pos[x2].x, H1 = pos[x1].y, H2 = pos[x2].y;stacks[W2][len[W2]++] = x1;len[W1]--;//减少长度stacks[W1][len[W1]] = -1;//删除积木pos[x1].x = pos[x2].x;//更新位置pos[x1].y = len[W2] - 1;//更新高度}else{const int  W1 = pos[x1].x, W2 = pos[x2].x, H1 = pos[x1].y, Hmax = len[W2] - 1, max = len[W1];for(int i = H1; i < max; i++){stacks[W2][len[W2]++] = stacks[W1][i]; //增加积木pos[stacks[W1][i]].x = pos[x2].x;//更新位置pos[stacks[W1][i]].y = Hmax + i - H1 + 1;//更新高度len[W1]--;stacks[W1][i] = -1;}}}void back(int n){const int W = pos[n].x, H = pos[n].y, max = len[W];for(int i = H + 1; i < max; i++)//遍历n上面的积木{pos[stacks[W][i]].x = stacks[W][i];//其原位置就是本身的值pos[stacks[W][i]].y = 0;//更新高度stacks[stacks[W][i]][0] = stacks[W][i];//移回原位len[stacks[W][i]]++;//这里忘了加长stacks[W][i] = -1;//删除积木len[W]--;}}void print(int n){for(int i = 0; i < n; i++){printf("%d:", i);for(int j = 0; j < len[i]; j++)printf(" %d", stacks[i][j]);printf("\n");}}


0 0
原创粉丝点击