其他分享
首页 > 其他分享> > HDU - 1078 FatMouse and Cheese

HDU - 1078 FatMouse and Cheese

作者:互联网

传送门

记忆化搜索

#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<utility>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cctype>
using namespace std;
 
typedef long long LL;           //数据太大了记得用LL,还有把INF也得换
typedef pair<int, int> P;
 
const int maxn = 1e7+10;        //cf数组最多差不多是这么多,1e8就会段错误
const int V_MAX = 800 + 10;
const int mod = 10007;
const int INF = 0x3f3f3f3f;     //如果数据范围为long long,记得把INF的值换了
const double eps = 1e-6;        //eps开太小二分容易死循环,所以直接来个100次循环就好了


                                //多组输入时,maxn太大,用memset()会超时,血的教训;

int n, m;
int grid[1000][1000];
int dp[1000][1000];

int dfs(int x, int y) {
    if(dp[x][y] > 0) return dp[x][y];
    int res = grid[x][y];
    for(int i = 1; i <= m; i++) {
        if(x + i <= n && grid[x+i][y] > grid[x][y]) res = max(dfs(x+i, y) + grid[x][y], res);
        if(x - i >= 1 && grid[x-i][y] > grid[x][y]) res = max(dfs(x-i, y) + grid[x][y], res);
        if(y + i <= n && grid[x][y+i] > grid[x][y]) res = max(dfs(x, y+i) + grid[x][y], res);
        if(y - i >= 1 && grid[x][y-i] > grid[x][y]) res = max(dfs(x, y-i) + grid[x][y], res);
    }
    return dp[x][y] = res;
}

void solve() {
    for(int i = 1; i <= n; i++) 
        for(int j = 1; j <= n; j++) 
            cin >> grid[i][j];
    memset(dp, 0, sizeof dp);
    dfs(1, 1);
    cout << dp[1][1] << endl;
}

int main()
{
    ios::sync_with_stdio(false);           //关了流同步就别用c的输入
    //freopen("D:\\in.txt", "r", stdin);
    while(cin >> n >> m && n != -1) {
        solve();
    }
    return 0;
}

标签:Cheese,HDU,int,res,FatMouse,grid,dfs,include,dp
来源: https://blog.csdn.net/TURNINING/article/details/115014116