记忆化搜索


Code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
//Mystery_Sky
//
#define M 500
#define INF 0x3f3f3f3f
int f[M][M];
int r, c, map[M][M], ans;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int dfs(int x, int y)
{
	if(f[x][y]) return f[x][y];
	int maxx = 1;
	int step = 0;
	for(int i = 0; i < 4; i++) {
		int xx = x + dx[i];
		int yy = y + dy[i];
		step = 0;
		if(xx <= 0 || xx > r || yy <= 0 || yy > c) continue;
		if(map[xx][yy] < map[x][y])	step = dfs(xx, yy) + 1;
		maxx = max(maxx, step);
	}
	f[x][y] = maxx;
	return f[x][y];
}

int main() {
	scanf("%d%d", &r, &c);
	for(int i = 1; i <= r; i++)
		for(int j = 1; j <= c; j++) scanf("%d", &map[i][j]);
	for(int i = 1; i <= r; i++)
		for(int j = 1; j <= c; j++) {
			f[i][j] = dfs(i, j);
			ans = max(ans, f[i][j]);
		}
	printf("%d\n", ans);
	return 0;
}

相关文章:

  • 2021-05-24
  • 2021-08-19
  • 2021-09-05
  • 2021-11-08
  • 2021-07-31
  • 2021-07-20
  • 2021-08-05
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-21
  • 2021-08-18
  • 2021-11-30
  • 2021-11-24
  • 2021-09-05
相关资源
相似解决方案