1. 程式人生 > 實用技巧 >百練1088滑雪

百練1088滑雪

用的遞推

首先要注意需要排序(結構體排序用sort+<的過載)

其實又開了一個數組用來存數...只不過結構體排序能方便很多

 1 #include <iostream>
 2 #include <string>
 3 #include <string.h>
 4 #include <vector>
 5 #include <time.h>
 6 #include <algorithm>
 7 
 8 using namespace std;
 9 
10 //#define DEBUG(x) cout << #x << " = " << x << endl
11 using namespace std; 12 const int MAXN=110; 13 int R,C; 14 int field[MAXN][MAXN]; 15 int len[MAXN][MAXN]; 16 ///將所有點從小到大排序 17 struct Point{ 18 int h, i, j; 19 Point(){} 20 Point(int h,int i,int j){ 21 this->h=h; 22 this->i=i; 23 this->j=j; 24 } 25 bool
operator<(const Point &p)const 26 { 27 return h<p.h; 28 } 29 }; 30 int PN=0; 31 Point points[MAXN*MAXN]; 32 int main() 33 { 34 scanf("%d %d",&R,&C); 35 for(int i=0;i<R;i++){ 36 for(int j=0;j<C;j++){ 37 int h; 38 scanf("%d",&h);
39 field[i][j]=h; 40 points[PN++]=Point(h,i,j); 41 len[i][j]=1; 42 } 43 } 44 sort(points,points+PN); 45 int r=-1; 46 ///人人為我型遞推 47 for(int k=0;k<PN;k++){ 48 int i=points[k].i, 49 j=points[k].j, 50 h=points[k].h; 51 if(i+1<R&&field[i+1][j]<field[i][j])len[i][j]=max(len[i][j],len[i+1][j]+1); 52 if(i-1>=0&&field[i-1][j]<field[i][j])len[i][j]=max(len[i][j],len[i-1][j]+1); 53 if(j+1<C&&field[i][j+1]<field[i][j])len[i][j]=max(len[i][j],len[i][j+1]+1); 54 if(j-1>=0&&field[i][j-1]<field[i][j])len[i][j]=max(len[i][j],len[i][j-1]+1); 55 r=max(r,len[i][j]); 56 } 57 printf("%d\n",r); 58 return 0; 59 }