记忆化搜索啊
或者直接对高度排序啊,这样就会先更新步数少的,后更新步数多的了,就可以直接利用先前已经得到的值了
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <queue> 5 #include <vector> 6 #define max(x, y) (x > y ? x : y) 7 #define min(x, y) (x > y ? y : x) 8 #define INF 0x3f3f3f3f 9 #define MOD 1000000007 10 #define Yes printf("Yes ") 11 #define No printf("No ") 12 using namespace std; 13 typedef long long LL; 14 typedef pair<int, int> PII; 15 16 int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}; 17 18 int h, w; 19 const int maxn = 1e2 + 10; 20 21 int hei[maxn][maxn]; 22 int ans[maxn][maxn]; 23 24 struct node { 25 int x, y, h; 26 }map[maxn * maxn]; 27 28 29 bool cmp(const node& A, const node& B) { 30 return A.h < B.h; 31 } 32 33 int main(int argc, const char * argv[]) { 34 int cnt = 0; 35 scanf("%d%d", &h, &w); 36 for (int i = 1; i <= h; i++) { 37 for (int j = 1; j <= w; j++) { 38 scanf("%d", &hei[i][j]); 39 map[cnt].x = i, map[cnt].y = j, map[cnt].h = hei[i][j]; 40 cnt++; 41 ans[i][j] = 1; 42 } 43 } 44 sort(map, map + cnt, cmp); 45 for (int i = 0; i < cnt; i++) { 46 int x = map[i].x, y = map[i].y; 47 for (int j = 0; j < 4; j++) { 48 int nx = x + dx[j], ny = y + dy[j]; 49 if (hei[x][y] > hei[nx][ny]) { 50 ans[x][y] = max(ans[x][y], ans[nx][ny] + 1); 51 } 52 } 53 } 54 int res = 0; 55 for (int i = 1; i <= h; i++) { 56 for (int j = 1; j <= w; j++) { 57 res = max(res, ans[i][j]); 58 } 59 } 60 printf("%d ", res); 61 return 0; 62 }