标签:str span algo long mem pac const div pre
题意:
给出一个矩阵,找出这个矩阵中严格最长下降序列的长度,可以从上下左右四个方向下降。
思路:
记忆化搜索一遍即可。
代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <string> 5 #include <iostream> 6 using namespace std; 7 const int N = 100 + 5; 8 const int dx[4] = {0,0,1,-1},dy[4] = {1,-1,0,0}; 9 int a[N][N],dp[N][N]; 10 int n,m; 11 int dfs(int x,int y) 12 { 13 if (~dp[x][y]) return dp[x][y]; 14 int ans = 0; 15 for (int i = 0;i < 4;i++) 16 { 17 int nx = x + dx[i],ny = y + dy[i]; 18 if (nx < 0) continue; 19 if (ny < 0) continue; 20 if (nx >= n) continue; 21 if (ny >= m) continue; 22 if (a[nx][ny] < a[x][y]) 23 { 24 ans = max(dfs(nx,ny),ans); 25 } 26 } 27 return dp[x][y] = ans + 1; 28 } 29 int main() 30 { 31 int t; 32 scanf("%d",&t); 33 while (t--) 34 { 35 string s; 36 cin >> s; 37 scanf("%d%d",&n,&m); 38 memset(dp,-1,sizeof(dp)); 39 for (int i = 0;i < n;i++) 40 { 41 for (int j = 0;j < m;j++) 42 { 43 scanf("%d",&a[i][j]); 44 } 45 } 46 for (int i = 0;i < n;i++) 47 { 48 for (int j = 0;j < m;j++) 49 { 50 dfs(i,j); 51 } 52 } 53 int ans = 0; 54 for (int i = 0;i < n;i++) 55 { 56 for (int j = 0;j < m;j++) 57 { 58 ans = max(ans,dp[i][j]); 59 } 60 } 61 cout << s << ": " << ans << endl; 62 } 63 return 0; 64 }
uva 10285 Longest Run on a Snowboard
标签:str span algo long mem pac const div pre
原文地址:https://www.cnblogs.com/kickit/p/8977719.html