题目链接:https://www.tyvj.cn/Problem_Show.aspx?id=1004
记忆化搜索
1 #include <cstdio> 2 #include <cstdlib> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 int m[102][102], dp[102][102], r, C; 7 int dfs(int x, int y) { 8 if (dp[x][y] > 0) return dp[x][y]; dp[x][y]=1; int a,b,c,d; a=b=c=d=0; 9 if (x-1>=1 && m[x-1][y] < m[x][y]) a = dfs(x-1,y); 10 if (x+1<=r && m[x+1][y] < m[x][y]) b = dfs(x+1,y); 11 if (y-1>=1 && m[x][y-1] < m[x][y]) c = dfs(x,y-1); 12 if (y+1<=C && m[x][y+1] < m[x][y]) d = dfs(x,y+1); 13 dp[x][y] = max(max(a,b),max(c,d))+1; return dp[x][y]; 14 } 15 int main(void) { 16 int Max; 17 freopen("in.txt","r",stdin); 18 scanf("%d%d",&r, &C); 19 for (int i = 1; i <= r; ++i) for (int j = 1; j <= C; ++j) scanf("%d", &m[i][j]), dp[i][j] = 0; Max=1; 20 for (int i = 1; i <= r; ++i) for (int j = 1; j <= C; ++j) {dp[i][j] = dfs(i, j); if (dp[i][j]>Max) Max=dp[i][j];} 21 for (int i = 1; i <= r; ++i) { 22 for (int j = 1; j <= C; ++j) printf("%d ", dp[i][j]); printf("\n"); 23 } 24 printf("%d\n", Max); 25 26 return 0; 27 }
因为全局变量和局部变量冲突错了很久。