日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

NUC_HomeWork1 -- POJ1088(DP)

發(fā)布時間:2025/5/22 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NUC_HomeWork1 -- POJ1088(DP) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
D -?滑雪

Description

Michael喜歡滑雪百這并不奇怪, 因為滑雪的確很刺激。可是為了獲得速度,滑的區(qū)域必須向下傾斜,而且當(dāng)你滑到坡底,你不得不再次走上坡或者等待升降機來載你。Michael想知道載一個區(qū)域中最長底滑坡。區(qū)域由一個二維數(shù)組給出。數(shù)組的每個數(shù)字代表點的高度。下面是一個例子?
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
一個人可以從某個點滑向上下左右相鄰四個點之一,當(dāng)且僅當(dāng)高度減小。在上面的例子中,一條可滑行的滑坡為24-17-16-1。當(dāng)然25-24-23-...-3-2-1更長。事實上,這是最長的一條。

Input

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

Output

輸出最長區(qū)域的長度。

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

小白上的記憶化搜索
int d(int i, int j)
{
  if(d[i][j] >= 0)
    return d[i][j];
  return d[i][j] = a[i][j] + (i == n ? 0 : d(i+1, j) >? d[i+1][j+1]);
}
同本題很想啊
弄一個dis[][]數(shù)組保存距離,然后看它4個方向能否繼續(xù)走下去,能走下去,就一直遞歸下去 1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 6 const int MAXN = 110; 7 int arr[MAXN][MAXN]; 8 int dis[MAXN][MAXN]; 9 int mov[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; 10 int line, col; 11 12 bool test(int x, int y) 13 { 14 if(x >= 0 && x < line && y >= 0 && y < col) 15 return true; 16 return false; 17 } 18 19 int d(int x, int y) 20 { 21 int tmp; 22 if(dis[x][y]) 23 return dis[x][y]; 24 25 for(int i = 0; i < 4; ++i) 26 { 27 int xx = x + mov[i][0]; 28 int yy = y + mov[i][1]; 29 if(test(xx, yy)) 30 { 31 if(arr[x][y] > arr[xx][yy]) 32 { 33 tmp = d(xx, yy); 34 dis[x][y] = dis[x][y] > tmp ? dis[x][y] : tmp + 1; 35 } 36 } 37 } 38 return dis[x][y]; 39 } 40 41 int main() 42 { 43 44 while(scanf("%d %d", &line, &col) != EOF) 45 { 46 for(int i = 0; i < line; i++) 47 for(int j = 0; j < col; j++) 48 scanf("%d", &arr[i][j]); 49 50 memset(dis, 0, sizeof(dis)); 51 52 int ans = 0, tmp; 53 for(int i = 0; i < line; i++) 54 { 55 for(int j = 0; j < col; j++) 56 { 57 tmp = d(i, j); 58 ans = ans > tmp ? ans : tmp; 59 } 60 } 61 62 printf("%d\n", ans + 1); 63 } 64 return 0; 65 } View Code

?

轉(zhuǎn)載于:https://www.cnblogs.com/ya-cpp/p/4075252.html

總結(jié)

以上是生活随笔為你收集整理的NUC_HomeWork1 -- POJ1088(DP)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。