3
30
2015
0

Tyvj 1004 滑雪

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int s[100][100],r,c,a[100][100],ans;

int f(int x,int y) { //从x,y开始最长走几步
	if (s[x][y]!=-1) return s[x][y]; //如果算过就直接返回
	s[x][y]=1;
	if (x>0 && a[x][y]>a[x-1][y])
		s[x][y]=max(s[x][y],f(x-1,y)+1);
	if (y>0 && a[x][y]>a[x][y-1])
		s[x][y]=max(s[x][y],f(x,y-1)+1);
	if (x<r-1 && a[x][y]>a[x+1][y])
		s[x][y]=max(s[x][y],f(x+1,y)+1);
	if (y<c-1 && a[x][y]>a[x][y+1])
		s[x][y]=max(s[x][y],f(x,y+1)+1);
	return s[x][y];
}

int main() {
	cin>>r>>c; memset(s,-1,sizeof(s));
	for (int i=0;i<r;++i)
		for (int j=0;j<c;++j)
			cin>>a[i][j];
	for (int i=0;i<r;++i)
		for (int j=0;j<c;++j)
			ans=max(ans,f(i,j)); //求最大值
	cout<<ans;
	return 0;
}
Category: 题解 | Tags: Tyvj 记忆化搜索

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com