蓝桥杯省赛模拟题 滑行

题目大意

小蓝准备在一个空旷的场地里面滑行,这个场地的高度不一,小蓝用一个 n n n m m m 列的矩阵来表示场地,矩阵中的数值表示场地的高度。

如果小蓝在某个位置,而他上、下、左、右中有一个位置的高度(严格)低于当前的高度,小蓝就可以滑过去,滑动距离为 1 1 1

如果小蓝在某个位置,而他上、下、左、右中所有位置的高度都大于等于当前的高度,小蓝的滑行就结束了。

小蓝不能滑出矩阵所表示的场地。

小蓝可以任意选择一个位置开始滑行,请问小蓝最多能滑行多远距离。

输入第一行包含两个整数 n , m n, m n,m ,用一个空格分隔。

接下来 n n n 行,每行包含 m m m 个整数,相邻整数之间用一个空格分隔,依次表示每个位置的高度。

输出一行包含一个整数,表示答案。

对于 30 30% 30 评测用例, 1 < = n < = 20 , 1 < = m < = 20 , 0 < = 高度 < = 100 1 <= n <= 20,1 <= m <= 20,0 <= 高度 <= 100 1<=n<=201<=m<=200<=高度<=100

对于所有评测用例, 1 < = n < = 100 , 1 < = m < = 100 , 0 < = 高度 < = 10000 1 <= n <= 100,1 <= m <= 100,0 <= 高度 <= 10000 1<=n<=1001<=m<=1000<=高度<=10000

解题思路

本题考查记忆化搜索,可用一个数组 f f f 用于记录从某个点开始能够滑行的最大距离,枚举所有的点,取从所所有点开始滑行的最大高度输出即可。调用 d f s ( x ) dfs(x) dfs(x) 时:

  • 若该点已被遍历过,则直接返回 f ( x , y ) f(x, y) f(x,y) 即可。
  • 若该点未被遍历过,则先判断可以滑向哪个方向,即下一个点的高度应低于当前点的高度,递归处理四个方向,取四个方向开始滑行的最大值,之后将值存入 f ( x , y ) f(x, y) f(x,y) 下次调用时,无需再次递归,直接返回即可。

AC_Code

  • C++
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;

const int N = 1e2 + 10;

int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

int n, m;
int g[N][N], f[N][N];

int dfs(int x, int y)
{
    if (f[x][y] != -1)
        return f[x][y];
    
    int res = 0;
    for (int i = 0; i < 4; ++ i )
    {
        int tx = x + dx[i], ty = y + dy[i];
        if (tx <= 0 || ty <= 0 || tx > n || ty > m || g[tx][ty] >= g[x][y])
            continue;
        res = max(res, dfs(tx, ty));
    }
    
    f[x][y] = res + 1;
    return f[x][y];
}

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; ++ i )
        for (int j = 1; j <= m; ++ j )
            cin >> g[i][j];
    
    memset(f, -1, sizeof f);
    
    int res = 0;
    for (int i = 1; i <= n; ++ i )
        for (int j = 1; j <= m; ++ j )
            res = max(res, dfs(i, j));
    cout << res << endl;
    
    return 0;
}
  • Java
import java.util.Scanner;

public class main {
	
	static int n, m;
	static int[][] g = new int[110][110];
	static int[][] f = new int[110][110];
	static int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
	
	static int dfs(int x, int y) {
		if (f[x][y] != -1)
	        return f[x][y];
	    
	    int res = 0;
	    for (int i = 0; i < 4; ++ i )
	    {
	        int tx = x + dx[i], ty = y + dy[i];
	        if (tx <= 0 || ty <= 0 || tx > n || ty > m || g[tx][ty] >= g[x][y])
	            continue;
	        res = Math.max(res, dfs(tx, ty));
	    }
	    
	    f[x][y] = res + 1;
	    return f[x][y];
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		m = sc.nextInt();
		
		for (int i = 1; i <= n; ++ i )
	        for (int j = 1; j <= m; ++ j )
	        	g[i][j] = sc.nextInt();
		
		for (int i = 1; i <= n; ++ i )
	        for (int j = 1; j <= m; ++ j )
	        	f[i][j] = -1; 
		int res = 0;
		for (int i = 1; i <= n; ++ i )
	        for (int j = 1; j <= m; ++ j )
	        	res = Math.max(res, dfs(i, j));
		System.out.println(res);
	}
	
}
  • Python
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]

g = list()
f = [[-1] * 110 for i in range(110)]

def dfs(x, y):
    if f[x][y] != -1:
        return f[x][y]
    res = 0
    for i in range(4):
        tx = x + dx[i]
        ty = y + dy[i]
        if tx < 0 or ty < 0 or tx >= n or ty >= m or g[tx][ty] >= g[x][y]:
            continue
        res = max(res, dfs(tx, ty))
    f[x][y] = res + 1
    return f[x][y]

n, m = map(int, input().split())
for i in range(n):
    li = list(map(int, input().split()))
    g.append((li))

res = 0
for i in range(n):
    for j in range(m):
        res = max(res, dfs(i, j));
print(res)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值