Hdu1078 memory-based search
Question:
For n * n maps, each grid has a value that can only go from a small grid to a large grid. k can go up, down, and up to k steps each time to ask you about the maximum number of grids you have taken. what is
Dp [I] [j] indicates the maximum value that can be obtained starting from (I, j) and then deeply searches for the solution.
#include
#include
#include
using namespace std
;int n
,k
,dp
[110
][110
],cost
[110
][110
];int dir
[4
][2
]={0
,1
,0
,-1
,1
,0
,-1
,0
};int mark
[110
][110
];int max
(int a
,int b
){ return a
>b
?a
:b
;}int dfs
(int x
,int y
){ int i
,j
,sum
=0
; for(i
=1
;i
<=k
;i
++) { for(j
=0
;j
<4
;j
++) { int xx
=x
+dir
[j
][0
]*i
; int yy
=y
+dir
[j
][1
]*i
; if(xx
<0
||xx
>=n
||yy
<0
||yy
>=n
) continue; if(cost
[xx
][yy
]<=cost
[x
][y
]) continue; if(mark
[xx
][yy
]) { sum
=max
(sum
,dp
[xx
][yy
]); } else { mark
[xx
][yy
]=1
; sum
=max
(sum
,dfs
(xx
,yy
)); } } } dp
[x
][y
]=cost
[x
][y
]+sum
; return dp
[x
][y
]; }int main(){ int i
,j
; while(~scanf
("%d%d"
,&n
,&k
)) { if(n
<0
) break; for(i
=0
;i
<n
;i
++) for(j
=0
;j
<n
;j
++) { scanf
("%d"
,&cost
[i
][j
]); } memset
(dp
,0
,sizeof(dp
)); memset
(mark
,0
,sizeof(mark
)); mark
[0
][0
]=1
; printf
("%d\n"
,dfs
(0
,0
)); } return 0
;}