急求一个编程题:10X10的格子,每个格子中随机生成一些数字,一只鸡从第一格开始只能走右边或下边的格子,怎么走,各个格子中的数字加起来最大

happy2007_ 2008-12-16 10:41:59
如题所示,大概的题意就是这些,希望那位高手能给予指点。急啊!
...全文
350 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
星羽 2008-12-17
  • 打赏
  • 举报
回复
如果想每次运行随机出不同的数
那么加上

#include <ctime>

还在在main里加一句

int main()
{
srand(time(0));


。。。。

。。。。
星羽 2008-12-17
  • 打赏
  • 举报
回复
这样打印好像更直观:)


#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
星羽 2008-12-17
  • 打赏
  • 举报
回复

#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

toadzw 2008-12-17
  • 打赏
  • 举报
回复

#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

绿色夹克衫 2008-12-17
  • 打赏
  • 举报
回复
递推或递归都可以吧!
f(row,column) = max(f(row - 1,column),f(row,column - 1)) + cell[row,column]
taodm 2008-12-17
  • 打赏
  • 举报
回复
穷举法!深度遍历代码比较简单。
wangyaosuper 2008-12-17
  • 打赏
  • 举报
回复
最简单的动态规划问题。
sparkliang 2008-12-17
  • 打赏
  • 举报
回复
标准的动态规划问题。
funnybunny 2008-12-16
  • 打赏
  • 举报
回复
嗯~刚漱口的时候想了一下,应该抽象为有向图,把每个格子当成图的交点,格子的数值就是从上一点到该点的边权值,
然后用那个单源最短(长)路径的算法求解,最后的出口就是所求的那个点吧...
突然的想法,可能不对...继续洗脸去~
funnybunny 2008-12-16
  • 打赏
  • 举报
回复
有那么2两意思~顶上!
回头再来看~

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧