HDU-1088 — 滑雪 [記憶化搜尋] [深搜]

來源:互聯網
上載者:User

 

滑雪  
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 68005 Accepted: 25017

 

Description

Michael喜歡滑雪百這並不奇怪, 因為滑雪的確很刺激。可是為了獲得速度,滑的地區必須向下傾斜,而且當你滑到坡底,你不得不再次走上坡或者等待升降機來載你。Michael想知道載一個地區中最長底滑坡。地區由一個二維數組給出。數組的每個數字代表點的高度。下面是一個例子
 1  2  3  4 516 17 18 19 615 24 25 20 714 23 22 21 813 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 51 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9

Sample Output

25

 

Code:

 

又是我亂改條件而且亂理解題意, 我饒恕不了自己了....

 

將每個點的最長地區長度並存在這個點上,下次再到這個點就可以直接用了,節約時間

 

#include"string.h"#include"stdio.h"int m,n;int a[110][110];int record[110][110];int slide(int i,int j){int max=0,temp;if(i>m||j>n||i<1||j<1) return 0;if(record[i][j]) return record[i][j];if(a[i+1][j]<a[i][j]){temp = slide(i+1,j);if(temp>max) max = temp;}if(a[i-1][j]<a[i][j]){temp = slide(i-1,j);if(temp>max) max = temp;}if(a[i][j+1]<a[i][j]){temp = slide(i,j+1);if(temp>max) max = temp; }if(a[i][j-1]<a[i][j]){temp = slide(i,j-1);if(temp>max) max = temp; }return record[i][j] = max+1;//1是加上本身這個點,到相鄰點長度為1}int main(){int i,j,max,temp;while(scanf("%d%d",&m,&n)!=EOF){memset(a,0,sizeof(a));memset(record,0,sizeof(record));for(i=1;i<=m;i++)for(j=1;j<=n;j++)scanf("%d",&a[i][j]);max=0;for(i=1;i<=m;i++)//將m*n個點的滑行距離都算{for(j=1;j<=n;j++){temp = slide(i,j);if(temp>max) max = temp;}}printf("%d\n",max);}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.