| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 83489 | Accepted: 31234 |
Description
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
Input
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
100*100的矩阵,如果裸dfs很可能超时,所以可以用记忆化搜索的方式,dp[i][j]表示当前到达[i,j]的最长路径,在dfs的同时,更新dp[i][j]。思路总体来说很简单。
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma commment(linker,"/STACK: 102400000 102400000")
#define mset0(t) memset(t,0,sizeof(t))
#define lson a,b,l,mid,cur<<1
#define rson a,b,mid+1,r,cur<<1|1
using namespace std;
const double eps=1e-6;
const int MAXN=110;
int vis[MAXN][MAXN],dp[MAXN][MAXN],num[MAXN][MAXN],n,m,dir1[4]={0,0,-1,1},dir2[4]={-1,1,0,0};
//dp[i][j]代表到达[i,j]的最长路径
void dfs(int x,int y)
{
for(int i=0;i<4;i++)
{
int xxx=x+dir1[i];
int yyy=y+dir2[i];
if(xxx>=1&&xxx<=n&&yyy>=1&&yyy<=m&&num[xxx][yyy]>num[x][y])
{
if(dp[x][y]+1>dp[xxx][yyy])//如果走到[xxx,yyy]后到达[xxx,yyy]的最长路径会增长,走上去才有意义
{
dp[xxx][yyy]=dp[x][y]+1;
dfs(xxx,yyy);
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
scanf("%d",&num[i][j]);
dp[i][j]=1;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
dfs(i,j);
int ans=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
ans=max(ans,dp[i][j]);
printf("%d\n",ans);
}
return 0;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/noooooorth/article/details/47073815