首页 > 编程语言 >蓝桥杯十五届软件赛C++B组题解

蓝桥杯十五届软件赛C++B组题解

时间:2024-09-19 21:24:45浏览次数:3  
标签:15 int 题解 ll C++ 蓝桥 using return include

最近蓝桥杯官网已经把十五届题目上架了,我会尽快的将题解发出来,没有发的过段时间再补。

​​​​​​​数字接龙

一个很鹅心的搜索题,一不注意就会写错,比赛的时候写不来,题目上架后也WA了两个样例才过。

题目大意:

也就是说从(1,1)开始 ,下一步路的数据总是要比当前数据大1,超过k就取模,然后还不能交叉走路,如第四点所说,一个田字格里面,从1走到3后,便不能从2到4,也不能从4到2,每个格子只能走一次。

思路:

由于数据比较小,而且还有特判不能多走,所以可以选择暴力搜索,具体细节很多,从代码里面慢慢理解就行。

代码:

#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<stack>
#include<numeric>
#include<stdlib.h>

using namespace std;
using ll = long long;

const ll mod = 1e9 + 7, N = 2e5 + 5, inf = 2e18;

int n, k;
int a[15][15];
bool vis[15][15];
bool ban[15][15][15][15];//禁止通行

int xx[] = {-1, -1, 0, 1, 1, 1, 0, -1};//路径
int yy[] = {0, 1, 1, 1, 0, -1, -1, -1};

string ans = "9";

bool in_map(int x, int y) {
	if (x <= n && x >= 1 && y <= n && y >= 1)return true;
	return false;
}

void dfs(int x, int y, int cnt, string teep) {
	if (x == n && y == n && teep.size() == n * n - 1) {
		ans = min(ans, teep);
		return ;
	}
	//cout<<1;
	//cout << teep << '\n';
	for (int i = 0; i < 8; i++) {
		int nx = x + xx[i], ny = y + yy[i];
		//不在地图
		if (!in_map(nx, ny))continue;
		//数据不符合
		if (a[nx][ny] != (cnt + 1) % k)continue;
		//走过了
		if (vis[nx][ny])continue;

		if (i & 1) { //斜着走
			if (i == 1 && !ban[x][y + 1][x - 1][y] && !ban[x - 1][y][x][y + 1]) { //没有禁令
				vis[nx][ny] = true;
				ban[x][y + 1][x - 1][y] = ban[x - 1][y][x][y + 1] = true;
				ban[x][y][nx][ny]=ban[nx][ny][x][y]=true;
				string new_teep = teep;
				teep += ('0' + i); //不太会转换
				dfs(nx, ny, a[nx][ny], teep);
				teep = new_teep;
				//回溯
				ban[x][y][nx][ny]=ban[nx][ny][x][y]=false;
				ban[x][y + 1][x - 1][y] = ban[x - 1][y][x][y + 1] = false;
				vis[nx][ny] = false;

			} else if (i == 3 && !ban[x + 1][y][x][y + 1] && !ban[x][y + 1][x + 1][y]) {
				vis[nx][ny] = true;
				ban[x + 1][y][x][y + 1] = ban[x][y + 1][x + 1][y] = true;
				ban[x][y][nx][ny]=ban[nx][ny][x][y]=true;
				
				string new_teep = teep;
				teep += ('0' + i); //不太会转换
				dfs(nx, ny, a[nx][ny], teep);
				teep = new_teep;
				//回溯
				ban[x][y][nx][ny]=ban[nx][ny][x][y]=false;
				ban[x + 1][y][x][y + 1] = ban[x][y + 1][x + 1][y] = false;
				vis[nx][ny] = false;
			} else if (i == 5 && !ban[x][y - 1][x + 1][y] && !ban[x + 1][y][x][y - 1]) {
				vis[nx][ny] = true;
				ban[x][y - 1][x + 1][y] = ban[x + 1][y][x][y - 1] = true;
				ban[x][y][nx][ny]=ban[nx][ny][x][y]=true;
				
				string new_teep = teep;
				teep += ('0' + i); //不太会转换
				dfs(nx, ny, a[nx][ny], teep);
				teep = new_teep;
				//回溯
				ban[x][y][nx][ny]=ban[nx][ny][x][y]=false;
				ban[x][y - 1][x + 1][y] = ban[x + 1][y][x][y - 1] = false;
				vis[nx][ny] = false;
			} else if (i == 7 && !ban[x][y - 1][x - 1][y] && !ban[x - 1][y][x][y - 1]) {
				vis[nx][ny] = true;
				ban[x][y - 1][x - 1][y] = ban[x - 1][y][x][y - 1] = true;
				ban[x][y][nx][ny]=ban[nx][ny][x][y]=true;
				
				string new_teep = teep;
				teep += ('0' + i); //不太会转换
				dfs(nx, ny, a[nx][ny], teep);
				//回溯
				teep = new_teep;
				ban[x][y][nx][ny]=ban[nx][ny][x][y]=false;
				ban[x][y - 1][x - 1][y] = ban[x - 1][y][x][y - 1] = false;
				vis[nx][ny] = false;
			}
		} else { //横着走
			vis[nx][ny] = true;
			string new_teep = teep;
			teep += ('0' + i); //不太会转换
			dfs(nx, ny, a[nx][ny], teep);
			teep = new_teep;
			vis[nx][ny] = false;
		}
	}

}

void solve() {
	cin >> n >> k;

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> a[i][j];
		}
	}
	string x = "";
	vis[1][1] = true;
	dfs(1, 1, a[1][1], x);
	cout << (ans == "9" ? "-1" : ans) << '\n';
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t = 1;
	// cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

握手问题

题目大意:

总共有 5050 人参加了本次会议。每个人要与其他人都握手一次,问至少握手多少次。

思路:

写一个二维数组,双重循环遍历,i与j握手后将数组a[i][j]标记为1,数组a[j][i]标记为2,如果a[i][j]已经是2了,就不用更改为1了,最后再遍历一次,输出1的个数就行。

代码:

#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<stack>
#include<numeric>
#include<stdlib.h>
#include<bits/stdc++.h>
using namespace std;
using ll = long long;

const ll mod = 1e9 + 7, N = 2e5 + 5, inf = 2e18;


void solve() {
     cout<<1204;
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t = 1;
	// cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

标签:15,int,题解,ll,C++,蓝桥,using,return,include
From: https://blog.csdn.net/2401_82683560/article/details/142369835

相关文章

  • P2051 [AHOI2009] 中国象棋 题解
    DP好题?首先确定,每一行/列只能放至多两个棋子,这么少,所以我们的状态肯定和棋子数有关。由于我们不关注具体的方案数,所以我们不妨只关心对应棋子数量的行/列的数量。同时,由于考虑行和列都是一样的,所以我们不妨用行递推。所以我们设$\dp_{i,j,k}\$表示当前放到第\(i\)行,有\(......
  • C++内存管理
    前言接下来我们继续学习C++的内存管理。1. C++/C中程序内存区域划分C++将内存区域划分为四个部分,分别是栈区,堆区,静态区,常量区。其中静态区又叫数据段,其中存储全局数据和动态数据。常量区又叫代码区,其中存储只读常量和可执行代码。栈里面存储函数参数,返回值,局部变量等。......
  • C++笔记21•C++11的新特性•
       相比于 C++98/03,C++11则带来了数量可观的变化,其中包含了约140个新特性,以及对C++03标准中约600个缺陷的修正,这使得C++11更像是从C++98/03中孕育出的一种新语言。相比较而言,C++11能更好地用于系统开发和库开发、语法更加泛华和简单化、更加稳定和安全,不仅功能更强大,而......
  • C++入门基础知识76(实例)——实例 1【输出 “Hello, World!“】
    成长路上不孤单......
  • C++刷怪笼(6)模板初阶
    1.前言在学习C++模板之前,我们会被同种函数的不同数据类型的繁琐写法而折磨,今天我们进入对模板的学习,来进一步的感受C++为我们今后的编程学习和工作所带来的便利。2.模板2.1泛型编程我们应该如何去实现一个所有数据类型通用的函数?voidSwap(int&left,int&right){i......
  • 【c++基础知识——&引用的深度理解】
    C++引用深度理解对于一个函数来说,传值和传引用,在函数功能上没有区别,但在性能和副作用方面有显著差异。传值当按值传递参数时,函数会创建参数的一个副本。这样做的好处是函数内部对参数的修改不会影响原始变量,但缺点是对于大对象来说,拷贝操作会带来性能开销。传引用......
  • 深度长文:揭开C/C++三目运算符的全部秘密,助你写出更优雅的代码(上)
    在编程中,简洁和高效是程序员永恒追求的目标。当我们面对条件判断时,通常第一反应是使用if-else语句——这是最为常见的选择。然而,C和C++中还有一种非常简洁优雅的条件判断方式——三目运算符(TernaryOperator)。也许你曾经在一些代码中见到它,简短的?:语法,但却不知道它的工作原理......
  • Capital许可使用常见问题解答
    在使用Capital软件的过程中,许多用户可能会遇到关于许可使用的各种问题。为了帮助大家更好地理解和合规使用Capital软件,我们整理了一份常见问题解答,希望能为您提供有价值的参考。一、Capital许可证的类型有哪些?Capital提供多种许可证类型,包括永久许可证、订阅许可证等。永久许可......
  • 【题解】Solution Set - NOIP2024集训Day32 数位 dp
    【题解】SolutionSet-NOIP2024集训Day32数位dphttps://www.becoder.com.cn/contest/5537order:1,3,5,6,2,4「SDOI2013」淘金就是要算前\(k\)大的和。考虑一个位置\((i,j)\)在变化完了之后的金子个数。(也即逆变换。设:\(f^\prime(x)\)表示在\(1\simN\)范围内,数位......
  • 【洛谷 P5730】【深基5.例10】显示屏 题解(数组+循环)
    【深基5.例10】显示屏题目描述液晶屏上,每个阿拉伯数字都是可以显示成的点阵的(其中X表示亮点,.表示暗点)。现在给出数字位数(不超过)和一串数字,要求输出这些数字在显示屏上的效果。数字的显示方式如同样例输出,注意每个数字之间都有一列间隔。输入格式第一行输入一个正整数,表示数......