Poj 1088 滑雪(經典動態規劃)

來源:互聯網
上載者:User

   很早就看了這個題,想了很久一直沒有實現,後來看別人的代碼,說是經典的動態規劃,但是我還是看不懂。過了一段時間又自己想了很久,才寫成了代碼,代碼有3000B,在北大已耗用時間也是200+MS,後來又看了一遍別人的動態規劃代碼,終於明白怎麼規了,然後自己根據別人的代碼又重新寫了一個動態規劃的代碼,代碼如下,已耗用時間0MS。(哎....還是動態規劃的意思比較明確啊..)

/**規劃思想:DP[i][j] = Max{ DP[i-1][j], DP[i+1][j], DP[i][j-1], DP[i][j+1] } + 1;**/#include <stdio.h>int r, c, map[101][101], dp[101][101], MAX;int DP(int x, int y){    int i, x0, y0, max=0, dir[4][2]={1,0,-1,0,0,1,0,-1};    if (dp[x][y]) return dp[x][y];    for (i=0; i<4; i++)    {        x0 = x + dir[i][0];        y0 = y + dir[i][1];        if (x0<0 || y0<0 || x0>=r || y0>=c) continue;        if (map[x0][y0]<map[x][y] && DP(x0, y0)>max) max = dp[x0][y0];    }    dp[x][y] = max + 1;    return dp[x][y];}int main(){    int i, j;    scanf("%d %d", &r, &c);    for (i=0; i<r; i++)      for (j=0; j<c; j++)        scanf("%d", &map[i][j]);    for (i=0; i<r; i++)      for (j=0; j<c; j++)      {          DP(i, j);          if (dp[i][j] > MAX) MAX = dp[i][j];      }    printf("%d/n", MAX);}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.