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

DFS + 剪枝0ms。哪来DP的结构啊。。

Posted by lulyon at 2011-03-08 16:08:53 on Problem 1088 and last updated at 2011-03-08 16:10:46
#include<iostream>
using namespace std;

typedef struct INFO{
	int h;      //(i,j)点的高度 
	int length;//保存以(i,j)为起点的最大滑雪长度 
}InfoType;

InfoType a[110][110];
int r,c;

void dfs(int i,int j){
	
	if(a[i][j].length>1)return; //已经求出最大滑雪长度的不必再搜了,剪掉
	
	int maxlength = 0;
	
	if(i-1>=0){
		if(a[i][j].h>a[i-1][j].h){
			dfs(i-1,j);
			if(maxlength<a[i-1][j].length)maxlength = a[i-1][j].length;
		}
	}
	if(i+1<r){
		if(a[i][j].h>a[i+1][j].h){
			dfs(i+1,j);
			if(maxlength<a[i+1][j].length)maxlength = a[i+1][j].length;
		}
	}
	if(j-1>=0){
		if(a[i][j].h>a[i][j-1].h){
			dfs(i,j-1);
			if(maxlength<a[i][j-1].length)maxlength = a[i][j-1].length;
		}
	}
	if(j+1<r){
		if(a[i][j].h>a[i][j+1].h){
			dfs(i,j+1);
			if(maxlength<a[i][j+1].length)maxlength = a[i][j+1].length;
		}
	}
	
	a[i][j].length = maxlength+1;
}

int main(){

	scanf("%d%d",&r,&c);
	
	for(int i=0; i<r; ++i){
		for(int j=0; j<c; ++j){
			scanf("%d",&a[i][j].h);
			a[i][j].length=1;//(i,j)为起点的最大滑雪长度的初始值为1 
		}
	}
	
	for(int i=0; i<r; ++i){
		for(int j=0; j<c; ++j){
			dfs(i,j);//以(i,j)为起点,深搜.
		}
	}
	int maxlength=-1;
	for(int i=0; i<r; ++i){
		for(int j=0; j<c; ++j){
			if(maxlength<a[i][j].length)
				maxlength = a[i][j].length;//找全局的最大滑雪长度 
		}
	}
	
	printf("%d\n",maxlength);
	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