1, from gird[0][0], each direction of the search, each step of the search for a bit
for(i=0; i<4; i++){ for(j=1; j<=k; J + +) { inttx=x+d[i][0]*J; intty=y+d[i][1]*J; if(tx>=0&&tx<n&&ty>=0&&ty<n&&grid[x][y]<Grid[tx][ty]) { inttemp=Memsearch (tx,ty); if(max<temp) max=temp; } }}
2, temp constantly updated four directions and each step to go how far optimal value
#include <cstdio>#include<string.h>using namespacestd;intN///Grid SizeintK///maximum number of steps to move at a timeintgrid[ the][ the];///Cheeseintcheese[ the][ the];///Memory Search///directionintd[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};///Memory SearchintMemsearch (intXinty) { inti,j; intmax=0; if(cheese[x][y]>0)returnCheese[x][y]; for(i=0; i<4; i++) { for(j=1; j<=k; J + +) { inttx=x+d[i][0]*J; intty=y+d[i][1]*J; if(tx>=0&&tx<n&&ty>=0&&ty<n&&grid[x][y]<Grid[tx][ty]) { inttemp=Memsearch (tx,ty); if(max<temp) max=temp; } } } returncheese[x][y]=max+grid[x][y];}intMain () { while(SCANF ("%d%d", &n,&k) &&n!=-1&&k!=-1) {memset (cheese,0,sizeof(cheese)); for(intI=0; i<n; i++) { for(intj=0; j<n; J + +) scanf ("%d",&Grid[i][j]); } printf ("%d\n", Memsearch (0,0)); } return 0;}
Memory Search, Fatmouse and Cheese