记忆化搜索,跟以前的做过的 滑雪 一样的。
DP+DFS。用dp[][]保存搜索记录,然后满足条件累加即可。
#include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #include<iostream> #include<list> #include<set> #include<bitset> #include<vector> #include<cmath> #define INF 0x7fffffff #define eps 1e-8 #define LL long long #define PI 3.141592654 #define CLR(a,b) memset(a,b,sizeof(a)) #define FOR(i,a,b) for(int i=a;i<b;i++) #define FOR_(i,a,b) for(int i=a;i>=b;i--) #define pub push_back #define puf push_front #define pob pop_back #define pof pop_front #define mp make_pair #define ft first #define sd second #define sf scanf #define pf printf #define sz(v) ((int)(v).size()) #define all(v) (v).begin(),(v).end() #define acfun std::ios::sync_with_stdio(false) #define SIZE 100 +1 using namespace std; int n,m; int g[SIZE][SIZE]; int xx[]={0,0,-1,1}; int yy[]={-1,1,0,0}; int dp[SIZE][SIZE]; int dfs(int i,int j) { if(dp[i][j]>1)return dp[i][j]; FOR(k,0,4) { int x=i+xx[k]; int y=j+yy[k]; if(x<0||y<0||x>=n||y>=m)continue; if(g[x][y]<g[i][j]) dp[i][j]=max(dp[i][j],dfs(x,y)+1); } return dp[i][j]; } int main() { int t; sf("%d",&t); while(t--) { char str[SIZE]; sf("%s%d%d",str,&n,&m); FOR(i,0,n) FOR(j,0,m) sf("%d",&g[i][j]),dp[i][j]=1; int ans=0; FOR(i,0,n) FOR(j,0,m) ans=max(ans,dfs(i,j)); pf("%s: %d\n",str,ans); } }
UVA 10285 Longest Run on a Snowboard
原文地址:http://blog.csdn.net/dongshimou/article/details/41442813