P8612 [蓝桥杯 2014 省 AB] 地宫取宝
https://www.luogu.com.cn/problem/P8612#submit
原始暴搜代码,没有记忆化,会tle
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
using namespace std;
typedef long long ll;
int n, m, k;
int mplst[60][60];
ll ans = 0;
ll mod = 1000000007;
bool vis[60][60] = { 0 };
int del[][2] = { {0,1},{1,0}};
ll dp[60][60];
void dfs(int x,int y,int max,int k_get)//(x,y),max:当前索取的礼物的最大值,k:当前拿了多少礼物
{
if(x==n-1 and y==m-1)
{
if (k_get == k)
ans++;
return;
}
if (k_get > k)return;
if (k_get + n - x + m - y -1< k)return;
for (int i = 0; i < 2; i++)
{
int dx = del[i][0], dy = del[i][1];
if(x+dx>=0 and x+dx<n and y+dy>=0 and y+dy<m)
{
if (!vis[x + dx][y + dy])
{
vis[x + dx][y + dy] = 1;
if (mplst[x + dx][y + dy] > max)
{
//get
dfs(x + dx, y + dy, mplst[x + dx][y + dy], k_get + 1);
//not get
dfs(x + dx, y + dy, max, k_get );
}
else
dfs(x + dx, y + dy, max, k_get);
vis[x + dx][y + dy] = 0;
}
}
}
}
int main()
{
cin >> n >> m >> k;
memset(mplst, -1, sizeof(mplst));
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> mplst[i][j];
dfs(0, 0, 0, 0);
memset(vis, 0, sizeof(vis));
dfs(0, 0, mplst[0][0], 1);
ans %= mod;
cout << ans;
return 0;
}