liukairui @ 2019-08-13 20:20:52
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
struct pos{
int x,y,h,l;
bool operator<(const pos& t)const{
return h<t.h;
}
}G[150][150];
bool vis[150][150];
int c,r,ans=1,wx[10]={0,-1,0,1},wy[10]={-1,0,1,0};
void bfs(int x,int y){
//if(vis[x][y])return;
vis[x][y]=true;
queue<pos> q;
G[x][y].l=1;
q.push(G[x][y]);
while(!q.empty()){
int sx=q.front().x,sy=q.front().y;
q.pop();
for(int i=0;i<4;i++){
if(sx+wx[i]>=0&&sx+wx[i]<c&&sy+wy[i]>=0&&sy+wy[i]<r&&G[sx+wx[i]][sy+wy[i]]<G[sx][sy]){
G[sx+wx[i]][sy+wy[i]].l=G[sx][sy].l+1;
q.push(G[sx+wx[i]][sy+wy[i]]);
ans=max(ans,G[sx+wx[i]][sy+wy[i]].l);
}
}
}
return;
}
int main(){
scanf("%d%d",&r,&c);
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
G[i][j].x=i;
G[i][j].y=j;
scanf("%d",&G[i][j].h);
}
}
for(int i=0;i<=r;i++)for(int j=0;j<=c;j++)vis[i][j]=false;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(vis[i][j])continue;
bfs(i,j);
}
}
cout<<ans;
return 0;
}
by Surpersolo @ 2019-08-20 09:39:41
#include<bits/stdc++.h>
using namespace std;
int n,m,a[110][110],f[110][110]= {},t,maxn,sum;
int dx[]= {1,-1,0,0};
int dy[]= {0,0,1,-1};
int search(int x,int y) {
if(f[x][y]>0)
return f[x][y];
t=1;
for(int i=0; i<=3; i++) {
int nx=x+dx[i];
int ny=y+dy[i];
if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&a[nx][ny]>a[x][y]) {
sum=search(nx,ny)+1;
if(sum>t)
t=sum;
}
}
f[x][y]=t;
return t;
}
int main() {
cin>>n>>m;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
cin>>a[i][j];
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++) {
t=search(i,j);
f[i][j]=t;
if(f[i][j]>maxn)
maxn=f[i][j];
}
cout<<maxn;
return 0;
}
by Surpersolo @ 2019-08-20 09:40:16