At the beginning of this question, I did not see that the number of peanuts planted at each point was different, so I considered the dfs and found that the number of peanuts in the question was different. . . . . . . . You can just sort it directly;
but dfs may get stuck to o(n!), but this question is not stuck; the
meaning of the question: every time you go to find the largest number of peanuts to pick, pay attention to every time you go A grid and a summary are 2 units of time, so pay attention to the time;
dfs idea: find out all MAX, and then search all situations, every time dfs comes in, it shows that this point is reachable and there is remaining time to go back;
AC code :
#include<bits/stdc++.h>
using namespace std;
struct Node{
int x,y;
int val;
Node(int xx,int yy,int v){
x=xx;y=yy;val=v;
}
};
vector<Node> V;
int m,n,k;
int ans=0;
int book[30][30];//标记数组
void dfs(int xx,int yy,int t,int sum){
int MM=0;
for(int i=0;i<V.size();i++){
//找最大值出来
if(!book[V[i].y][V[i].x]){
if(V[i].val>MM){
MM=V[i].val;
}
}
}
int index[40];
int cnt=0;
for(int i=0;i<V.size();i++){
if(V[i].val==MM&&!book[V[i].y][V[i].x]){
//找出所有最大的值点
index[cnt++]=i;
}
}
for(int i=0;i<cnt;i++){
int x=V[index[i]].x,y=V[index[i]].y;
if(k-t-(abs(y-yy)+abs(x-xx)+1)>=y+1){
book[y][x]=1;
dfs(x,y,t+abs(y-yy)+abs(x-xx)+1,sum+V[index[i]].val);
book[y][x]=0;
}
}
//能执行到这里说明不能走了
ans=max(ans,sum);//取结果最大值
}
int main(){
scanf("%d %d %d",&m,&n,&k);
int Map[20][20];
int x[25],y[25];
int Max=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
scanf("%d",&Map[i][j]);
Max=max(Map[i][j],Max);
if(Map[i][j])V.push_back(Node(j,i,Map[i][j]));
}
}
int num=0;
for(int i=0;i<V.size();i++){
if(V[i].val==Max){
x[num]=V[i].x;
y[num++]=V[i].y;
}
}
for(int i=0;i<num;i++){
if(k-y[i]-2>=y[i]+1)//下一个点可以走,并且有足够的时间回去
{
book[y[i]][x[i]]=1;
int sum=Map[y[i]][x[i]];
dfs(x[i],y[i],y[i]+2,sum);
book[y[i]][x[i]]=0;
}
}
printf("%d\n",ans);
return 0;
}