百练 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;
}


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