301006 - 滑雪

题解byliuhaonan

#include <iostream>

using namespace std;

int a[105][105],f[105][105],r,c; //f数组记忆化 
const int dx[4] = {-1,1,0,0}; //增量数组 
const int dy[4] = {0,0,-1,1};

int DFS(int x,int y) //目前在点(x,y) 
{
	if(f[x][y] != 0) return f[x][y]; //如果曾经走到过这个点 
	f[x][y] = 1;
	for(int i = 0;i < 4;i++) //四个方向 
	{
		int next_x,next_y;
		next_x = x + dx[i];
		next_y = y + dy[i];
		if(next_x >= 1&&next_x <= r&&next_y >= 1&&next_y <= c&&a[next_x][next_y] < a[x][y]) f[x][y] = max(f[x][y],DFS(next_x,next_y) + 1); //如果下个点合法,记录最大值 
	}
	return f[x][y];
}

int main()
{
	int i,j,maxn = -1000000000;
	cin>>r>>c;
	for(i = 1;i <= r;i++) for(j = 1;j <= c;j++) cin>>a[i][j];
	for(i = 1;i <= r;i++) for(j = 1;j <= c;j++) maxn = max(maxn,DFS(i,j)); //枚举起点 
	cout<<maxn<<endl;
	return 0; //华丽结尾 
}