Uva--10051(动规,记忆化搜索)

本文介绍了一道名为“Tower of Cubes”的算法题目,该题要求利用一系列具有不同颜色和重量的立方体构建最高的塔。文章详细解析了解题思路,包括如何处理立方体的旋转、颜色匹配限制及寻找最优解的方法。

2014-08-02 19:37:06

  Problem A: Tower of Cubes 

In this problem you are given N colorful cubes each having a distinct weight. Each face of a cube is colored with one color. Your job is to build a tower using the cubes you have subject to the following restrictions:

 

  • Never put a heavier cube on a lighter one.
  • The bottom face of every cube (except the bottom cube, which is lying on the floor) must have the same color as the top face of the cube below it.
  • Construct the tallest tower possible.

Input 

The input may contain multiple test cases. The first line of each test case contains an integer N ( $1 \le N \le 500$) indicating the number of cubes you are given. The i­th ( $1 \le i \le N$) of the next N lines contains the description of the i­th cube. A cube is described by giving the colors of its faces in the following order: front, back, left, right, top and bottom face. For your convenience colors are identified by integers in the range 1 to 100. You may assume that cubes are given in the increasing order of their weights, that is, cube 1 is the lightest and cube N is the heaviest.

The input terminates with a value 0 for N.

Output 

For each test case in the input first print the test case number on a separate line as shown in the sample output. On the next line print the number of cubes in the tallest tower you have built. From the next line describe the cubes in your tower from top to bottom with one description per line. Each description contains an integer (giving the serial number of this cube in the input) followed by a single white­space character and then the identification string (front, back, left, right, top or bottom) of the top face of the cube in the tower. Note that there may be multiple solutions and any one of them is acceptable.

Print a blank line between two successive test cases.

Sample Input 

3
1 2 2 2 1 2
3 3 3 3 3 3
3 2 1 1 1 1
10
1 5 10 3 6 5
2 6 7 3 6 9
5 7 3 2 1 9
1 3 3 5 8 10
6 6 2 2 4 4
1 2 3 4 5 6
10 9 8 7 6 5
6 1 2 3 4 7
1 2 3 3 2 1
3 2 1 1 2 3
0

Sample Output 

Case #1
2
2 front
3 front
 
Case #2
8
1 bottom
2 back
3 right
4 left
6 top
8 front
9 front
10 top 

思路:又是一个经典的方块叠塔,但是这里要记录一下路径(用next[]数组),由于方块可以旋转(每个面有各自颜色),所以把一个方块变成6个(6种面朝上),但是要注意这和以前每个方块无限量有区别,每个方块只有一个,所以应该给每个方块定义一个权,使一个方块变出的6个方块有相同的权,以免对一个方块重复取用(这题恰好用重量来定义这个权)
  1 /*************************************************************************
  2     > File Name: k.cpp
  3     > Author: Nature
  4     > Mail: 564374850@qq.com
  5     > Created Time: Thu 31 Jul 2014 03:41:44 PM CST
  6 ************************************************************************/
  7 
  8 #include <cstdio>
  9 #include <cstring>
 10 #include <cstdlib>
 11 #include <cmath>
 12 #include <vector>
 13 #include <iostream>
 14 #include <algorithm>
 15 using namespace std;
 16 
 17 struct node{
 18     int top;
 19     int bot;
 20     int w;
 21     int cnt;
 22     vector<int> s;
 23 }no[3005];
 24 
 25 int n,cnt;
 26 int dp[3005];
 27 int next[3005];
 28 
 29 void Build_graph(){
 30     for(int i = 0; i < cnt; ++i)
 31         for(int j = i + 1; j < cnt; ++j)
 32             if(no[i].w < no[j].w && no[i].bot == no[j].top){
 33                 ++no[i].cnt;
 34                 no[i].s.push_back(j);
 35             }
 36 }
 37 
 38 int Solve(int p){
 39     if(dp[p] != -1)
 40         return dp[p];
 41     int tmax = 0;
 42     int tem;
 43     for(int i = 0; i < no[p].cnt; ++i){
 44         if((tem = Solve(no[p].s[i])) > tmax){
 45             tmax = tem;
 46             next[p] = no[p].s[i];
 47         }
 48     }
 49     return dp[p] = tmax + 1;
 50 }
 51 
 52 int main(){
 53     //freopen("in","r",stdin);
 54     int Case = 0;
 55     int co[6];
 56     char str[6][10] = {"front","back","left","right","top","bottom"};
 57     while(scanf("%d",&n) == 1 && n){
 58         memset(dp,-1,sizeof(dp));
 59         memset(next,-1,sizeof(next));
 60         memset(no,0,sizeof(no));
 61         cnt = 6 * n;
 62         for(int i = 0; i < n; ++i){
 63             scanf("%d%d%d%d%d%d",&co[0],&co[1],&co[2],&co[3],&co[4],&co[5]);
 64             int k = i * 6;
 65             no[k].top = co[0];
 66             no[k].bot = co[1];
 67             no[k].w = i; // front
 68             no[k + 1].top = co[1];
 69             no[k + 1].bot = co[0];
 70             no[k + 1].w = i; //back
 71 
 72             no[k + 2].top = co[2];
 73             no[k + 2].bot = co[3];
 74             no[k + 2].w = i; //left
 75             no[k + 3].top = co[3];
 76             no[k + 3].bot = co[2];
 77             no[k + 3].w = i; //right
 78 
 79             no[k + 4].top = co[4];
 80             no[k + 4].bot = co[5];
 81             no[k + 4].w = i; //top
 82             no[k + 5].top = co[5];
 83             no[k + 5].bot = co[4];
 84             no[k + 5].w = i; //bottom
 85         }
 86         Build_graph();
 87         if(Case)
 88             printf("\n");
 89         printf("Case #%d\n",++Case);
 90         int ans = 0,tem,st;
 91         for(int i = 0; i < cnt; ++i){
 92             if((tem = Solve(i)) > ans){
 93                 ans = tem;
 94                 st = i;
 95             }
 96         }
 97         //for(int i = 0; i < cnt; ++i){
 98            // printf("dp[%d] : %d\n",i,dp[i]);
 99         //}
100         //for(int i = 0; i < cnt; ++i){
101            // printf("next[%d] : %d\n",i,next[i]);
102         //}
103         printf("%d\n",ans);
104         printf("%d %s\n",(st + 6) / 6,str[(st) % 6]);
105         while(next[st] != -1){
106             printf("%d %s\n",(next[st] + 6) / 6,str[(next[st]) % 6]);
107             st = next[st];
108         }
109     }
110     return 0;
111 }

 


转载于:https://www.cnblogs.com/naturepengchen/articles/3887368.html

先看效果: https://renmaiwang.cn/s/jkhfz Hue系列产品将具备高度的个性化定制能力,并且借助内置红、蓝、绿三原色LED的灯泡,能够混合生成1600万种不同色彩的灯光。 整个操作流程完全由安装于iPhone上的应用程序进行管理。 这一创新举措为智能照明控制领域带来了新的启示,国内相关领域的从业者也积极投身于相关研究。 鉴于Hue产品采用WiFi无线连接方式,而国内WiFi网络尚未全面覆盖,本研究选择应用更为普及的蓝牙技术,通过手机蓝牙与单片机进行数据交互,进而产生可调节占空比的PWM信号,以此来控制LED驱电路,实现LED的调光功能以及DIY调色方案。 本文重点阐述了一种基于手机蓝牙通信的LED灯设计方案,该方案受到飞利浦Hue智能灯泡的启发,但考虑到国内WiFi网络的覆盖限制,故而选用更为通用的蓝牙技术。 以下为相关技术细节的详尽介绍:1. **智能照明控制系统**:智能照明控制系统允许用户借助手机应用程序实现远程控制照明设备,提供个性化的调光及色彩调整功能。 飞利浦Hue作为行业领先者,通过红、蓝、绿三原色LED的混合,能够呈现1600万种颜色,实现了全面的定制化体验。 2. **蓝牙通信技术**:蓝牙技术是一种低成本、短距离的无线传输方案,工作于2.4GHz ISM频段,具备即插即用和强抗干扰能力。 蓝牙协议栈由硬件层和软件层构成,提供通用访问Profile、服务发现应用Profile以及串口Profiles等丰富功能,确保不同设备间的良好互操作性。 3. **脉冲宽度调制调光**:脉冲宽度调制(PWM)是一种高效能的调光方式,通过调节脉冲宽度来控制LED的亮度。 当PWM频率超过200Hz时,人眼无法察觉明显的闪烁现象。 占空比指的...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值