POJ 1088 滑雪 (記憶化、動態規劃、排序優化)
阿新 • • 發佈:2018-12-13
滑雪
Time Limit: 1000MS | Memory Limit: 65536K |
Total Submissions: 108063 | Accepted: 41157 |
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
方法一: 記憶化搜尋
狀態 表示 以 (i,j) 為最低點的 最長 長度
code:
#include<iostream> #include<stdio.h> #include<vector> #include<map> #include<cstring> #include<cstdlib> #include<algorithm> #include<set> #include<cmath> using namespace std; const int N = 105; int R,C; int dp[N][N]; int a[N][N]; // 判斷合法性 int ok(int i, int j){ if(i >= 1 && i <= R && j >= 1 && j <= C) return true; return false; } int max_h; // 在相鄰的 不越界的位置記憶化搜尋 每次搜尋 表示 當前位置為最低點 的最長長度 int rec(int i, int j){ if(dp[i][j] != 1) return dp[i][j]; int &ans = dp[i][j]; if(ok(i+1,j) && a[i+1][j] > a[i][j]) ans = max(ans,rec(i+1,j) + 1); if(ok(i-1,j) && a[i-1][j] > a[i][j]) ans = max(ans,rec(i-1,j) + 1); if(ok(i,j+1) && a[i][j+1] > a[i][j]) ans = max(ans,rec(i,j+1) + 1); if(ok(i,j-1) && a[i][j-1] > a[i][j]) ans = max(ans,rec(i,j-1) + 1); return ans; } int main() { while(scanf("%d%d",&R,&C) != EOF){ max_h = 0; for(int i = 1; i <= R; ++i){ for(int j = 1; j <= C; ++j){ scanf("%d",&a[i][j]); dp[i][j] = 1; } } for(int i = 1; i <= R; ++i){ for(int j = 1; j <= C; ++j){ max_h = max(rec(i,j),max_h); //每一次 的rec(i,j) 是以 (i,j) 為終點的 最長路徑 不需要重新初始化 } } printf("%d\n",max_h); } return 0; }
此題 記憶化 搜尋速度較快
方法二:記錄每個高度的座標,對高度從小到大排序,然後進行dp
定義狀態:
為 以 為最高點 的最長長度
狀態轉移方程:
其中 : 為 與 相鄰的 並且 高度低於 的
code:
#include<iostream>
#include<stdio.h>
#include<vector>
#include<map>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<cmath>
using namespace std;
const int N = 105;
struct H{
int h,x,y;
H(){}
H(int ph,int px,int py):h(ph),x(px),y(py){}
bool operator<(const H o)const {
return h < o.h;
}
};
H hs[N*N];
int dp[N*N];
int my_abs(int a, int b){
return a > b ? a-b : b-a;
}
bool adj(int x1,int y1, int x2, int y2){
if(x1 == x2 && my_abs(y1,y2) == 1)
return true;
if(y1 == y2 && my_abs(x1,x2) == 1)
return true;
return false;
}
int main()
{
int R,C;
int h;
while(scanf("%d%d",&R,&C) != EOF){
int cnt = 1;
for(int i = 1;i <= R; ++i){
for(int j = 1; j <= C; ++j){
scanf("%d",&h);
hs[cnt++] = H(h,i,j);
}
}
memset(dp,0,sizeof(dp));
sort(hs+1,hs+cnt);
for(int i = 1; i <= R*C; ++i){ // 初始化
dp[i] = 1;
}
int ans = 0;
for(int i = 1; i <= R*C; ++i){
for(int j = 1; j < i; ++j){
if(adj(hs[i].x,hs[i].y,hs[j].x,hs[j].y) && hs[i].h > hs[j].h){ // 相鄰 且高度上升
dp[i] = max(dp[i],dp[j] + 1);
}
}
ans = max(ans,dp[i]);
}
printf("%d\n",ans);
}
return 0;
}
這種方法 很耗時,要 800ms 才AC,其實 不需要遍歷 從i 到 j 找相鄰點,可以直接判斷相鄰點滿足條件與否,下面是改進
可以在 32ms甚至 16ms AC。
方法二的優化:
狀態 位置 為最高點 的 最長路徑
狀態轉移:
其中 為 與 相鄰 並且 高度小於 位置 的點。
注意:這裡 與 (i,j) 相鄰 並且高度 低於 的點 一定位於 遞推式的 第 步之前。(因為 已經按照高度排序了)
#include<iostream>
#include<stdio.h>
#include<vector>
#include<map>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<cmath>
using namespace std;
const int N = 105;
int a[N][N];
int dp[N][N];
struct H{
int h,x,y;
H(){}
H(int ph,int px, int py):h(ph),x(px),y(py){}
bool operator<(const H o)const{
return h < o.h;
}
};
H hs[N*N];
int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
/*
優化的地方:
用 a[][] 陣列 初始化 為 -1 可以快速 判斷 是否越界 (輸入完成後越界的為 -1 非越界的不為 -1)
不再是 從 j 遍歷到 i 找與 i 相鄰 的節點 而是 直接從相鄰點中找滿足條件的(高度更小、不越界) 這樣會快 很多
*/
int main()
{
int R,C;
scanf("%d%d",&R,&C);
memset(a,-1,sizeof(a));
int h;
int cnt = 1;
for(int i = 1; i <= R; ++i){
for(int j = 1; j <= C; ++j){
scanf("%d",&h);
hs[cnt++] = H(h,i,j);
a[i][j] = h;
}
}
sort(hs+1,hs+cnt);
//fill(dp[0],dp[0]+N*N,1); // 如果 不初始化為 1 則結果需要加 1 (自身)
int curx,cury,nx,ny,curh;
int ans = 0;
for(int i = 1; i < cnt; ++i){
curx = hs[i].x; cury = hs[i].y; curh = hs[i].h;
for(int k = 0; k < 4; ++k){
nx = curx + dir[k][0];
ny = cury + dir[k][1];
if(a[nx][ny] != -1 && a[nx][ny] < curh) // 滿足 不越界 並且相鄰 然後 高度小於當前高度的 肯定在前面 也就是 已經計算過了的之前狀態
dp[curx][cury] = max(dp[curx][cury],dp[nx][ny] + 1);
}
ans = max(ans,dp[curx][cury]);
}
printf("%d\n",ans+1);
return 0;
}