Poj 1088 滑雪

主要学习到的是下面两点:

/动态规划,用递归下的记忆化搜索来实现

//状态转移方程 DP(i,j) = max( DP(i-1,j),DP(i+1,j),DP(i,j-1),DP(i,j+1) ) + 1;

 

 

#include

using namespace std;    //动态规划,用递归下的记忆化搜索来实现

                                     //状态转移方程 DP(i,j) = max( DP(i-1,j),DP(i+1,j),DP(i,j-1),DP(i,j+1) ) + 1;

int map[101][101];

int maxlong[101][101]; //记录每个点对应的最大滑雪长度

int R,C;                         //R行,C

 

int DP(int x,int y)

{

         int max = 0;

         if(maxlong[x][y] > 0)             //如果以及计算过,直接返回(记忆化搜索效率之所以高的原因:不重复计算)

                   return maxlong[x][y];

         //以下四块只对合理的xy进行递归,

         if(x-1 >= 0)

         {

                   if(map[x][y] > map[x-1][y])           //如果能走,就进行递归

                   {

                            if(max < DP(x-1,y))

                                     max = DP(x-1,y);

                   }

         }

         if(x+1 < R)

         {

                   if(map[x][y] > map[x+1][y])

                   {

                            if(max < DP(x+1,y))

                                     max = DP(x+1,y);

                   }

         }

         if(y-1 >= 0 )

         {

                   if(map[x][y] > map[x][y-1])

                   {

                            if(max < DP(x,y-1))

                                     max = DP(x,y-1);

                   }

         }

         if(y+1 < C)

         {

                   if(map[x][y] > map[x][y+1])

                   {

                            if(max < DP(x,y+1))

                                     max = DP(x,y+1);

                   }

         }

         return maxlong[x][y] = max + 1;     //将结果记录在数组maxlong[x][y]中(记忆优化的重点);

}

 

int main()

{

         int i,j;

         while(scanf("%d%d",&R,&C) != EOF)

         {

                   for(i=0; i

                   {

                            for(j=0; j

                            {

                                     scanf("%d",&map[i][j]);

                            }

                   }

                   memset(maxlong,0,sizeof(maxlong));

                   for(i=0; i                  //处理每个点,将结果保存在数组maxlong[][]中;

                   {

                            for(j=0; j

                            {

                                     DP(i,j);

                            }

                   }

                   for(i=0; i                  //找到在数组maxlong[][]中最长的滑雪长度

                   {

                            for(j=0; j

                            {

                                     if(maxlong[0][0] < maxlong[i][j])

                                               maxlong[0][0] = maxlong[i][j];

                            }

                   }

                   printf("%d/n",maxlong[0][0]);

         }

         return 0;

}

你可能感兴趣的:(Poj 1088 滑雪)