Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 93221 | Accepted: 35289 |
Description
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
Input
Output
Sample Input
5 5 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
Sample Output
25
解题思路:这道题用的是记忆化搜索的思想,也就是 搜索 + 动态规划,把每次搜索的结果都存起来,等用到的时候就调用数组里面的值,所以时间效率要比普通的搜索高很多。
ps:动态规划是由下而上的,通俗的说就是先求出一个小状态的最优解,通过这个小状态的最优解一步一步的求出整个状态的最优解,而记忆化搜索是先求整个状态的最优解,在求这个最优解的过程中一定会用到小状态的最优解,而求这个小状态的最优解时,就是一步步递归的过程。只是我自己理解他们的区别, 不足之处请指出
大神对记忆化搜索的解释:http://blog.youkuaiyun.com/urecvbnkuhbh_54245df/article/details/5847876
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int direction[4][2] = {0,-1,-1,0,1,0,0,1};
int map[105][105];
int dp[105][105];
int row,col;
int DFS(int map_x,int map_y)
{
if(dp[map_x][map_y] != -1){
return dp[map_x][map_y];
}
int cnt_l=1;
for(int i=0; i<4; i++){
int re_mx = map_x + direction[i][0];
int re_my = map_y + direction[i][1];
if(re_mx >= row || re_my >= col)
continue;
else if(re_mx < 0 || re_my < 0)
continue;
else{
if(map[re_mx][re_my] > map[map_x][map_y]){
cnt_l = max(DFS(re_mx,re_my)+1,cnt_l);
}
}
}
dp[map_x][map_y] = cnt_l;
return cnt_l;
}
int main()
{
while(~scanf("%d%d",&row,&col)){
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
scanf("%d",&map[i][j]);
}
}
int long_m=0;
memset(dp,-1,sizeof(dp));
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
long_m = max(long_m,DFS(i,j));
}
}
printf("%d\n",long_m);
}
return 0;
}