|
题目传送门
设平面上的点(x,y)表示这个点最多能再往下滑的方块数
枚举周围的4个点,四个点求得的最大值加一就是这个点最多能再往下滑的方块数(这里是递归调用)
注意下边界,不能出了地图!
时间复杂度O(RC)
- #include <iostream>
- #include <cstring>
- using namespace std;
- int mp[105][105],r,c,dp[105][105];
- int fx[6][3] = {
- {1,0},
- {-1,0},
- {0,1},
- {0,-1},
- };
- int dfs(int x,int y){
- if (dp[x][y] != 0) return dp[x][y];
- int maxn = 1;
- for (int i = 0;i < 4;i++){
- int xx = x + fx[i][0];
- int yy = y + fx[i][1];
- if (xx >= 1 && yy >= 1 && xx <= r && yy <= c){
- if (mp[xx][yy] < mp[x][y]){
- maxn = max(maxn,dfs(xx,yy) + 1);
- }
- }
- }
- dp[x][y] = maxn;
- return dp[x][y];
- }
- int main(){
- cin >> r >> c;
- for (int i = 1;i <= r;i++){
- for (int j = 1;j <= c;j++){
- cin >> mp[i][j];
- }
- }
- int maxn = 0;
- for (int i = 1;i <= r;i++){
- for (int j = 1;j <= c;j++){
- maxn = max(maxn,dfs(i,j));
- }
- }
- cout << maxn;
- return 0;
- }
复制代码
|
|