标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5701 Accepted Submission(s): 2320
#include <cstdio> #include <cstring> #include <iostream> #include <cstdlib> #include <algorithm> using namespace std ; int dir[][2] = {{-1,0},{0,-1},{1,0},{0,1}} ; int n, k ; int mat[120][120] ; int dp[120][120] ; void _in() { for(int i = 0 ; i < n; ++i) for(int j = 0; j < n ;++j) cin >> mat[i][j] ; } int getans(int i, int j) { int& res = dp[i][j] ; if(res != -1) return res ; res = 0 ; for(int x = 1; x <= k ;++x) for(int y = 0; y < 4; ++y) { int ti = i + dir[y][0] * x ; int tj = j + dir[y][1] * x ; if(ti < 0 || ti >= n || tj < 0 || tj >= n) continue ; if(mat[ti][tj] <= mat[i][j]) continue ; res = max(res, getans(ti, tj) + mat[ti][tj]) ; } return res ; } int main() { //freopen("in.txt","r",stdin) ; ios::sync_with_stdio(0) ; while(cin >> n >> k) { _in() ; memset(dp, -1, sizeof dp) ; if(n == -1 && k == -1) break ; cout << getans(0, 0) + mat[0][0] << endl ; } return 0 ; }
标签:
原文地址:http://www.cnblogs.com/orchidzjl/p/4504624.html