标签:
3 1 1 2 5 10 11 6 12 12 7 -1 -1
37
#include <iostream> #include<cstring> #include<algorithm> #include<cstdio> using namespace std; int f[101][101],a[101][101]; int n,k,i,j; int dr[4][2]={{1,0},{0,1},{-1,0},{0,-1} }; int dfs(int x,int y) { int ans=0; if(!f[x][y]) { for(int i=0;i<4;i++) for(int j=1;j<=k;j++) { int xx=x+dr[i][0]*j; int yy=y+dr[i][1]*j; if (xx<1 || xx>n || yy<1 || yy>n) continue; if (a[xx][yy]<=a[x][y]) continue; // ans=max(ans,dfs(xx,yy)); f[x][y]=max(f[x][y],dfs(xx,yy)); } f[x][y]+=a[x][y]; // f[x][y]=ans+a[x][y]; } return f[x][y]; } int main() { while(scanf("%d%d",&n,&k) && n!=-1) { for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%d",&a[i][j]); memset(f,0,sizeof(f)); //ans=0; printf("%d\n",dfs(1,1)); } return 0; }
hdu 1078 FatMouse and Cheese(记忆化搜索)
标签:
原文地址:http://www.cnblogs.com/stepping/p/5648439.html