#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int dir[9][2]={
{
0,1},{
0,2},{
0,3},{
1,0},{
1,1},{
1,2},{
2,0},{
2,1},{
3,0}};//可以走的数组
int m,n;
int a[102][102];
int mark[102][102];
int ans = -999999;
void dfs(int x,int y,int s)
{
mark[x][y] = 1;//标记走过
if(s<ans)
return ;
if(x == n && y == m){
//终点
ans = s;
}
else{
for(int i=0; i<9; i++){
int xx = x+dir[i][0];
int yy = y+dir[i][1];
if(xx>=1 && xx<=n && yy>=1 && yy<=m && mark[xx][yy] == 0){
//满足条件内的
dfs(xx,yy,s+a[xx][yy]);
mark[xx][yy] = 0;//回溯
}
}
}
}
int main()
{
memset(mark,0,sizeof(mark));
cin >> n >> m;
for(int i=1;i<=n; i++){
for(int j=1; j<=m; j++){
cin>>a[i][j];
}
}
dfs(1,1,a[1][1]);
cout << ans <<endl;
}
蓝桥杯跳跃——dfs
猜你喜欢
转载自blog.csdn.net/weixin_44044395/article/details/115690687
今日推荐
周排行