2017年第八届蓝桥杯

在这里插入图片描述

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <algorithm>

using namespace std;
string map[12];
bool vis[12][12];
int ans;
void dfs(int x, int y)
{
	if (x < 0 || x > 9 || y < 0 || y > 9){//边界,走出去了
		ans ++;
		return ;
	}
	if (vis [x][y] == true) return ;
	vis[x][y] =true;//标记为走过 
	if (map[x][y] == 'U') dfs(x-1, y);
	if (map[x][y] == 'D') dfs(x+1, y);
	if (map[x][y] == 'L') dfs(x, y-1);
	if (map[x][y] == 'R') dfs(x, y+1);
	vis[x][y] = false;//复原为没走过 
}
int main()
{
	map[0] = "UDDLUULRUL";
	map[1] = "UURLLLRRRU";
	map[2] = "RRUURLDLRD";
	map[3] = "RUDDDDUUUU";
	map[4] = "URUDLLRRUU";
	map[5] = "DURLRLDLRL";
	map[6] = "ULLURLLRDU";
	map[7] = "RDLULLRDDD";
	map[8] = "UUDDUDUDLL";
	map[9] = "ULRDLUURRR";
	for (int i = 0; i < 10; i++){
		for (int j = 0; j < 10; j++){
			vis[i][j] = false;
			dfs(i, j);
		}
	}
	printf("%d", ans);
	return 0;	
}

答案是31

第五题
标题:字母组串

由 A,B,C 这3个字母就可以组成许多串。
比如:“A”,“AB”,“ABC”,“ABA”,“AACBB” …

现在,小明正在思考一个问题:
如果每个字母的个数有限定,能组成多少个已知长度的串呢?

他请好朋友来帮忙,很快得到了代码,
解决方案超级简单,然而最重要的部分却语焉不详。

请仔细分析源码,填写划线部分缺少的内容。

#include <stdio.h>

// a个A,b个B,c个C 字母,能组成多少个不同的长度为n的串。
int f(int a, int b, int c, int n)
{
if(a<0 || b<0 || c<0) return 0;
if(n==0) return 1;

return ______________________________________ ;  // 填空

}

int main()
{
printf("%d\n", f(1,1,1,2));
printf("%d\n", f(1,2,3,3));
return 0;
}

对于上面的测试数据,小明口算的结果应该是:
6
19

注意:只填写划线部分缺少的代码,不要提交任何多余内容或说明性文字。

发布了77 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/xingfushiniziji/article/details/103526177