百練 1088 dp

1088:滑雪

總時間限制: 
1000ms 
內存限制: 
65536kB
描述
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更長。事實上,這是最長的一條。
輸入
輸入的第一行表示區域的行數R和列數C(1 <= R,C <= 100)。下面是R行,每行有C個整數,代表高度h,0<=h<=10000。
輸出
輸出最長區域的長度。
樣例輸入
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
樣例輸出
25
來源
Don't know

dp有3種思路 : 第一種 :直接求出值來 ,之後這個值並不改變了
第二種 :不停地更新 
第三種 :記憶化搜索
這個地方我用的第一種 因爲從優先隊列裏拿出來的值更新的話一定可以確定,即使比他高的點沒有更新也沒關係,因爲比他低的點都已經更新好了。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include <sstream>
#include <ostream>
#include <algorithm>
#include <ctype.h>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define inf 1e9+7
#define pi acos(-1)
#define natrule exp(1)
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
int mat[200][200];
int dp[200][200];
int r,c;
typedef pair<int,int> P;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int check(int x,int y){
    if(x>=1&&x<=r&&y>=1&&y<=c) return 1;
    return 0;
}
priority_queue<pair<int,P>,vector<pair<int,P> >,greater<pair<int,P> > > q;
int main()
{
    cin>>r>>c;
    for(int i=1;i<=100;i++){
        for(int j=1;j<=100;j++)
            dp[i][j]=1;
    }
    int maxx=-1;
    for(int i=1;i<=r;i++){
        for(int j=1;j<=c;j++){
            cin>>mat[i][j];
            q.push(make_pair(mat[i][j],make_pair(i,j)));
        }
    }
    while(!q.empty()){
        pair<int,P> a=q.top();
        q.pop();
        int x=a.second.first;
        int y=a.second.second;
        for(int i=0;i<4;i++)
           {
            if(check(x+dx[i],y+dy[i])){
                if(mat[x][y]>mat[x+dx[i]][y+dy[i]])
                    dp[x][y]=max(dp[x][y],dp[x+dx[i]][y+dy[i]]+1);
          }
        }
        maxx=max(maxx,dp[x][y]);
    }
    cout<<maxx<<endl;
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章