Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 68355 | Accepted: 25187 |
Description
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更長。事實上,這是最長的一條。
Input
Output
Sample Input
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
Sample Output
25
代碼:
#include<stdio.h> #include<string.h> int a[110][110],vis[110][110]; int R,C; int f(int i,int j) { int tem=0,max=0; if(i<1 || i>R || j<1 || j>C)//超出邊界,直接返回; return 0; if(vis[i][j])//如果已訪問,則返回; return vis[i][j]; if(a[i][j]>a[i-1][j]) {//向上比較; tem=f(i-1,j); max=max<tem?tem:max; } if(a[i][j]>a[i+1][j]) {//向下比較; tem=f(i+1,j); max=max<tem?tem:max; } if(a[i][j]>a[i][j-1]) {//向左比較; tem=f(i,j-1); max=max<tem?tem:max; } if(a[i][j]>a[i][j+1]) {//向右比較; tem=f(i,j+1); max=max<tem?tem:max; } return vis[i][j]=max+1; } int main() { int i,j,tem,max; while(scanf("%d %d",&R,&C)!=EOF) { tem=max=0; memset(a,0,sizeof(a)); memset(vis,0,sizeof(vis)); for(i=1;i<=R;i++) for(j=1;j<=C;j++) scanf("%d",&a[i][j]); for(i=1;i<=R;i++) for(j=1;j<=C;j++) { tem=f(i,j); max=max>tem?max:tem; } printf("%d\n",max); } return 0; }