poj 百練1088滑雪路線問題 動態規劃

#include <stdio.h>
#include <stdlib.h>

#define NUM 105

int h[NUM][NUM];
struct Node{
	int x,y;
	int height;
};
struct Node nodes[NUM*NUM];
int cmp(const void* a,const void *b){
	struct Node aa,bb;
	aa=*((struct Node*)a);
	bb=*((struct Node*)b);
	return aa.height-bb.height;
}
int steps[NUM][NUM];
void init(){
	int i,j;
	for(i=0;i<NUM;i++){
		for(j=0;j<NUM;j++){
			steps[i][j]=1;
		}
	}
}
int  main(){
	int r,c;
	scanf("%d %d",&r,&c);
	int index=0;
	int i,j;
	for(i=1;i<=r;i++){
		for(j=1;j<=c;j++){
			int t;
			scanf("%d",&t);
			h[i][j]=t;
			nodes[index].x=i;
			nodes[index].y=j;
			nodes[index].height=t;
			index++;
		}
	}
	qsort(nodes,index,sizeof(struct Node),cmp);
	init();
	int max=1;
	int k;
	for(k=0;k<index;k++){
		int x=nodes[k].x;
		int y=nodes[k].y;
		//up
		if(x>1&&h[x-1][y]>h[x][y]&&steps[x-1][y]<steps[x][y]+1){
			steps[x-1][y]=steps[x][y]+1;
		}
		//right
		if(y<c&&h[x][y+1]>h[x][y]&&steps[x][y+1]<steps[x][y]+1){
			steps[x][y+1]=steps[x][y]+1;
		}
		//down
		if(x<r&&h[x+1][y]>h[x][y]&&steps[x+1][y]<steps[x][y]+1){
			steps[x+1][y]=steps[x][y]+1;
		}
		//left
		if(c>1&&h[x][y-1]>h[x][y]&&steps[x][y-1]<steps[x][y]+1){
			steps[x][y-1]=steps[x][y]+1;
		}
	}
	int ii,jj;
	for(ii=1;ii<=r;ii++){
		for(jj=1;jj<=c;jj++){
			if(steps[ii][jj]>max)
				max=steps[ii][jj];
		}
	}
	printf("%d\n",max);
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章