Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

这题需要DP么?我直接对所有点排序 然后从小到大计算longest path不行么(记录已经计算的格子的longest path)? 我觉得

Posted by alpc12 at 2007-02-13 15:50:54 on Problem 1088
#include <stdio.h>
#include <algorithm>
using std::sort;
const int N = 101;
int lgst[N][N];
struct node{int x, y, h; void set(int xx, int yy, int hh) {x = xx; y=yy; h=hh;}}nd[N*N];
int brd[N][N];
int r, c;

bool operator<(const node& a, const node& b) {
	return a.h < b.h;
}

int main() {
	int i, j;
	scanf("%d%d", &r, &c);
	for(i = 0; i<r; i++) 
		for(j = 0; j<c; j++) {
			scanf("%d", &(brd[i][j]));
			nd[i*c+j].set(i, j, brd[i][j]);
		}
	sort(nd, nd+r*c);
	for(i = 0; i < r; i++)
		for(j = 0; j < c; j++)
			lgst[i][j] = 1;
	int max = 1;
	for(i = 1; i<r*c; i++) {
		int x = nd[i].x;
		int y = nd[i].y;
		int h = nd[i].h;
		if(x > 0 && brd[x-1][y] < h) 
			if(lgst[x-1][y] + 1 > lgst[x][y])
				lgst[x][y] = lgst[x-1][y] + 1;
		if(x < c-1 && brd[x+1][y] < h) 
			if(lgst[x][y] < lgst[x+1][y] + 1)
				lgst[x][y] = lgst[x+1][y] + 1;
		if(y > 0 && brd[x][y-1] < h) 
			if(lgst[x][y-1] + 1 > lgst[x][y])
				lgst[x][y] = lgst[x][y-1] + 1;
		if(y < r-1 && brd[x][y+1] < h)
			if(lgst[x][y] < lgst[x][y+1] +1)
				lgst[x][y] = lgst[x][y+1] + 1;
		if(lgst[x][y] > max) max = lgst[x][y];
	}
	printf("%d\n", max);
	return 0;
}
	

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator