65,210
社区成员
发帖
与我相关
我的任务
分享#include <stdio.h>
int frog( int N, int a, int b, int start, int k, int *insects, int** result )
{
int max = 0, tmp = 0;
int i;
if( start >= N || k < 0 ) {
return 0;
}
if( result[start][k] >= 0 ) {
return result[start][k];
}
for( i = a; i <= b; i ++ ) {
if( k > 0 ) {
tmp = frog( N, a, b, start + i, k - 1, insects, result );
if( tmp > max ) {
max = tmp;
}
}
}
max += insects[start];
if( result[start][k] < max ) {
result[start][k] = max;
}
return result[start][k];
}
int** allocMatrix( int row, int column )
{
int i, j;
int ** r = (int**)malloc( row * sizeof( int* ) );
for( i = 0; i < row; i++ ) {
r[i] = (int*) malloc( column * sizeof( int* ) );
for( j = 0; j < column; j++ ) {
r[i][j] = -1;
}
}
return r;
}
int main( int args, char** argv )
{
int N = 140;
int k = 100;
int a = 2;
int b = 5;
int insects[140];
int i = 0;
int **result = allocMatrix( N, k + 1 );
for( i = 0; i < N; i++ ) {
insects[i] = i + 1;
}
int r = frog( N, a, b, 0, k, insects, result );
printf( "result=%d\n", result[0][k] );
}
// FrogGame.cpp : Defines the entry point for the console application.
//
/*
* frog问题
描述:
一只小青蛙在一个一维路径上抓虫子吃,一共有N格,每次能跳T步(a <=T <=b),
跳了K次后就会痿掉,跳不动了,游戏结束。每跳到一个格子上就能把那个格子上的虫子吃完,
一开始青蛙在第一格上(上面的虫子(如果有的话)就自然被吃掉了)
输入:
第一行需要用户输入N, A, B(1 <= A <= B <= N), K (K > = 1).
第二行需要用户输入每一格的虫子数量
输出:
程序输出这种状态下能吃的虫子最大数目
*/
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <iostream>
#include <memory>
#include <assert.h>
using namespace std;
class Path
{
public:
Path(const int n, const int* c)
: N_(n)
{
C_ = new int[N_];
memcpy(C_, c, N_*sizeof(int));
}
virtual ~Path()
{
delete[] C_;
}
int at(const int idx)
{
assert(idx < N_);
return C_[idx];
}
int findMax(const int from, const int to)
{
assert(from >= 0);
assert(from <= to);
assert(to < N_);
int result = from;
for (int i=from+1; i<=to; ++i)
{
if (C_[result] < C_[i])
result = i;
}
return result;
}
int length()
{
return N_;
}
void clean(const int idx)
{
assert(idx < N_);
C_[idx] = 0;
}
void print()
{
for (int i=0; i<N_; ++i)
{
cout << C_[i] << " ";
}
cout << endl;
}
private:
const int N_; // 一维路径的格数
int* C_; // 每一格的虫子数量
};
class Frog
{
public:
Frog(const int k, const int a, const int b)
: K_(k), A_(a), B_(b)
{
}
virtual ~Frog()
{
}
int go(Path& path)
{
int nEated = path.at(0);
path.clean(0);
path.print();
int idx;
if (B_ > path.length() - 1)
{
idx = path.findMax(A_, path.length() - 1);
}
else
{
idx = path.findMax(A_, B_);
}
nEated += path.at(idx);
path.clean(idx);
path.print();
int k = 1;
while (k < K_)
{
int ll = idx - B_;
int lr = idx - A_;
int rl = idx + A_;
int rr = idx + B_;
if (ll < 0)
{
ll = 0;
if (lr < 0)
{
lr = 0;
}
}
if (rr > path.length()-1)
{
rr = path.length()-1;
if (rl > path.length()-1)
{
rl = path.length()-1;
}
}
int left = path.findMax(ll, lr);
int right = path.findMax(rl, rr);
if (path.at(left) > path.at(right))
idx = left;
else
idx = right;
nEated += path.at(idx);
path.clean(idx);
path.print();
k++;
}
return nEated;
}
private:
const int K_; // 跳了K次后就会痿掉
const int A_; // 每次能跳T步(a <=T <=b)
const int B_; // 每次能跳T步(a <=T <=b)
};
int main(int argc, char* argv[])
{
while (1)
{
cout << "Frog游戏" << endl;
cout << "输入" << endl;
cout << "第一行需要用户输入N(N <= 100), A, B(1<=A<=B<=N), K(K > = 1)" << endl;
int N, A, B, K;
cin >> N >> A >> B >> K;
if (A < 1 || A > B || B > N || K < 1 || N > 100)
{
cerr << "输入错误" << endl;
return 1;
}
cout << "第二行需要用户输入每一格的虫子数量" << endl;
int* C = new int[N];
for (int i = 0; i < N; ++i)
cin >> C[i];
cout << "输出" << endl;
Path path(N, C);
Frog frog(K, A, B);
cout << frog.go(path) << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int curr_max;
int *data;
int len;
int min_step;
int max_step;
int *step;
int n_steps;
void TraceBack(int left_steps,int place,int curr)
{
int t = data[place];
curr += t;
data[place] = 0;
if (left_steps)
{
for(int i=0;place + step[i] < 0;++i)
;
for (; i < n_steps && place + step[i] < len; ++i)
{
if(data[place + step[i]]>0) // 如过要考虑重复跳,就把这行注释掉
TraceBack(left_steps-1,place + step[i],curr);
}
}
else
{
if (curr > curr_max) curr_max = curr;
}
data[place] = t;
}
int main()
{
int k;
cin >> len >> min_step >> max_step >> k;
n_steps=(max_step-min_step+1)*2;
data = new int [len];
step = new int [n_steps];
for (int i = 0; i < len; ++i)
cin >> data[i];
for(i=min_step;i<=max_step;i++)
{
step[n_steps/2+(i-min_step)]=i;
step[n_steps/2-(i-min_step)-1]=-i;
}
curr_max = -1;
TraceBack(k,0,0);
cout << curr_max << endl;
return 0;
}