滑雪问题

来源:互联网 发布:怎么提高编程打字速度 编辑:程序博客网 时间:2024/04/30 18:03
题目内容:


 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子 
 1  2  3  4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
输入描述


输入的第一行表示区域的行数R和列数C(1<=R,C<=100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。




输出描述


输出最长区域的长度。




输入样例


5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9




输出样例

25


#include<stdio.h>int main(){int  r , c , i , j , p , q , max = -1 , a[101][101] , count = 0 ;scanf("%d%d",&r,&c);for(i = 1; i < r+1 ; i++)for(j = 1; j < c+1; j++){scanf("%d",&a[i][j]);if(a[i][j] > max){max = a[i][j];p = i;q = j;}}//printf("p = %d  q = %d\n",p , q);for(i = 0 ; i < r+2; i++ ){a[i][0] = 10001;a[i][c+1] = 10001;}for(j = 0 ; j < c+2; j++ ){a[0][j] = 10001;a[r+1][j] = 10001;}while(1){max = 10001;if((a[p-1][q] > a[p][q]) && (a[p+1][q] > a[p][q]) && (a[p][q-1] > a[p][q]) && (a[p][q+1] > a[p][q])){printf("%d",++count);break;}i = a[p][q] - a[p-1][q];j = a[p][q] - a[p+1][q];r = a[p][q] - a[p][q-1];c = a[p][q] - a[p][q+1];if((i >= 0) && (i <= max))max = i;if((j >= 0) && (j <= max))max = j;if((r >= 0) && (r <= max))max = r;if((c >= 0) && (c <= max))max = c;if(max == i){a[p][q] = 10001;p = p - 1;count++;//printf("i = %d   a[p][q] = %d  count = %d\n", i, a[p][q] ,count);}if(max == j){a[p][q] = 10001;p = p + 1;count++;//printf("j = %d   a[p][q] = %d  count = %d\n", j, a[p][q] ,count);}if(max == r){a[p][q] = 10001;q = q - 1;count++;//printf("r = %d   a[p][q] = %d  count = %d\n", r, a[p][q] ,count);}if(max == c){a[p][q] = 10001;q = q + 1;count++;//printf("c = %d   a[p][q] = %d  count = %d\n", c, a[p][q] ,count);}}return 0;}


1 0
原创粉丝点击