MATRIX_CHAIN

//这是按照算法导论给出解法写出的算法
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;

ifstream fin("C:\\data17.in");

char ID[100];
int length[100],width[100];
int n;
int multinum[100][100];
int s[100][100];
#define MAXNUM 65535

void Init()
{
	char name;
	int len,wid;
	n=0;
	while(fin>>name>>len>>wid)
	{
		ID[n]=name;
		length[n]=len;
		width[n++]=wid;
	}
	memset(multinum,0,sizeof(multinum));
	memset(s,0,sizeof(s));
}

void MATRIX_CHAIN_ORDER()
{
	for(int i=0;i<n;++i)
		multinum[i][i]=0;
	for(int l=2;l<=n;++l)
	{
		for(int i=0;i<n-l+1;++i)
		{
			int j=i+l-1;
			multinum[i][j]=MAXNUM;
			int p;
			for(int k=i;k<j;++k)
			{
				p=multinum[i][k]+multinum[k+1][j]+length[i]*width[k]*width[j];
				if(p<multinum[i][j])
				{
					multinum[i][j]=p;
					s[i][j]=k;
				}
			}
		}
	}
}

void PRINT_OPTIMAL_PARENS(int i,int j)
{
	if(i==j)
		cout<<ID[i];
	else
	{
		cout<<"(";
		PRINT_OPTIMAL_PARENS(i,s[i][j]);
		PRINT_OPTIMAL_PARENS(s[i][j]+1,j);
		cout<<")";
	}
}

int main()
{
	Init();
	MATRIX_CHAIN_ORDER();
	PRINT_OPTIMAL_PARENS(0,n-1);
	system("pause");
	return 0;
}

clc,close,clear all; p=[3,2,5,10,2,3]; [min_cost, optimal_order, m, s] = matrix_chain_order_with_heatmap(p, true); fprintf('最小乘法次数: %d\n', min_cost); fprintf('最优计算顺序: %s\n', optimal_order); fprintf('代价表 m:\n'); disp(m); fprintf('分割点表 s:\n'); disp(s); % function [all_cost,min_cost, optimal_order,st] = matrix_chain_order(p) % % n = length(p) - 1; % m = zeros(n, n); % s = zeros(n, n); % % % for i = 1:n % m(i, i) = 0; % end % % for L = 2:n % for i = 1:n-L+1 % j = i+L-1; % m(i, j) = realmax; % % for k = i:j-1 % cost = m(i, k) + m(k+1, j) + p(i)*p(k+1)*p(j+1); % fprintf('最小相乘次数=%d\n',cost); % if cost < m(i, j) % m(i, j) = cost; % s(i, j) = k; % end % end % % end % end % all_cost=m; % min_cost = m(1,n); % st=s; % optimal_order = print_optimal_order(s, 1, n); % end function [min_cost, optimal_order, m, s] = matrix_chain_order_with_heatmap(p, make_plot) % 矩阵链乘法动态规划实现(含热力图选项) % 输入: % p - 矩阵链的维度数组 % make_plot - 是否生成热力图 (true/false) % 输出: % min_cost - 最小乘法次数 % optimal_order - 最优括号化方案字符串 % m - 代价表 % s - 分割点表 n = length(p) - 1; m = zeros(n, n); s = zeros(n, n); % 初始化对角线 for i = 1:n m(i, i) = 0; end % 动态规划填表 for L = 2:n for i = 1:n-L+1 j = i+L-1; m(i, j) = realmax; for k = i:j-1 cost = m(i, k) + m(k+1, j) + p(i)*p(k+1)*p(j+1); if cost < m(i, j) m(i, j) = cost; s(i, j) = k; end end end end min_cost = m(1, n); optimal_order = print_optimal_order(s, 1, n); % 如果需要生成热力图 if make_plot plot_matrix_chain_heatmaps(p, m, s); end end % 辅助函数:递归构造最优括号化方案 function s_str = print_optimal_order(s, i, j) if i == j s_str = sprintf('A%d', i); else k = s(i, j); left_str = print_optimal_order(s, i, k); right_str = print_optimal_order(s, k+1, j); s_str = ['(' left_str ' × ' right_str ')']; end end代码报错 出错 untitled>matrix_chain_order_with_heatmap (第 86 行) plot_matrix_chain_heatmaps(p, m, s); 出错 untitled (第 3 行) [min_cost, optimal_order, m, s] = matrix_chain_order_with_heatmap(p, true);
最新发布
08-01
#include “esp_system.h” #include “Game_Main.h” #include “driver/gpio.h” #include “esptim.h” #include “esp_random.h” #include “esp_mesh.h” #include “…/Fun/videoplay.h” #include “…/Fun/draw.h” #include “…/Fun/RGBPanel.h” #include “…/Fun/LanGame.h” #include “…/Fun/GameDevice.h” #include “…/Fun/JC24BDevice.h” #include “…/Fun/video_control.h” #include “…/Fun/LanWifiMesh.h” #include “…/Fun/Button.h” #include “…/Fun/countdown_timer.h” #include “…/config.h” uint8_t GMAIN_GameState; static uint8_t GMAIN_TestColorIndex; static TIM_timing_t GMAIN_TimeObj1; static uint8_t GMAIN_BootClickCount = 0; static TIM_timing_t GMAIN_BootTimeObj; static uint8_t GMAIN_testTime = 3; CountdownTimer game_timer; // 全局倒计时器 static TickType_t last_update = 0; // 全局显示更新时间戳 // void GMAIN_ChangeState(uint8_t state) // { // // 状态切换前处理当前状态 // switch (GMAIN_GameState) { // case GAME_M_TIME: // // 如果当前是倒计时状态,暂停计时器 // if (timer_get_state(&game_timer) == TIMER_RUNNING) { // timer_pause(&game_timer); // } // break; // } // GMAIN_GameState = state; // 更新状态 // switch (GMAIN_GameState) // { // case 0: // video_control_play(VIDEO_LOGO, VIDEO_PLAY_LOOP, 0); // break; // case 1: // video_control_play(VIDEO_WIN, VIDEO_PLAY_LOOP, esp_mesh_is_root() ? 10 : 12); // break; // case 2: // video_control_play(VIDEO_WIN_ASSIST, VIDEO_PLAY_LOOP, esp_mesh_is_root() ? 10 : 15); // break; // case 3: // video_control_play(VIDEO_WWJ_STATE_COIN_IN, VIDEO_PLAY_SINGLE, 0); // break; // case 4: // video_control_play(VIDEO_WWJ_STATE_COIN_OK, VIDEO_PLAY_LOOP, 0); // break; // case 5: // video_control_play(VIDEO_WWJ_STATE_GAMMING, VIDEO_PLAY_LOOP, 0); // break; // case GAME_M_WWJ_STATE_STOP: // video_control_play(VIDEO_WWJ_STATE_STOP, VIDEO_PLAY_SINGLE, 0); // break; // case GAME_M_TIME: // // 初始化倒计时器(如果未初始化) // if (timer_get_state(&game_timer) == TIMER_STOPPED) { // timer_init(&game_timer, 10.0f); // 10秒倒计时 // } // // 启动倒计时 // timer_start(&game_timer); // // 初始化显示相关变量 // static TickType_t last_update = 0; // last_update = xTaskGetTickCount(); // 重置更新时间 // break; // case GAME_M_SELF_TEST: // video_stop(); // GMAIN_TestColorIndex = 0; // TIM_TIMING_SET_1MS(GMAIN_TimeObj1, 0); // break; // } // } // void GMAIN_Run(void) // { // switch (GMAIN_GameState) // { // case GAME_M_IDLE: // break; // case GAME_M_WIN: // break; // case GAME_M_WIN_ASSIST: // break; // case GAME_M_WWJ_STATE_COIN_IN: // case GAME_M_WWJ_STATE_COIN_OK: // break; // case GAME_M_WWJ_STATE_GAMMING: // break; // case GAME_M_WWJ_STATE_STOP: // break; // case GAME_M_TIME: // 倒计时状态处理 // // 获取剩余时间 // int integer_seconds, centiseconds; // timer_get_remaining_split(&game_timer, &integer_seconds, &centiseconds); // TickType_t now = xTaskGetTickCount(); // if (now - last_update >= pdMS_TO_TICKS(10)) { // RGBPanel_FillScreen(0x0000); // RGBPanel_ShowValue(10, 0, integer_seconds, // RGBPanel_IMAGE_WWJ_STATE_RED_NUM0, // RGBPanel_NO_IMAGE); // RGBPanel_drawImageById(40, 0, RGBPanel_IMAGE_WWJ_STATE_RED_Point); // RGBPanel_ShowValue(50, 0, centiseconds, // RGBPanel_IMAGE_WWJ_STATE_RED_NUM0, // RGBPanel_NO_IMAGE); // RGBPanel_Update(); // last_update = now; // } // // 检查是否超时 // if (timer_is_expired(&game_timer)) { // // 倒计时结束处理 // GMAIN_ChangeState(GAME_M_WIN); // 切换到胜利状态 // } // break; // case GAME_M_SELF_TEST: // if (TIM_TIMING_RUN(GMAIN_TimeObj1)) // { // if (GMAIN_TestColorIndex >= 8) // { // JC24BDevice_SendLogoPlay(); // LanWifiMesh_SendLogoPlay(); // GMAIN_ChangeState(GAME_M_IDLE); // return; // } // uint8_t r = 0; // uint8_t g = 0; // uint8_t b = 0; // if ((GMAIN_TestColorIndex & 0x01) > 0) // { // r = 0xFF; // } // if ((GMAIN_TestColorIndex & 0x02) > 0) // { // g = 0xFF; // } // if ((GMAIN_TestColorIndex & 0x04) > 0) // { // b = 0xFF; // } // RGBPanel_FillScreenRGB888(r, g, b); // RGBPanel_Update(); // GMAIN_TestColorIndex++; // TIM_TIMING_SET_100MS(GMAIN_TimeObj1, GMAIN_testTime); // } // break; // } // if (IO_KeyPressed(KEY_BUTTON_TEST)) // { // GMAIN_ChangeState(GAME_M_WIN); // JC24BDevice_BroadcastWin(); // LanWifiMesh_BroadcastWin(); // } // if (TIM_TIMING_RUN(GMAIN_BootTimeObj)) // { // TIM_TIMING_SET_1S(GMAIN_BootTimeObj, 10000); // GMAIN_BootClickCount = 0; // } // if (IO_KeyPressed(KEY_BUTTON_BOOT) && g_config.meshType == CONFIG_MESH_TYPE_ROOT) // { // GMAIN_BootClickCount++; // if (GMAIN_BootClickCount >= 5) // { // config_set_mesh_type(CONFIG_MESH_TYPE_CHILD); // esp_restart(); // } // TIM_TIMING_SET_1S(GMAIN_BootTimeObj, 2); // } // } extern int g_wifi_rssi; static void GMAIN_ShowRootFlag(void); void GMAIN_FrameCall(uint32_t frameIndex) { switch (g_config.matrix_chain) { case 2: { switch (GMAIN_GameState) { case GAME_M_WWJ_STATE_COIN_IN: RGBPanel_ShowValue(54, 18, g_GameDeviceData.coinInCount, RGBPanel_IMAGE_WWJ_STATE_WHITE_NUM0, RGBPanel_NO_IMAGE); RGBPanel_ShowValue(92, 18, g_GameDeviceData.coinPlayCount, RGBPanel_IMAGE_WWJ_STATE_YELLOW_NUM0, RGBPanel_NO_IMAGE); break; } } break; case 3: { switch (GMAIN_GameState) { case GAME_M_WWJ_STATE_COIN_IN: RGBPanel_ShowValue(102, 18, g_GameDeviceData.coinInCount, RGBPanel_IMAGE_WWJ_STATE_WHITE_NUM0, RGBPanel_NO_IMAGE); RGBPanel_ShowValue(144, 18, g_GameDeviceData.coinPlayCount, RGBPanel_IMAGE_WWJ_STATE_YELLOW_NUM0, RGBPanel_NO_IMAGE); break; } } break; case 4: { switch (GMAIN_GameState) { case GAME_M_WWJ_STATE_COIN_IN: case GAME_M_WWJ_STATE_COIN_OK: RGBPanel_ShowValue(168, 18, g_GameDeviceData.coinInCount, RGBPanel_IMAGE_WWJ_STATE_WHITE_NUM0, RGBPanel_NO_IMAGE); RGBPanel_ShowValue(208, 18, g_GameDeviceData.coinPlayCount, RGBPanel_IMAGE_WWJ_STATE_YELLOW_NUM0, RGBPanel_NO_IMAGE); break; } } break; default: break; } if (esp_mesh_is_root()) { GMAIN_ShowRootFlag(); } else if (g_wifi_rssi <= -60 || g_wifi_rssi == 0) { RGBPanel_ShowWifiRssi(g_wifi_rssi); } } // 6*6 static const uint8_t GMAIN_mesh_root_flag[] = { 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, }; #define COLOR_YELLOW 0xFFE0 #define GRADIENT_STEPS (128) // 渐变的步数 static uint16_t gradientProgress = 0; typedef struct { uint8_t colorR; uint8_t colorG; uint8_t colorB; } color_t; static color_t startColor = { 31, 31, 31}; static color_t endColor = { 31, 0, 0}; static uint8_t colorIndex = 0; const uint8_t colorIndexBuff[7] = { 0x7, 0x05, 0x01, 0x03, 0x02, 0x06, 0x04}; uint16_t getGradientColor(void) { // 计算每个通道的渐变步长 uint16_t r = startColor.colorR + (endColor.colorR - startColor.colorR) * gradientProgress / (GRADIENT_STEPS - 1); uint16_t g = startColor.colorG + (endColor.colorG - startColor.colorG) * gradientProgress / (GRADIENT_STEPS - 1); uint16_t b = startColor.colorB + (endColor.colorB - startColor.colorB) * gradientProgress / (GRADIENT_STEPS - 1); // 增加渐变进度 if (++gradientProgress == (GRADIENT_STEPS - 1)) { gradientProgress = 0; startColor = endColor; colorIndex = (colorIndex + 1) % 7; endColor.colorR = ((colorIndexBuff[colorIndex] & 0x04) > 0 ? 31 : 0); endColor.colorG = ((colorIndexBuff[colorIndex] & 0x02) > 0 ? 31 : 0); endColor.colorB = ((colorIndexBuff[colorIndex] & 0x01) > 0 ? 31 : 0); } uint16_t color = (r << 11) | (g << 5) | b; // 返回RGB565格式的颜色值 return color; } static void GMAIN_ShowRootFlag(void) { uint16_t color = getGradientColor(); int16_t startX = 58; startX += (g_config.matrix_chain - 1) * 64; for (uint8_t y = 0; y < 6; y++) { for (uint8_t x = 0; x < 6; x++) { RGBPanel_drawPixel(startX + x, y, (GMAIN_mesh_root_flag[x + y * 6] ? color : 0)); } } } void GMAIN_VideoEndCall(void) { switch (GMAIN_GameState) { case GAME_M_IDLE: break; case GAME_M_WIN: case GAME_M_WIN_ASSIST: case GAME_M_WWJ_STATE_COIN_IN: case GAME_M_WWJ_STATE_STOP: GMAIN_count_down(GAME_M_IDLE); JC24BDevice_SendLogoPlay(); LanWifiMesh_SendLogoPlay(); break; case GAME_M_SELF_TEST: break; } } void GMAIN_Init(void) { timer_init(&game_timer, 0); // 初始化为0秒 GMAIN_count_down(GAME_M_SELF_TEST); GMAIN_BootClickCount = 0; TIM_TIMING_SET_1S(GMAIN_BootTimeObj, 10000); } void GMAIN_count_down(uint8_t state) { // 状态切换前处理当前状态 switch (GMAIN_GameState) { case GAME_M_TIME: // 如果当前是倒计时状态,暂停计时器 if (timer_get_state(&game_timer) == TIMER_RUNNING) { timer_stop(&game_timer); } break; } GMAIN_GameState = state; // 更新状态 switch (GMAIN_GameState) { case 0: video_control_play(VIDEO_LOGO, VIDEO_PLAY_LOOP, 0); break; case 1: video_control_play(VIDEO_LOGO, VIDEO_PLAY_LOOP, 0); break; case 2: video_stop(); // 初始化倒计时器(如果未初始化) if (timer_get_state(&game_timer) == TIMER_STOPPED) { timer_init(&game_timer, g_GameDeviceData.settingtime_value); // 10秒倒计时 } // 启动倒计时 timer_start(&game_timer); // 初始化显示相关变量 static TickType_t last_update = 0; last_update = xTaskGetTickCount(); // 重置更新时间 break; case 3: timer_pause(&game_timer); break; case 4: video_control_play(VIDEO_LOGO, VIDEO_PLAY_LOOP, 0); break; case 5: video_control_play(VIDEO_LOGO, VIDEO_PLAY_LOOP, 0); break; case GAME_M_SELF_TEST: video_stop(); GMAIN_TestColorIndex = 0; TIM_TIMING_SET_1MS(GMAIN_TimeObj1, 0); break; } } void GMAIN_count_down_Run(void) { switch (GMAIN_GameState) { case 0: break; case 1: break; case 2: // 倒计时状态处理 // 获取剩余时间 int integer_seconds, centiseconds; timer_get_remaining_split(&game_timer, &integer_seconds, &centiseconds); TickType_t now = xTaskGetTickCount(); if (now - last_update >= pdMS_TO_TICKS(10)) { RGBPanel_FillScreen(0x0000); RGBPanel_ShowValue(0, 0, integer_seconds, RGBPanel_IMAGE_WWJ_STATE_RED_NUM0, RGBPanel_NO_IMAGE); RGBPanel_drawImageById(100, 0, RGBPanel_IMAGE_WWJ_STATE_RED_Point); RGBPanel_ShowValue(150, 0, centiseconds, RGBPanel_IMAGE_WWJ_STATE_RED_NUM0, RGBPanel_NO_IMAGE); RGBPanel_Update(); last_update = now; } // 检查是否超时 if (timer_is_expired(&game_timer)) { // 倒计时结束处理 GMAIN_count_down(GAME_M_WIN); // 切换到胜利状态 } break; case 3: break; case 4: break; case 5: break; case GAME_M_SELF_TEST: if (TIM_TIMING_RUN(GMAIN_TimeObj1)) { if (GMAIN_TestColorIndex >= 8) { JC24BDevice_SendLogoPlay(); LanWifiMesh_SendLogoPlay(); GMAIN_count_down(GAME_M_IDLE); return; } uint8_t r = 0; uint8_t g = 0; uint8_t b = 0; if ((GMAIN_TestColorIndex & 0x01) > 0) { r = 0xFF; } if ((GMAIN_TestColorIndex & 0x02) > 0) { g = 0xFF; } if ((GMAIN_TestColorIndex & 0x04) > 0) { b = 0xFF; } RGBPanel_FillScreenRGB888(r, g, b); RGBPanel_Update(); GMAIN_TestColorIndex++; TIM_TIMING_SET_100MS(GMAIN_TimeObj1, GMAIN_testTime); } break; } // if (IO_KeyPressed(KEY_BUTTON_TEST)) // { // GMAIN_count_down(GAME_M_WIN); // JC24BDevice_BroadcastWin(); // LanWifiMesh_BroadcastWin(); // } if (TIM_TIMING_RUN(GMAIN_BootTimeObj)) { TIM_TIMING_SET_1S(GMAIN_BootTimeObj, 10000); GMAIN_BootClickCount = 0; } if (IO_KeyPressed(KEY_BUTTON_BOOT) && g_config.meshType == CONFIG_MESH_TYPE_ROOT) { GMAIN_BootClickCount++; if (GMAIN_BootClickCount >= 5) { config_set_mesh_type(CONFIG_MESH_TYPE_CHILD); esp_restart(); } TIM_TIMING_SET_1S(GMAIN_BootTimeObj, 2); } } 我想让显示自适应位数变化,小数点前三位数后两位数,以固定位置显示在屏幕中,缺少的自动补零,现在代码的逻辑是会自动靠拢缺少的位数空间。单个数字宽度为45
07-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值