BlockStructure

块结构覆盖
本文介绍了一种算法,旨在使用最少数量的非交叠矩形来精确复现由一系列垂直方块组成的特定结构。该算法考虑了不同高度方块的排列,并讨论了如何通过矩形覆盖这些结构的具体实现。

/*Problem Statement

A group of vertical blocks are placed densely one after another on the ground. The blocks each have a width of 1, but their heights may vary.

For example, if the heights of the vertical blocks are given as {1,5,5,1,6,1}, the configuration would look like the following picture:


    ×
 ×× ×
 ×× ×
 ×× ×
 ×× ×
××××××

Your task is to reproduce the exact shape of this structure using some number of non-intersecting rectangles.

You will be given a int[] heights representing the heights of the vertical blocks from left to right.

Return the minimal number of rectangles necessary to perform this task with the given configuration of blocks.

Definition
Class: BlockStructure
Method: cover
Parameters: int[]
Returns: int
Method signature: int cover(int[] heights)
(be sure your method is public)

Constraints
- heights will have between 1 and 50 elements, inclusive.
- Each element of heights will be between 1 and 1000, inclusive.
Examples
0) {1,5,5,1,6,1} Returns: 3
We can use rectangles with sizes 6x1, 2x4 and 1x5.

    ×
 ×× ×
 ×× ×
 ×× ×
 ×× ×
××××××

1) {2,2,2,4,4} Returns: 2
We can use a 3x2 rectangle and a 2x4 rectangle.

   ××
   ××
×××××
×××××

2) {6,6,6,6,6,6} Returns: 1
The structure is a rectangle.

××××××
××××××
××××××
××××××
××××××
××××××

3){71,44,95,16,10,80,12,17,98,61} Returns: 10
It's impossible to use less than 10 rectangles.

4){100,100,97,100,100,100,97,98,99,99}
Returns: 5

This problem statement is the exclusive and proprietary property of TopCoder, Inc.
Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited.
(c)2003, TopCoder, Inc. All rights reserved.*/

package blockStructure;

import java.util.ArrayList;

public class BlockStructure {

public static int cover(int[] heights) {

int[][] records = markRecords(heights);

int result = 0;

int i = 0;
int j = 0;
while(i < records.length && j < records[i].length) {
if(records[i][j] == 1) {
int start_i = i;
int start_j = j;
while(j < records[i].length && records[i][j] == 1) j++;
j--;

while(i < records.length && rangeExist(records, i, start_j, j)) i++;
i--;

result++;
records = removeRecords(records, start_i, start_j, i, j);
i = j = 0;
} else if(records[i][j] == 0) {
if(j < records[i].length - 1) j++;
else {
j = 0;
i++;
}
}
}

return result;
}

//private static int result = 0;

public static int cover_2(int[] heights) {
int result = 0;

if(heights.length == 1) return 1;

deduct(heights);

result++;

if(isEmpty(heights)) return result;

ArrayList zeroList = zeroPos(heights);

int i = 0;
int temp = -1;
while(i < zeroList.size()) {
int position = ((Integer)zeroList.get(i)).intValue();
if(position == 0 || position - temp == 1) {
temp = position;
}
else {
int[] subtree = new int[position - temp - 1];
for(int j = temp + 1; j < position; j++){
subtree[j - temp - 1] = heights[j];
}
result += cover_2(subtree);

temp = position;

}
i++;
}

int position = ((Integer)zeroList.get(zeroList.size() - 1)).intValue();
if(position != heights.length - 1) {
int[] subtree = new int[heights.length - position - 1];
for(int j = position + 1; j < heights.length; j++){
subtree[j - position - 1] = heights[j];
}
result += cover_2(subtree);
}

return result;
}

private static boolean isEmpty(int[] array) {
for(int k = 0; k < array.length; k++) {
if(array[k] != 0) return false;
}

return true;
}

private static ArrayList zeroPos(int[] array) {
ArrayList list = new ArrayList();

for(int i = 0; i < array.length; i++) {
if(array[i] == 0) list.add(i);
}

return list;
}

private static int maxium(int[] array) {
int maxium = 0;
for(int i : array) {
if(i > maxium) maxium = i;
}

return maxium;
}

private static void deduct(int[] array) {
int min = minum(array);

for(int i = 0; i < array.length; i++) {
array[i] = array[i] - min;
}
}

private static int minum(int[] array) {
int min = Integer.MAX_VALUE;
for(int i : array) {
if(i < min) min = i;
}

return min;
}

private static int[][] markRecords(int[] array) {
int largestNum = maxium(array);

int[][] result = new int[largestNum][array.length];

for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
try{
if(array[j] > i ) result[i][j] = 1;
else result[i][j] = 0;
}catch(Exception e){
e.printStackTrace();
}
}
}

return result;
}

private static int[][] removeRecords(int[][] records, int i, int j, int x, int y) {
for(int k = i; k <= x; k++) {
for(int m = j; m <= y; m++) {
records[k][m] = 0;
}
}
return records;
}

private static boolean rangeExist(int[][] records, int i, int start_j, int j) {
for(int k = start_j; k <= j; k++) {
if(records[i][k] == 0) return false;
}

return true;
}

}

【无人机】基于改进粒子群算法的无人机路径规划研究[和遗传算法、粒子群算法进行比较](Matlab代码实现)内容概要:本文围绕基于改进粒子群算法的无人机路径规划展开研究,重点探讨了在复杂环境中利用改进粒子群算法(PSO)实现无人机三维路径规划的方法,并将其与遗传算法(GA)、标准粒子群算法等传统优化算法进行对比分析。研究内容涵盖路径规划的多目标优化、避障策略、航路点约束以及算法收敛性和寻优能力的评估,所有实验均通过Matlab代码实现,提供了完整的仿真验证流程。文章还提到了多种智能优化算法在无人机路径规划中的应用比较,突出了改进PSO在收敛速度和全局寻优方面的优势。; 适合人群:具备一定Matlab编程基础和优化算法知识的研究生、科研人员及从事无人机路径规划、智能优化算法研究的相关技术人员。; 使用场景及目标:①用于无人机在复杂地形或动态环境下的三维路径规划仿真研究;②比较不同智能优化算法(如PSO、GA、蚁群算法、RRT等)在路径规划中的性能差异;③为多目标优化问题提供算法选型和改进思路。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注算法的参数设置、适应度函数设计及路径约束处理方式,同时可参考文中提到的多种算法对比思路,拓展到其他智能优化算法的研究与改进中。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值