代码
#include <iostream>
#include <vector>
using namespace std;
int max = 0;
int f_max(int a, int b)
{
if (a < b)
return b;
return a;
}
void func(int (&arr)[5][5], int x, int y, int row, int col, int sum)
{
if (x < row && y < col)
sum += arr[x][y];
else
return;
if (x == row-1 && y == col-1)
{
if (sum > max)
max = sum;
return;
}
if (x < row)
func(arr, x + 1, y, row, col, sum);
if (y < col)
func(arr, x, y + 1, row, col, sum);
}
int main()
{
int row_ = 5, col_ = 5, t = 30;
//cin >> row_ >> col_ >> t;
//vector<vector<int>> arr(row_, vector<int>(col_));
//for (int i = 0; i < row_; ++i)
//{
// for (int j = 0; j < col_; ++j)
// {
// cin >> arr[i][j];
// }
//}
//for (int i = 0; i < row_; ++i)
//{
// for (int j = 0; j < col_; ++j)
// {
// cout << arr[i][j];
// }
//}
vector<vector<int>> tmp(row_, vector<int>(col_));
int arr[5][5] = {
{ 3,5,4,2,3 },
{ 4,5,3,4,3 },
{ 4,3,5,3,2 },
{ 2,5,3,3,5 },
{ 5,3,4,4,1 },
};
int sum = 0;
for (int i = 1; i < row_+1; ++i)
{
for (int j = 1; j < col_+1; ++j)
{
func(arr, 0, 0, i, j, sum);
tmp[i-1][j-1] = max;
cout << max << " ";
max = 0;
}
cout << endl;
}
cout << endl;
vector<vector<int>> min_(row_, vector<int>(col_));
min_[row_ - 1][col_ - 1] = t - arr[row_ - 1][col_ -1];
for (int i = row_ - 1; i >= 0; --i)
{
for (int j = col_ - 1; j >= 0; --j)
{
if (i == row_ - 1 && j == col_ - 1)
continue;
if (i == row_ - 1 && j < col_ - 1)
min_[i][j] = min_[i][j + 1] - arr[i][j];
else if (i < row_ - 1 && j == col_ - 1)
min_[i][j] = min_[i + 1][j] - arr[i][j];
else
min_[i][j] = f_max(min_[i + 1][j], min_[i][j + 1]) - arr[i][j];
}
cout << endl;
}
for (int i = 0; i < row_; ++i)
{
for (int j = 0; j < col_; ++j)
cout << min_[i][j] + arr[i][j] - tmp[i][j] << " ";
cout << endl;
}
cout << max << endl;
system("pause");
return 0;
}
结果
全是负数说明,没有游玩路径
非负数中,找最小的,然后用目标值t,减去这个数就是最优的游玩时间。