• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

跳跃|蓝桥杯

武飞扬头像
Lewis_1231
帮助1

跳跃|蓝桥杯

题目链接https://www.lanqiao.cn/problems/553/learning/

学新通
学新通
学新通
题目分析
这道题数据结构不大,可以采用dfs;
也可以dp;
能走的距离只有9个,向斜下方走两格不可以(虽然是距离小于三),这里题目好像有些问题

代码

dfs

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int squ[101][101];
int book[101][101];
int dir[9][2] = { 1,0,2,0,3,0,0,1,0,2,0,3,1,1,1,2,2,1 };
int m, n;
int maxx = -1;
void dfs(int x, int y,int sum)
{
	if (x == n && y == m)
	{
		if (sum > maxx)maxx = sum;
		return;
	}
	for (int k = 0; k < 9; k  )
	{
		int nx = x   dir[k][0];
		int ny = y   dir[k][1];
		if (nx >= 1 && ny >= 1 && nx <= n && ny <= m&&!book[nx][ny])
		{
			book[nx][ny] = 1;
			dfs(nx, ny, sum   squ[nx][ny]);
			book[nx][ny] = 0;
		}
	}
}
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i  )
		for (int j = 1; j <= m; j  )
			cin >> squ[i][j];
	book[1][1] = 1;
	dfs(1, 1, -4);
	cout << maxx;
	return 0;
}
学新通

dp

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int squ[101][101];
int dp[101][101];
int dir[9][2] = { 1,0,2,0,3,0,0,1,0,2,0,3,1,1,1,2,2,1 };
int main()
{
	int m, n;
	cin >> n >> m;
	for (int i = 1; i <= n; i  )
		for (int j = 1; j <= m; j  )
			cin >> squ[i][j];
	dp[1][1] = squ[1][1];
	for(int i=1;i<=n;i  )
		for(int j=1;j<=m;j  )
			for (int k = 0; k < 9; k  )
			{
				int nx = i   dir[k][0];
				int ny = j   dir[k][1];
				if (nx >= 1 && ny >= 1 && nx <= n && ny <= m)
					dp[nx][ny] = max(dp[nx][ny], dp[i][j]   squ[nx][ny]);
			}
	cout << dp[n][m];
	return 0;
}
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfibjfj
系列文章
更多 icon
同类精品
更多 icon
继续加载