【UVA】10285-Longest Run on a Snowboard(動態規劃)

來源:互聯網
上載者:User

標籤:style   http   color   使用   os   io   for   2014   

這題出簡單了,不需要列印路徑。

狀態方程dp[i][j] = max(dp[i-1][j],dp[i][j-1],dp[i+1][j],dp[i][j+1]);

14003395 10285 Longest Run on a Snowboard Accepted C++ 0.026 2014-08-07 11:43:51

枚舉每個點進行遍曆,使用記憶化搜尋。要不會逾時。

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<stack>#include<queue>#include<map>#include<set>#include<list>#include<string>#include<sstream>#include<ctime>using namespace std;#define _PI acos(-1.0)#define INF (1 << 10)#define esp 1e-6typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> pill;/*======================================================================================*/#define MAXD 100 + 10char name[MAXD];int m,n,ans;int dp[MAXD][MAXD];int mat[MAXD][MAXD];int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};int DP(int x,int y){    if(dp[x][y] != -1)        return dp[x][y];    dp[x][y] = 1;    for(int i = 0 ; i < 4 ; i++){        int _x = dir[i][0] + x;        int _y = dir[i][1] + y;        if(_x >= 0 && _y >=0 && _x < n && _y < m && mat[x][y] < mat[_x][_y]){            dp[x][y] = max(dp[x][y],DP(_x,_y) + 1);        }    }    ans = max(ans,dp[x][y]);    return dp[x][y];}int main(){    int T;    scanf("%d",&T);    while(T--){        scanf("%s%d%d",name,&n,&m);        int pos_x,pos_y;        int MIN = INF;        memset(dp,-1,sizeof(dp));        for(int i = 0 ; i < n ; i++)            for(int j = 0 ; j < m ; j++){                scanf("%d",&mat[i][j]);                if(mat[i][j] < MIN){                    MIN = mat[i][j];                    pos_x = i;                    pos_y = j;                }            }        ans = 0;        for(int i = 0 ; i < n ; i++)            for(int j = 0 ; j < m ; j++)                DP(i,j);        printf("%s: %d\n",name,ans);    }    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.