90分DP求助
查看原帖
90分DP求助
836324
Leslie_Lau楼主2024/8/8 21:52

评测记录

代码:

#include <iostream>
#include <algorithm>
using namespace std;

struct stu {
	int height, x, y;
} d[10005];

bool cmp (stu a, stu b) {
	return a.height < b.height;
}

int n, m, mp[105][105], cnt, dp[105][105], ans;
int movex[ ] = {0, 1, -1, 0, 0};
int movey[ ] = {0, 0, 0, 1, -1};

int main () {
	cin >> n >> m;
	if (n == 0 || m == 0) {
		cout << 1;
		return 0;
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> mp[i][j];
			d[++cnt].height = mp[i][j];
			d[cnt].x = i; d[cnt].y = j;
		}
	}
	sort (d + 1, d + cnt + 1, cmp);
	for (int i = 1; i <= cnt; i++) {
		int x = d[i].x, y = d[i].y;
		for (int j = 1; j <= 4; j++) {
			int nowx = x + movex[j];
			int nowy = y + movey[j];
			if (mp[nowx][nowy] == mp[x][y]) continue;
			dp[x][y] = max (dp[x][y], dp[nowx][nowy] + 1);
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			ans = max (ans, dp[i][j]);
		}
	}
	cout << ans;
	return 0;
}
2024/8/8 21:52
加载中...