poj(1088)

来源:互联网 发布:身份证拍照软件 编辑:程序博客网 时间:2024/05/16 04:25
题目:http://poj.org/problem?id=1088

记忆化搜索,不然会超时的,这种方式避免了重复计算,所以效率要高一些

#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#define maxn 105using namespace std;int r,c;int map[maxn][maxn];int dp[maxn][maxn];int f(int x, int y){    if(dp[x][y] != 0)       returndp[x][y];    int temp = 0;    if(map[x][y] > map[x + 1][y])       temp = max(temp , f(x + 1,y));    if(map[x][y] > map[x][y + 1])       temp = max(temp , f(x , y + 1));    if(map[x][y] > map[x - 1][y])       temp = max(temp , f(x - 1, y));    if(map[x][y] > map[x][y - 1])       temp = max(temp , f(x , y - 1));    dp[x][y] = temp + 1;    return dp[x][y];}int main(){    int i,j;    while(~scanf("%d%d",&r,&c)){       int ans =0;       memset(map, 0x3f, sizeof(map));       for(i =1;i <=r; i++)          for(j = 1;j <= c;j++)              scanf("%d",map[i] + j);       memset(dp, 0,sizeof(dp));       for(i =1;i <=r;i++)          for(j = 1;j <= c;j++)              ans = max(ans , f(i , j));       printf("%d\n",ans);          }    return 0;}


0 0
原创粉丝点击