您的位置:首页 > 其它

Problem 18 & 67

2012-01-21 20:28 183 查看
Problem

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.

3
7 4

2 4 6

8 5 9 3
That is, 3 + 7 + 4 + 9 = 23.

Find the maximum total from top to bottom of the triangle below:

75

95 64

17 47 82

18 35 87 10

20 04 82 47 65

19 01 23 75 03 34

88 02 77 73 07 63 67

99 65 04 28 06 16 70 92

41 41 26 56 83 40 80 70 33

41 48 72 33 47 32 37 16 94 29

53 71 44 65 25 43 91 52 97 51 14

70 11 33 28 77 73 17 78 39 68 17 57

91 71 52 38 17 14 91 43 58 50 27 29 48

63 66 04 68 89 53 67 30 73 16 69 87 40 31

04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However,Problem 67, is the same challenge with a triangle
containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)

Code(懒得改回去了,这是问题67的方法,区别只是初始数组大小,将MAX_LINE改为15就成了。问题67的文本请点击这里下载)

#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

class Node
{
public:
Node(unsigned int num) :
m_lp(NULL),
m_rp(NULL),
m_lc(NULL),
m_rc(NULL),
m_sum(0),
m_num(num)
{
}

Node(Node* lp, Node* rp, unsigned int num)
{
m_lp = lp;
m_rp = rp;
m_num = num;
if (lp != NULL)
{
lp->m_rc = this;
}
if (rp != NULL)
{
rp->m_lc = this;
}

if (m_lp == NULL && m_rp == NULL)
{
m_sum = m_num;
}
else if (m_lp == NULL && m_rp != NULL)
{
m_sum = m_rp->m_sum + m_num;
}
else if (m_rp == NULL && m_lp != NULL)
{
m_sum = m_lp->m_sum + m_num;
}
if (m_lp!=NULL && m_rp!=NULL)
{
if (m_lp->m_sum > m_rp->m_sum)
{
m_sum = m_lp->m_sum + m_num;
}
else
{
m_sum = m_rp->m_sum + m_num;
}
}
}

void parent(Node* lp, Node* rp)
{
m_lp = lp;
m_rp = rp;
if (lp != NULL)
{
lp->m_rc = this;
}
if (rp != NULL)
{
rp->m_lc = this;
}
if (m_lp == NULL && m_rp == NULL)
{
m_sum = m_num;
}
else if (m_lp == NULL && m_rp != NULL)
{
m_sum = m_rp->m_sum + m_num;
}
else if (m_rp == NULL && m_lp != NULL)
{
m_sum = m_lp->m_sum + m_num;
}
if (m_lp!=NULL && m_rp!=NULL)
{
if (m_lp->m_sum > m_rp->m_sum)
{
m_sum = m_lp->m_sum + m_num;
}
else
{
m_sum = m_rp->m_sum + m_num;
}
}
}

~Node()
{
delete m_rc;
m_rc = NULL;
delete m_lc;
m_lc = NULL;
}

private:
Node(){}

public:
Node* m_lp;
Node* m_rp;
Node* m_lc;
Node* m_rc;
unsigned int m_num;
unsigned int m_sum;
};

int main()
{
const int MAX_LINE = 100;
const int BUFF_SIZE = (1+MAX_LINE)*MAX_LINE/2;
unsigned int buff[BUFF_SIZE];

int index = 0;
FILE* fp = fopen("triangle.txt", "r");
while (!feof(fp))
{
fscanf(fp, "%02d", &buff[index]);
index++;
}

index = 0;
vector<vector<Node*> > numstring;
for (int i = 0; i != MAX_LINE; i++)
{
vector<Node*> temp;
for (int j = 0; j != i+1; j++)
{
Node* node = new Node(buff[index]);
index++;
temp.push_back(node);
}
numstring.push_back(temp);
}

for (int i = 0; i != MAX_LINE; i++)
{
for (int j = 0; j != i+1; j++)
{
//cout << "i:" << i << ",j:" << j << endl;
Node* lp = NULL;
Node* rp = NULL;
if (i == 0)
{
numstring[i][j]->parent(lp, rp);
continue;
}
if (j != 0)
{
lp = numstring[i-1][j-1];
}
if (j < i)
{
rp = numstring[i-1][j];
}
numstring[i][j]->parent(lp, rp);
}
}

unsigned int max = 0;
for (int i = 0; i != MAX_LINE; i++)
{
if (numstring[MAX_LINE-1][i]->m_sum > max)
{
max = numstring[MAX_LINE-1][i]->m_sum;
}
//cout << numstring[MAX_LINE-1][i]->m_sum << endl;
}

cout << max << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  null delete numbers fp file