第二和第十个一直显示RE
查看原帖
第二和第十个一直显示RE
490370
Siing_Rousong楼主2021/4/20 19:27

题解里也有与我十分相似的代码

#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int place[105][105] = {0};
int record[105][105] = {0};
int ans = 0;
int flag = 1;
int max_e = 0;

struct point {
	int x;
	int y;
	int height;
};
point point_record[105];

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

void findpathlength(int r, int c) {
	for (int i = 1; i <= r * c; i++) {
		int up=0, down=0, left=0, right=0;
		int now_x = point_record[i].x;
		int now_y = point_record[i].y;
		int now_height = point_record[i].height;
		
				if (place[now_x - 1][now_y] < now_height) {
					up = record[now_x - 1][now_y];
				}
				record[now_x][now_y] = max(up+1, record[now_x][now_y]);

				if (place[now_x + 1][now_y] < now_height) {
					down = record[now_x+ 1][now_y];
				}
				record[now_x][now_y] = max(down + 1, record[now_x][now_y]);
				
				if (place[now_x][now_y-1] < now_height) {
					left = record[now_x][now_y-1];
				}
				record[now_x][now_y] = max(left + 1, record[now_x][now_y]);

				if (place[now_x][now_y +1] < now_height) {
					right = record[now_x][now_y+1];
				}
				record[now_x][now_y] = max(right + 1, record[now_x][now_y]);

				if (max_e < record[now_x][now_y])max_e = record[now_x][now_y];	
			}
}

int main()
{
	int row, col;
	cin >> row >> col;
	for (int i = 1; i <= row; i++) {
		for (int j = 1; j <= col; j++)
		{
			cin >> place[i][j];
			point_record[flag].x = i;
			point_record[flag].y = j;
			point_record[flag].height = place[i][j];
			record[i][j] = 1;
			flag++;
		}
	}
	sort(point_record+1 , point_record + row * col+1 ,cmp);
	findpathlength(row,col);
	cout << max_e;
}
2021/4/20 19:27
加载中...