【蓝桥杯】第八届蓝桥杯(2017)-省赛题解_C/C++__大学A组
【蓝桥杯】第八届蓝桥杯(2017)-省赛题解_C/C++__大学A组
文章目录
题目就不贴了,记录一下自己做题的思路和代码.
详细题目链接
1、迷宫(DFS)
解题思路:
代码
2、跳蚱蜢(BFS)
解题思路:
代码
3、魔方状态(不会)
解题思路
4、方格分割(DFS,不会)
解题思路
5,6代码填空题(略)
7、正则问题(DFS)
解题思路
代码
题目就不贴了,记录一下自己做题的思路和代码.
详细题目链接
1、迷宫(DFS)
DFS依次判断每一个点能否走出去。答案:31
#include
#include
using namespace std;
const int N = 11;
int n = 10;
char g[N][N];
bool st[N][N];
int dx[4] = {-1,0,1,0},dy[4] = {0,1,0,-1};
int get(char c)
{
if(c == 'U') return 0;
else if(c == 'R') return 1;
else if(c == 'D') return 2;
else return 3;
}
bool dfs(int x,int y)
{
if(x <0 || x >= n || y < 0 || y >= n) return true;
st[x][y] = true;
int d = get(g[x][y]);
int a = x + dx[d], b = y + dy[d];
if(st[a][b]) return false;
if(dfs(a,b)) return true;
}
int main()
{
for(int i =0; i < n ;i ++ ) cin >> g[i];
int cnt = 0;
for(int i =0 ;i < n;i ++ )
for(int j = 0;j < n ; j ++ )
{
memset(st,0,sizeof st);
if(dfs(i,j)) cnt ++;
}
cout << cnt << endl;
}
// 答案:31
2、跳蚱蜢(BFS)
答案:20
#include
#include
#include
using namespace std;
int get_0(string &s)
{
for(int i = 0; i < s.size();i ++ )
if(s[i] == '0') return i;
}
int bfs()
{
string start = "012345678";
string end = "087654321";
unordered_map step;
int d[4] = {1,2,-1,-2};
step[start] = 0;
queue q;
q.push(start);
while(q.size())
{
string s = q.front();
q.pop();
int cnt = step[s];
if(s == end) return cnt;
int pos = get_0(s);
for(int i = 0;i < 4;i ++)
{
swap(s[pos],s[((pos + d[i]) % 9 + 9) % 9 ]) ;// C ++ 取模
if(step.count(s) == 0)
{
step[s] = cnt + 1;
q.push(s);
}
swap(s[pos],s[((pos + d[i]) % 9 + 9) % 9 ]) ;// 交换回来
}
}
}
int main()
{
cout << bfs() << endl;
return 0;
}
3、魔方状态(不会)
附上其他大佬链接,看样子也是BFS,题目太难不具有参考性了
4、方格分割(DFS,不会)
附其他人题解
5,6代码填空题(略)
7、正则问题(DFS)
递归处理。想到了dfs返回 ()内x的数量,但是具体细节还是写不出来,还得学习y总的手法。
#include
using namespace std;
int k;
string s;
int dfs() // dfs返回一个()的x
{
int res = 0;
while(k < s.size())
{
if(s[k] == '(')
{
k ++; // 跳过 (
res += dfs();
k ++; // 跳过 )
}else if(s[k] == '|')
{
k ++ ; // 跳过 |
res = max(res,dfs());
}else if(s[k] == ')') break;
else if(s[k] == 'x')
{
k ++;
res ++;
}
}
return res;
}
int main()
{
cin >> s;
cout << dfs() << endl;
return 0;
}
【蓝桥杯】第八届蓝桥杯(2017)-省赛题解_C/C++__大学A组相关教程
【蓝桥杯】k-进制数
【蓝桥杯】k-进制数 【蓝桥杯】k-进制数 题目链接: https://www.dotcpp.com/oj/problem1117.html 思路: 有效数字条件:① 有效数字的首位不能为 0 ,② 有效数字中不能有连续的 0 ,③ k 进制的数位上只能是 0~k-1。 首先假设 N = 1,即只有一位数字,它包
蓝桥杯第六届牌型种数(Java)
蓝桥杯第六届牌型种数(Java) 暴力: public class Main { public static void main(String[] args) { int[] a = new int[13]; int count = 0; for (a[0] = 0; a[0] = 4; a[0]++) for (a[1] = 0; a[1] = 4; a[1]++) for (a[2] = 0; a[2] = 4; a[2]++) for (a
[蓝桥杯2017初赛]日期问题 (思路)
[蓝桥杯2017初赛]日期问题 (思路) #includeiostream#includestdio.h#includestring.h#includemath.h#includealgorithm#includesstream#define ll long long#includesetusing namespace std;bool is(int x) ///判断是否是闰年 {if(x%400==0||x%4==0x%100!=0)
VS 2017 Git failed with a fatal error的解决办法
VS 2017 Git failed with a fatal error的解决办法 问题现象 这几天用刚装的VS2017写了个程序,在提交推送到Github过程中提示了“Git failed with a fatal error”错误,输出窗口的提示如下图: Vs2017提交Github报错 解决方案 经过一番折腾后再网上找到如下
2017年最新企业面试题之shell(四)
2017年最新企业面试题之shell(四) 2017年最新企业面试题之shell(四) 一、抽签脚本(1)要求如下: 1、写一个脚本执行后,输入名字,产生随机数01-99之间的数字; 2、输入名字后,屏幕输出信息,并将名字和数字记录到文件里; 3、如果相同的名字重复输入
蓝桥杯第八届省赛A组c/c++
蓝桥杯第八届省赛A组c/c++ A. 迷宫 X星球的一处迷宫游乐场建在某个小山坡上。它是由10x10相互连通的小房间组成的。 房间的地板上写着一个很大的字母。 我们假设玩家是面朝上坡的方向站立,则: L表示走到左边的房间, R表示走到右边的房间, U表示走到上坡方
蛇行矩阵求解的C语言实现-蓝桥杯
蛇行矩阵求解的C语言实现-蓝桥杯 主要思路是找到行和列的规律,再用for循环的嵌套求解。 #includestdio.hint main(void){int n;scanf(%d,n);static int a=1;static int b=1;for(int row=1;rown+1;row++){a+=row-1;printf(%d,a);b=a;for(int i=2;in-row+2;i++)
第8天 VS2017动态链接库(.dll)的生成与使用和C的标准库
第8天 VS2017动态链接库(.dll)的生成与使用和C的标准库 动态库DLL 这里以VS2017为例子,讲解一下动态链接库(.dll)的生成与使用。 一、动态链接库(.dll)的生成 1、打开:“文件”-“新建”-“项目” 2、打开:“已安装”-“模板”-“Visual C++” -“win