您的位置:首页 > 其它

UVA-101 The Blocks Problem

2016-08-18 19:29 381 查看
2016-08-18


UVA - 101 The Blocks Problem

题目大意:操作积木。有 0~n-1 个积木,初始状态是并排横放。有四种操作:move a onto b,将 a、b 上的方块放回原位然后将 a 放在 b 上;move a over b,将 a 上的方块放回原味然后将 a 放在 b 所在位置的最顶上;pile a onto b,将 b 上的方块放回原味然后将 a 及其上所有的方块放到 b 上;pile a over b,将 a 及其上所有方块放到 b 所在的位置最顶上。

解题思路:开个二维数组模拟就好,这种题目把最小的动作分解出来组合就好了。

注意:这题主要是要注意一些小细节,一直WA。。。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int map[50][50];
char str[2][20];
int a, b;

void find(int x) {
for (int i = 0; i < 25; i++)
for (int j = 0; j < 25; j++)
if ( map[i][j] == x ) {
a = i;
b = j;
}
}

void del(int x) {
find(x);
for (int i = b; i < 25; i++)
map[a][i] = -1;
}

void re(int x) {
find(x);
for (int i = b+1; i < 25; i++)
if ( map[a][i] != -1 ) {
int tmp = map[a][i];
map[tmp][0] = tmp;
map[a][i] = -1;
}
}

void move_onto(int x, int y) {
re(x);
re(y);
find(x);
map[a][b] = -1;
find(y);
map[a][b+1] = x;
}

void move_over(int x, int y) {
re(x);
del(x);
find(y);
for (int i = b; i < 25; i++)
if ( map[a][i] == -1) {
map[a][i] = x;
break;
}
}

void pile_onto(int x, int y) {
re(y);
find(x);
int tmp[50];
int j = 0;
for (int i = b; map[a][i] != -1; i++)
tmp[j++] = map[a][i];
int tag = j;
del(x);
find(y);
j = 0;
for (int i = b; j < tag; i++)
if ( map[a][i] == -1 )
map[a][i] = tmp[j++];
}

void pile_over(int x, int y) {
find(x);
int tmp[50];
int j = 0;
for (int i = b; map[a][i] != -1; i++)
tmp[j++] = map[a][i];
int tag = j;
del(x);
find(y);
j = 0;
for (int i = b; j < tag; i++)
if ( map[a][i] == -1 )
map[a][i] = tmp[j++];
}

int main() {
int n;
int c, d;
scanf("%d", &n);
memset(map, -1, sizeof(map));
for (int i = 0; i < n; i++)
map[i][0] = i;
while (	scanf("%s", str[0]) ) {
if ( !strcmp(str[0], "quit") )
break;
scanf("%d%s%d", &c, str[1], &d);
find(c);
int x1 = a;
find(d);
int x2 = a;
if ( x1 == x2 )
continue;
if ( !strcmp(str[0], "move") && !strcmp(str[1], "onto") )
move_onto(c, d);
if ( !strcmp(str[0], "move") && !strcmp(str[1], "over") )
move_over(c, d);
if ( !strcmp(str[0], "pile") && !strcmp(str[1], "onto") )
pile_onto(c, d);
if ( !strcmp(str[0], "pile") && !strcmp(str[1], "over") )
pile_over(c, d);
}
for (int i = 0; i < n; i++) {
cout << i << ":";
for (int j = 0; map[i][j] != -1; j++)
cout << " " << map[i][j];
cout << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: