UVA 101 The Blocks Problem
2013-07-16 09:50
375 查看
UVA 101 The Blocks Problem
纯粹的模拟,,一开始题意理解错了 在POJ还过了,,换成UVA WA掉了 又改 才过的#include <stdio.h> #include <string.h> int n; char a[10], b[10]; int aa, bb; int stack[255][255]; int num[255]; int x, y; int x2, y2; void find(int xx) { int i, j; for (i = 0; i < n; i ++) for (j = 0; j < n; j ++) { if (stack[i][j] == xx) { x = i; y = j; return; } } } void find2(int xx) { int i, j; for (i = 0; i < n; i ++) for (j = 0; j < n; j ++) { if (stack[i][j] == xx) { x2 = i; y2 = j; return; } } } void move_onto(int a, int b) { find(a); find2(b); int i; for (i = num[x] - 1; i >= y + 1; i --) { stack[stack[x][i]][num[stack[x][i]]] = stack[x][i]; num[stack[x][i]] ++; stack[x][i] = -1; num[x] --; } stack[x][y] = -1; num[x] --; for (i = num[x2] - 1; i >= y2 + 1; i --) { stack[stack[x2][i]][num[stack[x2][i]]] = stack[x2][i]; num[stack[x2][i]] ++; stack[x2][i] = -1; num[x2] --; } stack[x2][y2 + 1] = a; num[x2] ++; } void pile_onto(int a, int b) { int sb; find(a); find2(b); int aa[30]; int t = 0; sb = num[x]; int i; for (i = y; i < sb; i ++) { aa[t ++] = stack[x][i]; stack[x][i] = -1; num[x] --; } for (i = num[x2] - 1; i > y2; i --) { stack[stack[x2][i]][num[stack[x2][i]]] = stack[x2][i]; num[stack[x2][i]] ++; stack[x2][i] = -1; num[x2] --; } int tt = 0; for (i = y2 + 1; i < y2 + 1 + t; i ++) { stack[x2][i] = aa[tt++]; num[x2] ++; } } void pile_over(int a, int b) { int sb; find(a); find2(b); int aa[30]; int t = 0; sb = num[x]; int i; for (i = y; i < sb; i ++) { aa[t ++] = stack[x][i]; stack[x][i] = -1; num[x] --; } int tt = 0; sb = num[x2]; for (i = sb; i < sb + t; i ++) { stack[x2][i] = aa[tt ++]; num[x2] ++; } } void move_over(int a, int b) { find(a); find2(b); int i; for (i = num[x] - 1; i >= y + 1; i --) { stack[stack[x][i]][num[stack[x][i]]] = stack[x][i]; num[stack[x][i]] ++; stack[x][i] = -1; num[x] --; } stack[x][y] = -1; num[x] --; stack[x2][num[x2]] = a; num[x2] ++; } int main() { while (scanf("%d", &n) != EOF) { memset(stack, -1, sizeof(stack)); memset(num, 0, sizeof(num)); int i; int j; for (i = 0; i < n; i ++) { stack[i][0] = i; num[i] ++; } getchar(); while (scanf("%s",a) != EOF) { int ju1 = 3; int ju2 = 3; if (strcmp(a, "quit") == 0) { break; } scanf("%d %s %d", &aa, b, &bb); find(aa); find2(bb); if (x == x2 || aa == bb) continue; if (strcmp(a, "move") == 0) { ju1 = 0; } if (strcmp(a, "pile") == 0) { ju1 = 1; } if (strcmp(b, "onto") == 0) { ju2 = 0; } if (strcmp(b, "over") == 0) { ju2 = 1; } if (ju1 == 0 && ju2 == 0) { move_onto(aa, bb); } if (ju1 == 0 && ju2 == 1) { move_over(aa, bb); } if (ju1 == 1&& ju2 == 0) { pile_onto(aa, bb); } if (ju1 == 1 && ju2 == 1) { pile_over(aa, bb); } } for (i = 0 ; i < n; i ++) { printf("%d:", i); for (j = 0; j < num[i]; j ++) { if(stack[i][j] != -1) printf(" %d", stack[i][j]); } printf("\n"); } } return 0; }
相关文章推荐
- UVA-101 The Blocks Problem
- uva_101 - The Blocks Problem
- uva 101 - The Blocks Problem
- Uva101—— The Blocks Problem
- UVa 101 - The Blocks Problem
- UVA - 101 The Blocks Problem
- UVa101 - 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
- 紫薯e5-2 uva101 the blocks problem
- UVa OJ The Blocks Problem 木块问题 101
- UVA 101 The Blocks Problem(模拟)
- UVa 101 - The Blocks Problem|java实现
- UVA ~ 101 ~ The Blocks Problem(vector+模拟)
- uva 101 - The Blocks Problem
- UVA 101 - The Blocks Problem
- UVa Problem 101 - The Blocks Problem