Openjudge-百练-1088-滑雪(动态规划)

原题链接:

#include <iostream>
using namespace std;

int m,n;
int s1[100][100],s2[100][100];	//s1为高度数组,s2为递归过程存储滑坡高度 
int step[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};	//改变x,y值控制走动方向 
int x_temp,y_temp;

int bfs(int x, int y)
{
	int max_high = 1;	//最大高度 
	
	if(s2[x][y] != 0)	//若存储数组内有该项数据,则直接返回该项的值 
		return s2[x][y];
		
	for(int i = 0; i < 4; ++i)		//遍历四个方向 
	{
		x_temp = x+step[i][0];
		y_temp = y+step[i][1];
		if(x_temp < 0 || x_temp >= m || y_temp < 0 || y_temp >= n)	//判断是否超界 
			continue;
		if(s1[x][y] < s1[x_temp][y_temp])
		{
			int max_step = bfs(x_temp,y_temp)+1;
			if(max_high < max_step) 
			max_high = max_step;
		}
	}
	s2[x][y] = max_high; 
	return max_high;
}

int main()
{
	int max = 0;
	cin >> m >> n;
	for(int i = 0; i < m; ++i)
		for(int j = 0; j < n; ++j)
			cin >> s1[i][j];
	for(int i = 0; i < m; ++i)
		for(int j = 0; j < m; ++j)
		{
			if(max < bfs(i,j))
				max = bfs(i,j);
		}
	cout << max;
}

猜你喜欢

转载自blog.csdn.net/adorkable_thief/article/details/80191320