简单的搜索,不必多说了,初始状态下每个点能到达的长度是1,它本身。还有,注意关掉文件重定向,被坑好多次了。
代码如下:
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 typedef unsigned int ui; 5 typedef long long ll; 6 typedef unsigned long long ull; 7 #define pf printf 8 #define mem(a,b) memset(a,b,sizeof(a)) 9 #define prime1 1e9+7 10 #define prime2 1e9+9 11 #define pi 3.14159265 12 #define scand(x) scanf("%llf",&x) 13 #define f(i,a,b) for(int i=a;i<=b;i++) 14 #define scan(a) scanf("%d",&a) 15 #define dbg(args) cout<<#args<<":"<<args<<endl; 16 #define pb(i) push_back(i) 17 #define ppb(x) pop_back(x) 18 #define inf 0x3f3f3f3f 19 #define maxn 105 20 int n,m,t,a[maxn][maxn]; 21 int dp[maxn][maxn];//dp[i][j]:从(i,j)位置开始的最长道路的长度 22 int dir[][2]={0,1,0,-1,1,0,-1,0}; 23 bool vis[maxn][maxn]; 24 int dfs(int x,int y) 25 { 26 if(vis[x][y])return dp[x][y]; 27 vis[x][y]=true; 28 int xx,yy; 29 f(i,0,3) 30 { 31 xx=x+dir[i][0]; 32 yy=y+dir[i][1]; 33 if(xx<1||xx>n||yy<1||yy>m)continue; 34 if(a[xx][yy]>=a[x][y])continue; 35 dp[x][y]=max(dfs(xx,yy)+1,dp[x][y]); 36 } 37 return dp[x][y]; 38 } 39 int main() 40 { 41 //freopen("input.txt","r",stdin); 42 //freopen("output.txt","w",stdout); 43 //std::ios::sync_with_stdio(false); 44 scan(n); 45 scan(m); 46 int ans=0; 47 f(i,1,n) 48 f(j,1,m) 49 { 50 scan(a[i][j]); 51 dp[i][j]=1; 52 } 53 f(i,1,n) 54 f(j,1,m) 55 { 56 dfs(i,j); 57 ans=max(ans,dp[i][j]); 58 } 59 pf("%d ",ans); 60 }