第四届蓝桥杯省赛C++A组 振兴中华

21 篇文章 1 订阅

标题:振兴中华

小明参加了学校的趣味运动会,其中的一个项目是:跳格子。

地上画着一些格子,每个格子里写一个字,如下所示:(也可参见p1.jpg)

从我做起振
我做起振兴
做起振兴中
起振兴中华

比赛时,先站在左上角的写着“从”字的格子里,可以横向或纵向跳到相邻的格子里,但不能跳到对角的格子或其它位置。一直要跳到“华”字结束。

要求跳过的路线刚好构成“从我做起振兴中华”这句话。

请你帮助小明算一算他一共有多少种可能的跳跃路线呢?

答案是一个整数,请通过浏览器直接提交该数字。
注意:不要提交解答过程,或其它辅助说明类的内容。

 

答案:35

思路:dfs,因为只能向右或向下走,所以我们可以用(4,5)作为终点,也可以用步数作为终点。

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<map>  
#include<set>
using namespace std;

int x[2] = { 1,0 }, y[2] = { 0,1 };
int vis[4][5] = { 0 };
int ans = 0;

void dfs(int a,int b)
{
	if (a > 4 || b > 5)
		return;
	if (a == 4 && b == 5)
	{
		ans++;
		return;
	}
	for (int i = 0; i < 2; i++)
	{
		int c = a + x[i];
		int d = b + y[i];
		if (c >= 1 && c <= 4 && d >= 1 && d <= 5)
		{
			if (!vis[c - 1][d - 1])
				vis[c - 1][d - 1] = 1;
			dfs(c, d);
			vis[c - 1][d - 1] = 0;
		}
	}
}
int main()
{
	vis[0][0] = 1;
	dfs(1, 1);
	cout << ans << endl;
	return 0;
}

PS:用步数作为dfs终点的出处:https://blog.csdn.net/u010625743/article/details/44630055

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<map>  
#include<set>
using namespace std;

int a[4][5];
int sum;
void dfs(int row, int col, int index)
{
	if (a[row][col] == index && index == 7)
		sum++;
	else
 	{
		if (row + 1<4)
			dfs(row + 1, col, index + 1);
		if (col + 1<5)
			dfs(row, col + 1, index + 1);
	}
}
int main()
{
	int row, col;
	for (row = 0; row<4; row++)
		for (col = 0; col<5; col++)
			a[row][col] = row + col;

	dfs(0, 0, 0);
	cout<<sum<<endl;
	return 0;
}

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值