HDU - 1078 FatMouse and Cheese

input:

3 1
1 2 5
10 11 6
12 12 7
-1 -1v

  

output:

37

  

题目大意:

一个n×n的矩阵,老鼠每次只能吃比上大的食物所在的区域,每次老鼠只能垂直或者水平移动[1,k]步,求老鼠最
多能吃多少食物。

  

分析:

dp+dfs。跟滑雪有点类似,dp[i][j]=(i,j)位置位置最多能吃多少食物。利用dfs查找那些dp[i][j]==0的区域,
比较四个方向上,并取最大值赋给dp[i][j]。

  

code:

#define frp

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f;
const ll inf = 0x7fffff;
const int maxn = 1000;
const int MAXN = 1050;
int n, k;
int dp[MAXN][MAXN], a[MAXN][MAXN];
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};

bool inside(int x, int y) {
    return x >= 1 && x <= n && y >= 1 && y <= n;
}

int dfs(int i, int j) {
    if (dp[i][j])return dp[i][j];
    int ans = 0;
    for (int l = 0; l < 4; ++l) {
        for (int m = 1; m <= k; m++) {
            int x = i + dir[l][0] * m;
            int y = j + dir[l][1] * m;
            if (inside(x, y) && a[i][j] < a[x][y]) {
                ans = max(ans, dfs(x, y));
            }
        }
    }
//    cout<<ans<<endl;
    return dp[i][j] = ans + a[i][j];
}

void solve() {
    while (cin >> n >> k&&n!=-1&&k!=-1) {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                cin>>a[i+1][j+1];
//                cout<<a[i+1][j+1]<<" ";
            }
//            cout<<endl;
        }
        int ans=-1;
        cout<<dfs(1,1)<<endl;
        memset(dp,0, sizeof(dp));
//        cout << n << " " << k << endl;
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifdef frp
    freopen("D:\\coding\\c_coding\\in.txt", "r", stdin);
//    freopen("D:\\coding\\c_coding\\out.txt", "w", stdout);
#endif
    solve();
    return 0;
}

  

posted @ 2018-09-30 07:48  visualVK  阅读(119)  评论(0编辑  收藏  举报