64,642
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <iomanip>
#include <vector>
#include <list>
using namespace std;
const int cell_num = 10;
int get_max_path(int cell[cell_num][cell_num], int x, int y, list<pair<int, int>>& res)
{
res.push_back(make_pair<int, int>(x, y));
if (x == cell_num - 1 && y == cell_num - 1)
return cell[x][y];
if (x == cell_num - 1)
return cell[x][y] + get_max_path(cell, x, y + 1, res);
if (y == cell_num - 1)
return cell[x][y] + get_max_path(cell, x + 1, y, res);
size_t pos1 = res.size();
int p1 = cell[x][y] + get_max_path(cell, x + 1, y, res);
size_t pos2 = res.size();
int p2 = cell[x][y] + get_max_path(cell, x, y + 1, res);
list<pair<int, int>>::iterator i1 = res.begin();
list<pair<int, int>>::iterator i2 = res.begin();
if (p1 > p2)
{
advance(i1, pos2);
i2 = res.end();
}
else
{
advance(i1, pos1);
advance(i2, pos2);
}
res.erase(i1, i2);
return max(p1, p2);
}
int main()
{
int cell[cell_num][cell_num];
for (int i = 0; i < cell_num; ++i)
{
for (int j = 0; j < cell_num; ++j)
{
cell[i][j] = rand() % 100;
cout<<setw(5)<<cell[i][j];
}
cout<<endl;
}
cout<<endl;
list<pair<int, int>> res;
int max_path = get_max_path(cell, 0, 0, res);
cout<<"the max value is : "<<max_path<<endl;
cout<<"the path : "<<endl;
pair<int, int> last_pos = *res.begin();
for (list<pair<int, int>>::iterator i = res.begin();
i != res.end(); ++i)
{
pair<int, int> pos = (*i);
if (pos.first > last_pos.first)
{
cout<<endl;
for (int i = 0; i < pos.second; ++i)
cout<<" ";
}
else
{
for (int i = last_pos.second + 1; i < pos.second; ++i)
cout<<" ";
}
cout<<setw(5)<<cell[pos.first][pos.second];
last_pos = pos;
}
return 0;
}
-------
41 67 34 0 69 24 78 58 62 64
5 45 81 27 61 91 95 42 27 36
91 4 2 53 92 82 21 16 18 95
47 26 71 38 69 12 67 99 35 94
3 11 22 33 73 64 41 11 53 68
47 44 62 57 37 59 23 41 29 78
16 35 90 42 88 6 40 42 64 48
46 5 90 29 70 50 6 1 93 48
29 23 84 54 56 40 66 76 31 8
44 39 26 23 37 38 18 82 29 41
the max value is : 1141
the path :
41 67
45 81 27 61
92
69
73
37
88
70
56 40 66 76
82 29 41
#include <iostream>
#include <iomanip>
#include <vector>
#include <list>
using namespace std;
const int cell_num = 10;
int get_max_path(int cell[cell_num][cell_num], int x, int y, list<pair<int, int>>& res)
{
res.push_back(make_pair<int, int>(x, y));
if (x == cell_num - 1 && y == cell_num - 1)
return cell[x][y];
if (x == cell_num - 1)
return cell[x][y] + get_max_path(cell, x, y + 1, res);
if (y == cell_num - 1)
return cell[x][y] + get_max_path(cell, x + 1, y, res);
size_t pos1 = res.size();
int p1 = cell[x][y] + get_max_path(cell, x + 1, y, res);
size_t pos2 = res.size();
int p2 = cell[x][y] + get_max_path(cell, x, y + 1, res);
list<pair<int, int>>::iterator i1 = res.begin();
list<pair<int, int>>::iterator i2 = res.begin();
if (p1 > p2)
{
advance(i1, pos2);
i2 = res.end();
}
else
{
advance(i1, pos1);
advance(i2, pos2);
}
res.erase(i1, i2);
return max(p1, p2);
}
int main()
{
int cell[cell_num][cell_num];
for (int i = 0; i < cell_num; ++i)
{
for (int j = 0; j < cell_num; ++j)
{
cell[i][j] = rand() % 100;
cout<<setw(5)<<cell[i][j];
}
cout<<endl;
}
cout<<endl;
list<pair<int, int>> res;
int max_path = get_max_path(cell, 0, 0, res);
cout<<"the max value is : "<<max_path<<endl;
cout<<"the path : "<<endl;
for (list<pair<int, int>>::iterator i = res.begin();
i != res.end(); ++i)
cout<<(*i).first<<", "<<(*i).second<<endl;
return 0;
}
------------
41 67 34 0 69 24 78 58 62 64
5 45 81 27 61 91 95 42 27 36
91 4 2 53 92 82 21 16 18 95
47 26 71 38 69 12 67 99 35 94
3 11 22 33 73 64 41 11 53 68
47 44 62 57 37 59 23 41 29 78
16 35 90 42 88 6 40 42 64 48
46 5 90 29 70 50 6 1 93 48
29 23 84 54 56 40 66 76 31 8
44 39 26 23 37 38 18 82 29 41
the max value is : 1141
the path :
0, 0
0, 1
1, 1
1, 2
1, 3
1, 4
2, 4
3, 4
4, 4
5, 4
6, 4
7, 4
8, 4
8, 5
8, 6
8, 7
9, 7
9, 8
9, 9