您的位置:首页 > 其它

uva 10051 Tower of Cubes

2014-06-03 22:11 295 查看
首先把一个方块拆成6种不同的形态,然后就是最长递增子串问题,递增的逻辑就是方块底面和顶面的颜色的配对与否。

#include <stdio.h>
#include <vector>
#include <stack>
using namespace std;

#define FRONT 0
#define BACK 1
#define LEFT 2
#define RIGHT 3
#define TOP 4
#define BOTTOM 5

int n, max_len;
struct node{
int type;
int bottom_color;
int top_color;
};

struct path_node{
int index;
int type;
};

struct cube{
int color[6];
};
struct cube cube_arr[505];

vector<struct node> arr;

struct dp_node{
int len;
int last_index;
};
struct dp_node dp[3030];

void get_all_type(const struct cube &cube_ins, vector<struct node> &v){
v.clear();
struct node new_node;

new_node.type = FRONT;
new_node.top_color = cube_ins.color[FRONT];
new_node.bottom_color = cube_ins.color[BACK];
v.push_back(new_node);

new_node.type = BACK;
new_node.top_color = cube_ins.color[BACK];
new_node.bottom_color = cube_ins.color[FRONT];
v.push_back(new_node);

new_node.type = LEFT;
new_node.top_color = cube_ins.color[LEFT];
new_node.bottom_color = cube_ins.color[RIGHT];
v.push_back(new_node);

new_node.type = RIGHT;
new_node.top_color = cube_ins.color[RIGHT];
new_node.bottom_color = cube_ins.color[LEFT];
v.push_back(new_node);

new_node.type = TOP;
new_node.top_color = cube_ins.color[TOP];
new_node.bottom_color = cube_ins.color[BOTTOM];
v.push_back(new_node);

new_node.type = BOTTOM;
new_node.top_color = cube_ins.color[BOTTOM];
new_node.bottom_color = cube_ins.color[TOP];
v.push_back(new_node);
}

void func(int n){
int i, j, len, cur_i;
int max_len, max_i;
vector<struct node> all_type;
static int case_n = 1;

arr.clear();
for(i=1; i<=n; i++){
get_all_type(cube_arr[i], all_type);
for(j=0; j<6; j++){
arr.push_back(all_type[j]);
}
}

dp[0].len = 1;
dp[0].last_index = -1;
max_i = 0;
max_len = 1;
for(i=1; i<arr.size(); i++){
dp[i].len = 1;
dp[i].last_index = -1;
for(j=i-1; j>=0; j--){
if(j+2 <= dp[i].len)
break;

if(i/6!=j/6 && arr[j].bottom_color==arr[i].top_color){
len = dp[j].len + 1;
if(len > dp[i].len){
dp[i].len = len;
dp[i].last_index = j;
}
}
}
if(max_len < dp[i].len){
max_len = dp[i].len;
max_i = i;
}
}

stack<struct path_node> s;
struct path_node path_node_ins;

cur_i = max_i;
for(i=1; i<=max_len; i++){
path_node_ins.index = cur_i/6+1;
path_node_ins.type = arr[cur_i].type;
s.push(path_node_ins);
cur_i = dp[cur_i].last_index;
}

if(case_n >= 2)
printf("\n");
printf("Case #%d\n", case_n++);
printf("%d\n", max_len);
while(!s.empty()){
printf("%d ", s.top().index);
switch(s.top().type){
case FRONT:
printf("front\n");
break;
case BACK:
printf("back\n");
break;
case LEFT:
printf("left\n");
break;
case RIGHT:
printf("right\n");
break;
case TOP:
printf("top\n");
break;
case BOTTOM:
printf("bottom\n");
break;
}
s.pop();
}
}

int main(void){
int i, j;

//freopen("input.dat", "r", stdin);
while(scanf("%d", &n), n){
for(i=1; i<=n; i++){
for(j=0; j<6; j++)
scanf("%d", cube_arr[i].color+j);
}
func(n);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm uva