1. 程式人生 > >poj 1088滑雪 (動態規劃)

poj 1088滑雪 (動態規劃)

滑雪
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 59524Accepted: 21672

Description

Michael喜歡滑雪百這並不奇怪, 因為滑雪的確很刺激。可是為了獲得速度,滑的區域必須向下傾斜,而且當你滑到坡底,你不得不再次走上坡或者等待升降機來載你。Michael想知道載一個區域中最長底滑坡。區域由一個二維陣列給出。陣列的每個數字代表點的高度。下面是一個例子 
 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

輸入的第一行表示區域的行數R和列數C(1 <= R,C <= 100)。下面是R行,每行有C個整數,代表高度h,0<=h<=10000。

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

Source

這道題關鍵是要理解題目的意思,理解好了就大概可以判斷出題目的一般解法可以使列舉每個點的最長距離,但是這樣可能會TLE,所以加上記錄就可以了,簡單的實現可以考慮遞迴,然後就可以了。這道題的最優子結構是m[i][j]表示(i,j)點出發的最長距離,然後轉移方程是m[i][j]=max(m[i-1][j],m[i+1][j],m[i][j-1],m[i][j+1])+1(就是哪個方向的點有最長距離就往哪裡走)

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int a[105][105];
int m[105][105];

int findmax(int i,int j)
{
    if(m[i][j]!=0) return m[i][j];
    int maxe=0;
    if(a[i][j]>a[i-1][j] && findmax(i-1,j)>maxe)
       maxe=findmax(i-1,j);
    if(a[i][j]>a[i+1][j] && findmax(i+1,j)>maxe)
       maxe=findmax(i+1,j);
    if(a[i][j]>a[i][j-1] && findmax(i,j-1)>maxe)
       maxe=findmax(i,j-1);
    if(a[i][j]>a[i][j+1] && findmax(i,j+1)>maxe)
       maxe=findmax(i,j+1);
    return m[i][j]=maxe+1;
}

int main()
{
    int R,C;
    scanf("%d%d",&R,&C);
    memset(a,10005,sizeof(a));
    memset(m,0,sizeof(m));
    int i,j;
    int maxe=-1; 
    for(i=1;i<=R;i++)
       for(j=1;j<=C;j++)
          scanf("%d",&a[i][j]);
    for(i=1;i<=R;i++)
       for(j=1;j<=C;j++)
         if(maxe<findmax(i,j))  maxe=findmax(i,j);
    printf("%d\n",maxe);
    system("pause");
    return 0;
}

相關推薦

poj 1088滑雪 動態規劃

滑雪Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 59524Accepted: 21672DescriptionMichael喜歡滑雪百這並不奇怪, 因為滑雪的確很刺激。可是為了獲得速度,滑的區域必須向

POJ 1088 滑雪動態規劃解題報告

Description Michael喜歡滑雪百這並不奇怪, 因為滑雪的確很刺激。可是為了獲得速度,滑的區域必須向下傾斜,而且當你滑到坡底,你不得不再次走上坡或者等待升降機來載你。Michael想知道載一個區域中最長底滑坡。區域由一個二維陣列給出。陣列的每個數

POJ 1088 滑雪動態規劃經典

滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 63875 Accepted: 23387 Description Michael喜歡滑雪百這並不奇怪, 因為滑雪的確很刺激。可是為了獲得速度

poj 1088 滑雪動態規劃:記憶化搜尋

這個題開始想著用動態規劃遞推來做的 但是根本不知從哪裡下手 想了下還是記憶化更方便 我的方法是先把邊界設定為無窮大 每次dfs知道當前點周圍沒有比它還低的位置即可 0ms程式碼如下: #include

poj 1088 滑雪-----DP 動態規劃

滑雪 Time Limit:1000MS Memory Limit:65536K Total Submissions:57209 Accepted:20771 Description Michael喜歡滑雪百這並不奇怪, 因為滑雪的確很刺激。可是為了

滑雪動態規劃

Description Michael喜歡滑雪百這並不奇怪,因為滑雪的確很刺激。可是為了獲得速度,滑的區域必須向下傾斜,而且當你滑到坡底,你不得不再次走上坡或者等待升降機來載你。Michael想知道載一個區域中最長底滑坡。區域由一個二維陣列給出。陣列的每個數字代表點的高度。下面是一個例子 1 2 3

E - 滑雪 POJ - 1088 動態規劃

E - 滑雪  POJ - 1088  Michael喜歡滑雪百這並不奇怪, 因為滑雪的確很刺激。可是為了獲得速度,滑的區域必須向下傾斜,而且當你滑到坡底,你不得不再次走上坡或者等待升降機來載你。Michael想知道載一個區域中最長底滑坡。區域由一個二維陣列給出。陣列的每

POJ 1088 滑雪 記憶化、動態規劃、排序優化

滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 108063 Accepted: 41157 Description Michael喜歡滑雪百這並不奇怪, 因為

POJ 1088: 滑雪經典 DP+記憶化搜索

esp roman ted font eof 個人 algorithm set str 滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 74996

poj - 1088 - 滑雪dp

target art dsm 題目 ipp 每次 元素 org mod 題意:一個R * C的矩陣(1 <= R,C <= 100),元素代表該點的高度h(0<=h<=10000),從隨意點出發,每次僅僅能走上、下、左、右。且將要到的高度要比

POJ-1050】To The Max動態規劃

ref script greate err sca max des eat tput To the Max Time Limit: 1000MS Memory Limit: 10000K Description Given a two-dimensional array

poj-2533 longest ordered subsequence動態規劃

末尾 ring pac cond print ges sam fine nlog Time limit2000 ms Memory limit65536 kB A numeric sequence of ai is ordered if a1 < a2 <

POJ 1088 滑雪 搜尋

Description Michael喜歡滑雪百這並不奇怪, 因為滑雪的確很刺激。可是為了獲得速度,滑的區域必須向下傾斜,而且當你滑到坡底,你不得不再次走上坡或者等待升降機來載你。Michael想知道載一個區域中最長底滑坡。區域由一個二維陣列給出。陣列的每個數字代表點的高度。下面是一個例子

T - Whac-a-Mole POJ - 3034 動態規劃

T - Whac-a-Mole  POJ - 3034  While visiting a traveling fun fair you suddenly have an urge to break the high score in the Whac-a-Mole ga

I - Post Office POJ - 1160 動態規劃

I - Post Office  POJ - 1160  There is a straight highway with villages alongside the highway. The highway is represented as an integer a

D - To the Max POJ - 1050 動態規劃

D - To the Max  POJ - 1050  Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array

C - A decorative fence POJ - 1037 動態規劃

C - A decorative fence  POJ - 1037  Richard just finished building his new house. Now the only thing the house misses is a cute little w

POJ 1458 Common Subsequence動態規劃

Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 61454 &n

poj(1088)——滑雪經典遞推型動歸

題意: 中文題,就是在所有的點中找一個點作為起點,然後叫你找出從起點開始的最長路徑是多少。 這裡高度必須嚴格遞減。 思路: 一開始我碰到這題時,沒有思路,是看題解寫的。 但是今天我回頭再去看時,發現自己能夠獨立寫出來了,而且和上次的方法不一樣。也許這就是進步吧! 其實就是一

POJ 1088 滑雪 記憶化搜尋

                                                                滑雪 Time Limit:1000MS    Memory Lim