ボックススタッキングの問題
この問題では、さまざまなボックスのセットが提供されます。長さ、幅、幅はボックスごとに異なる場合があります。私たちの仕事は、これらの箱の積み重ねを見つけることです。その高さは可能な限り高くなっています。好きなようにボックスを回転させることができます。ただし、維持するルールがあります。
下のボックスの上面の面積が上のボックスの下部の面積よりも大きい場合は、別のボックスにボックスを配置できます。
入力と出力
Input:
A list of boxes is given. Each box is denoted by (length, bredth, height).
{ (4, 6, 7), (1, 2, 3), (4, 5, 6), (10, 12, 32) }
Output:
The maximum possible height of box stack is: 60 アルゴリズム
maxHeight(boxList, n)
入力- さまざまなボックスのリスト、ボックスの数。
出力- ボックスを積み重ねて見つけた最大の高さ。
Begin define rotation array rot of size 3n. index := 0 for all boxes i, in the boxList, do rot[index].len := boxList[i].len rot[index].hei := maximum of boxList[i].hei and boxList[i].bre rot[index].bre := minimum of boxList[i].hei and boxList[i].bre index := index + 1 rot[index].len := boxList[i].bre rot[index].hei := maximum of boxList[i].len and boxList[i].hei rot[index].bre := minimum of boxList[i].len and boxList[i].hei index := index + 1 rot[index].len := boxList[i].hei rot[index].hei := maximum of boxList[i].len and boxList[i].bre rot[index].bre := minimum of boxList[i].len and boxList[i].bre index := index + 1 n := 3n sort the rot list define maxHeightTemp array for i := 1 to n-1, do for j := 0 to i-1, do if rot[i].bre < rot[j].bre AND rot[i].hei < rot[j].hei AND maxHeightTemp[i] < maxHeightTemp + rot[i].len, then maxHeightTemp[i] := maxHeightTemp[j] + rot[i].len done done maxHeight := -1 for i := 0 to n-1, do if maxHeight < maxHeightTemp[i], then maxHeight := maxHeightTemp[i] done done return maxHeight End
例
#include<iostream>
#include<algorithm>
using namespace std;
struct Box {
int length, bredth, height;
};
int min (int x, int y) {
return (x < y)? x : y;
}
int max (int x, int y) {
return (x > y)? x : y;
}
bool compare(Box b1, Box b2) {
return b1.height > b2.height; //to sort the box as descending order of height
}
int maxHeight( Box boxList[], int n ) {
Box rotation[3*n]; //a box can be rotared as 3 type, so there is 3n number of rotations
int index = 0;
for (int i = 0; i < n; i++) {
//store initial position of the box
rotation[index].length = boxList[i].length;
rotation[index].height = max(boxList[i].height, boxList[i].bredth);
rotation[index].bredth = min(boxList[i].height, boxList[i].bredth);
index++;
//dimention after first rotation
rotation[index].length = boxList[i].bredth;
rotation[index].height = max(boxList[i].length, boxList[i].height);
rotation[index].bredth = min(boxList[i].length, boxList[i].height);
index++;
//Dimention after second rotation
rotation[index].length = boxList[i].height;
rotation[index].height = max(boxList[i].length, boxList[i].bredth);
rotation[index].bredth = min(boxList[i].length, boxList[i].bredth);
index++;
}
n = 3*n; //set n as 3n for 3 rotations of each boxes
sort(rotation, rotation+n, compare); //sort rotation array as descending order
int maxHTemp[n]; //temporary max height if ith box is stacked
for (int i = 0; i < n; i++ )
maxHTemp[i] = rotation[i].length;
for (int i = 1; i < n; i++ ) //find optimized stack height
for (int j = 0; j < i; j++ )
if ( rotation[i].bredth < rotation[j].bredth && rotation[i].height < rotation[j].height
&& maxHTemp[i] < maxHTemp[j] + rotation[i].length) {
maxHTemp[i] = maxHTemp[j] + rotation[i].length;
}
int maxHeight = -1;
for ( int i = 0; i < n; i++ ) //find the maximum height from all temporary heights
if ( maxHeight < maxHTemp[i] )
maxHeight = maxHTemp[i];
return maxHeight;
}
int main() {
Box arr[] = { {4, 6, 7}, {1, 2, 3}, {4, 5, 6}, {10, 12, 32} };
int n = 4;
cout<<"The maximum possible height of box stack is: " << maxHeight (arr, n) << endl;
} 出力
The maximum possible height of box stack is: 60
-
蛇と梯子の問題
有名なゲーム「蛇と梯子」について知っています。このゲームでは、いくつかの部屋が部屋番号とともにボード上に存在します。一部の客室ははしごやヘビでつながっています。はしごを手に入れると、順番に移動することなく、目的地の近くに到達するためにいくつかの部屋に登ることができます。同様に、ヘビを捕まえると、下の部屋に送られ、その部屋から旅を再開します。 この問題では、開始から目的地に到達するために必要なサイコロの投げの最小数を見つける必要があります。 入力と出力 Input: The starting and ending location of the snake and ladders. Sn
-
頂点被覆問題
無向グラフの場合、頂点被覆は頂点のサブセットであり、グラフのすべてのエッジ(u、v)について、uまたはvのいずれかがセットに含まれます。 二分木を使用すると、頂点被覆問題を簡単に解決できます。 この問題は、2つのサブ問題に分けることができます。ルートが頂点被覆の一部である場合。この場合、ルートはすべての子エッジをカバーします。左右のサブツリーの頂点被覆のサイズを簡単に見つけて、ルートに1を追加できます。 入力と出力 Input: A binary tree. Output: The vertex cover is 3. アルゴリズム vertexCover(root node