ZISUOJ 数据结构--队列及其应用

说明:

        基本都是bfs的常见模板题型,思路都很直接,不过后面有两道题很搞心态,它们给的坐标x、y是反的,导致刚开始一直错。题目还是要看仔细,不能先入为主。

题目列表:

问题 A: 围圈报数(完善程序) 

参考题解:

#include<iostream>
#include<queue>
using namespace std;
int n,m,k=1,tmp;
queue<int> arr;
int main()
{cin>>n>>m;for(int i=1;i<=n;i++)arr.push(i);// _____(1)____//依次进入队列while(arr.size())// while(_____(2)_____)//判断队列里是否还有人{tmp=arr.front();if(k%m==0)cout<<tmp<<" ";elsearr.push(tmp);// ______(3)______//如果不是第m个人,则重新入队// _____(4)_____//从队列里删除arr.pop();k++;}return 0;
}

问题 B: 围圈报数

参考题解:

#include <iostream>
#include <queue>
using std::cin;
using std::cout;
int main() {cin.tie(nullptr)->sync_with_stdio(false);int n,m,count = 0;cin >> n >> m;std::queue<int> q;for(int i = 1;i<=n;i++) q.push(i);while(q.size()){auto t = q.front();count++;if(count%m==0) {cout << t << ' ';}else {q.push(t);}q.pop();}cout << std::endl;return 0;
}

问题 C: 报数相同次数circle

参考题解:

#include <iostream>
#include <queue>
using std::cin;
using std::cout;
int main() {cin.tie(nullptr)->sync_with_stdio(false);int n;cin >> n;int a,b;cin >> a >> b;std::queue<int> q1,q2;for(int i = 1;i<=a;i++) q1.push(i);for(int i = 1;i<=b;i++) q2.push(i);int count = 0;for(int i = 1;i<=n;i++) {auto t1 = q1.front(),t2 = q2.front();if(t1==t2) count++;q1.push(t1),q2.push(t2);q1.pop(),q2.pop();}cout << count << std::endl;return 0;
}

问题 D: 最小倍数

参考题解:

#include <iostream>
#include <queue>
using std::cin;
using std::cout;
using ll = long long;
ll n,x;
void bfs() {std::queue<ll> q;q.push(1);while(q.size()) {x = q.front();q.pop();if(x%n==0&&x>=n) {cout << x << '\n';return;}q.push(x*10);q.push(x*10+1);}cout << x << '\n';
}
int main() {cin.tie(nullptr)->sync_with_stdio(false);while(cin >> n,n) {bfs();}return 0;
}

问题 E: 迷宫的最短路径

参考题解:

#include <iostream>
#include <queue>
#include <cstring>
using std::cin;
using std::cout;
int main() {cin.tie(nullptr)->sync_with_stdio(false);constexpr int N = 1e2+5;int sx = 1,sy = 1,ex = 1,ey = 1;struct node {int x,y,s;}t,t1;char g[N][N];bool vis[N][N];memset(vis,false,sizeof vis);int dx[] = {0,0,-1,1};int dy[] = {-1,1,0,0};std::queue<node> q;int n,m;cin >> n >> m;for(int i = 1;i<=n;i++) {for(int j = 1;j<=m;j++) {cin >> g[i][j];if(g[i][j]=='S') {sx=i,sy=j;}else if(g[i][j]=='G') {ex=i,ey=j;}}}auto bfs = [&]()->void{t.x = sx,t.y = sy,t.s = 0;q.push(t);vis[sx][sy] = true;while(!q.empty()) {t = q.front();q.pop();if(t.x==ex&&t.y==ey) {cout << "The min steps are:" << t.s << "!\n";return;}for(int i = 0;i<4;i++) {int u = t.x+dx[i],v = t.y+dy[i];if(u<1||u>n||v<1||v>m||vis[u][v]||g[u][v]=='#') continue;vis[u][v] = true;t1.x = u,t1.y = v,t1.s = t.s+1;q.push(t1);}}cout << "sorry!\n";};bfs();return 0;
}

问题 F: 象棋中的马之进阶

参考题解:

#include <iostream>
#include <queue>
#include <cstring>
using std::cin;
using std::cout;
int main() {cin.tie(nullptr)->sync_with_stdio(false);constexpr int N = 15;struct node {int x,y,s;}t,t1;int dx[] = {-1,1,2,2,1,-1,-2,-2};int dy[] = {2,2,1,-1,-2,-2,-1,1};int sx,sy,ex,ey;bool vis[N][N];memset(vis,false,sizeof vis);cin >> sy >> sx >> ey >> ex;std::queue<node> q;auto bfs = [&]() ->void {t.x = sx,t.y = sy,t.s = 0;vis[sx][sy] = true;q.push(t);while(!q.empty()) {t = q.front();q.pop();if(t.x==ex&&t.y==ey) {cout << t.s << std::endl;return;}for(int i = 0;i<8;i++) {int u = t.x+dx[i],v = t.y+dy[i];if(u<1||u>10||v<1||v>9||vis[u][v]) continue;vis[u][v] = true;t1.x = u,t1.y = v,t1.s = t.s+1;q.push(t1);}}cout << 0 << std::endl;};bfs();return 0;
}

 

问题 G: 迷宫探险

参考题解:

#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using std::cin;
using std::cout;
int main() {cin.tie(nullptr)->sync_with_stdio(false);constexpr int N = 1e2+5;struct node {int x,y,s;bool operator > (const node &W) const {return s > W.s;}}t,t1;char g[N][N];bool vis[N][N];int dx[] = {0,0,-1,1};int dy[] = {-1,1,0,0};int n,sx = 1,sy = 1,ex = 1,ey = 1;std::priority_queue<node,std::vector<node>,std::greater<node>> pq;auto bfs = [&]() ->void {while(!pq.empty()) pq.pop();memset(vis,false,sizeof vis);t.x = sx,t.y = sy,t.s = 0;vis[sx][sy] = true;pq.push(t);while(!pq.empty()) {t = pq.top();pq.pop();if(t.x==ex&&t.y==ey) {cout << t.s << '\n';return;}for(int i = 0;i<4;i++) {int u = t.x+dx[i],v = t.y+dy[i];if(u<1||u>n||v<1||v>n||vis[u][v]||g[u][v]=='#') continue;vis[u][v] = true;int ds = 1;if(g[u][v]!='.') ds += int(g[u][v]^48);t1.x = u,t1.y = v,t1.s = t.s+ds;pq.push(t1);}}cout << -1 << '\n';};while(cin >> n) {ex = n,ey = n;for(int i = 1;i<=n;i++) {for(int j = 1;j<=n;j++) {cin >> g[i][j];}}bfs();}return 0;
}

问题 H: 迷宫

 

参考题解:

#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using std::cin;
using std::cout;
int main() {cin.tie(nullptr)->sync_with_stdio(false);constexpr int N = 1e2+5;struct node {int x,y,s;}t,t1;char g[N][N];bool vis[N][N];int dx[] = {0,0,-1,1};int dy[] = {-1,1,0,0};int sx,sy,ex,ey,k,n,m;std::queue<node> q;auto bfs = [&]()->void {memset(vis,false,sizeof vis);while(!q.empty()) q.pop();t.x = sx,t.y = sy,t.s = -1;vis[sx][sy] = true;q.push(t);while(!q.empty()) {t = q.front();q.pop();if(t.x==ex&&t.y==ey&&t.s<=k) {cout << "yes\n";return;}for(int i = 0;i<4;i++) {t1.x = t.x+dx[i],t1.y = t.y+dy[i];int &u = t1.x,&v = t1.y;while(u>=1&&u<=n&&v>=1&&v<=m&&g[u][v]=='.') {if(!vis[u][v]) {t1.s=t.s+1;vis[u][v] = true;q.push(t1);}u+=dx[i],v+=dy[i];}}}cout << "no\n";};int T = 1;cin >> T;while(T--) {cin >> n >> m;for(int i = 1;i<=n;i++) {for(int j = 1;j<=m;j++) {cin >> g[i][j];}}cin >> k >> sy >> sx >> ey >> ex;bfs();}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/1908.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

快速部署stable diffusion@Ubuntu

Stable Diffusion可以根据文本描述生成相关的图像&#xff0c;是当前最热门的文生图模型。 在Ubuntu下&#xff0c;可以选择快速安装&#xff0c;或者手动一步步安装。 快速安装 使用文档中的方法&#xff0c;先下载一个sh文件&#xff0c;然后执行这个文件&#xff0c;就自动…

就业班 第三阶段(负载均衡) 2401--4.19 day3 nginx3

二、企业 keepalived 高可用项目实战 1、Keepalived VRRP 介绍 keepalived是什么keepalived是集群管理中保证集群高可用的一个服务软件&#xff0c;用来防止单点故障。 ​ keepalived工作原理keepalived是以VRRP协议为实现基础的&#xff0c;VRRP全称Virtual Router Redundan…

实验7-5:补全代码,删除多个元素

实验7-5&#xff1a;补全代码&#xff0c;删除多个元素 【问题描述】 对于一维数据数组{5,0.3,0.2,1,0.9,3,7,15,10,13,0.1,2}&#xff0c;输入num&#xff0c;删除其中所有小于num的值&#xff0c;输出删除后的数组。 说明&#xff1a;请只提供需要补全的代码部分&#xff0…

乐鑫科技收购创新硬件公司 M5Stack 控股权

乐鑫科技 (688018.SH) 宣布收购 M5Stack&#xff08;明栈信息科技&#xff09;的控股权。这一战略举措对于物联网和嵌入式系统领域的两家公司来说都是一个重要的里程碑&#xff0c;也契合了乐鑫和 M5Stack 共同推动 AIoT 技术民主化的愿景。 M5Stack 以其创新的硬件开发方式而闻…

SpringCloud系列(9)--将服务消费者Consumer注册进Eureka Server

前言&#xff1a;上一章节我们介绍了如何将服务提供者注册进Eureka服务里&#xff0c;本章节则介绍如何将服务消费者Consumer注册进Eureka服务里 Eureka架构原理图 1、修改consumer-order80子模块的pom.xml文件&#xff0c;引入Eureka Clinet的依赖&#xff0c;然后reolad一下&…

Spring IOC工作流程

控制反转(Inversion Of Control),将对象的创建和依赖关系(对象之间的依赖关系可以通过配置文件或者注解来建立)交给第三方容器处理,用的时候告诉容器需要什么然后直接去拿就行了。 Person类作为bean public class Person {public void work(){System.out.println("I am…

Selenium(一):八大元素定位

元素定位八大方法 1、find_element_by_id 通过id定位 find_element(By.ID,"kw") #建议使用2、find_element_by_name 通过标签名定位 find_element(By.NAME,"wd") #建议使用3、find_element_link_text 通过链接文本定位 find_element(By.LINK_TEXT,&q…

高级软考项目管理之项目进度管理

项目进度管理 规划进度管理:为规划、编制、管理、执行和控制项目进度而制定政策程序和文档的过程。 #mermaid-svg-AxNznqgNM9LuBQ9a {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-AxNznqgNM9LuBQ9a .error-icon{f…

VSCode的C/C++开发 ===> Windows

一、开发环境搭建 安装mingw-w64编译器(GCC for Windows 64 & 32 bits)、Cmake工具(选装) VSCode插件安装 C/C cmake cmake tools 二、代码实践演练 基于g命令 g编译单文件&#xff0c;生成带调试信息的可执行文件、并调试 g -g main.cpp -o my_single_swap g编译多文件…

element plus 布局 代码没反应 样式并未生效

就是这样深深浅浅的颜色不显示&#xff0c;整个页面都是白的。 因为网页上示例代码中没有 添加grid-content ep-bg-purple-dark 等相关颜色的样式 在element plus的github中有相关代码&#xff0c;这里可以找到颜色样式 element-plus/docs/examples/layout/index.scss at de…

C语言(扫雷游戏)

Hi~&#xff01;这里是奋斗的小羊&#xff0c;很荣幸各位能阅读我的文章&#xff0c;诚请评论指点&#xff0c;关注收藏&#xff0c;欢迎欢迎~~ &#x1f4a5;个人主页&#xff1a;小羊在奋斗 &#x1f4a5;所属专栏&#xff1a;C语言 本系列文章为个人学习笔记&#x…

webgl canvas系列——animation中基本旋转、平移、缩放(模拟冒泡排序过程)

文章目录 ⭐前言⭐canvas绘制图片&#x1f496;状态保存和恢复&#x1f496;移动、旋转、缩放、变形&#x1f496;移动绘制一个渐变的box&#x1f496;旋转&#x1f496;缩放 ⭐模拟冒泡排序过程⭐结束 ⭐前言 大家好&#xff0c;我是yma16&#xff0c;本文分享webgl canvas系…

JAVA学习笔记25(面向对象编程(高级))

1.4 final关键字 ​ *final可以修饰类、属性、方法和局部变量 1.可能会用到final关键字 1.当不希望类被继承时&#xff0c;可以用final修饰 final class A{ }2.当不希望父类的某个方法被子类覆盖/重写(override)时&#xff0c;可以用final关键字修饰 【访问修饰符 final 返…

GB4806.11食品接触用天然橡胶餐具检测标准解读

GB 4806.11-2023是食品安家标准&#xff0c;针对食品接触用橡胶材料及制品进行了详细规定。该标准于2023年发布&#xff0c;旨在确保食品接触用橡胶材料及制品的安全性和卫生性&#xff0c;以保障消费者的健康。 与之前的版本相比&#xff0c;GB 4806.11-2023主要进行了以下修…

企业常用Linux正则表达式与三剑客/企业生产环境及知识/企业中远程连接ssh工具(为什么连接有时慢?)

企业高薪思维: 1.学习去抓重点有价值知识 2.猛劲学&#xff0c;使劲学&#xff08;能否给别人将会&#xff0c;讲明白&#xff0c;写明白&#xff0c;练习明白&#xff09;&#xff0c;在学习过程中你觉得学会了60-80%&#xff0c;其实你只会了40-50%&#xff0c;你要讲明白会操…

构建云原生湖仓:Apache Iceberg与Amoro的结合实践

随着大数据技术的快速发展&#xff0c;企业对数据的处理和分析需求日益增长。传统的数据仓库已逐渐无法满足现代业务对数据多样性和实时性的要求&#xff0c;这促使了数据湖和数据仓库的融合&#xff0c;即湖仓一体架构的诞生。在云原生技术的推动下&#xff0c;构建云原生湖仓…

AWD线下攻防万字最完整战术(记第一届“长城杯”半决赛战术)

目录 准备阶段 1.登录比赛平台&#xff08;获取资产&#xff09; 查看账号账号修改 服务器SSH口令mysqlWEB服务口令(后台密码)数据库后台管理员密码 账号用户检查 2.dump源码&#xff08;方便应急响应恢复靶机&#xff09; 网站源码备份 压缩文件解压文件备份到服务器本地上传…

【原创】springboot+mysql疫苗预约管理系统设计与实现

个人主页&#xff1a;程序猿小小杨 个人简介&#xff1a;从事开发多年&#xff0c;Java、Php、Python、前端开发均有涉猎 博客内容&#xff1a;Java项目实战、项目演示、技术分享 文末有作者名片&#xff0c;希望和大家一起共同进步&#xff0c;你只管努力&#xff0c;剩下的交…

Linux部署Coturn以及关于打洞的思考

目录 Coturn介绍部署架构图 2.1 局域网——无NAT映射 2.2 NAT网Corturn安装步骤验证 4.1 局域网——无NAT映射 4.2 NAT网 4.2.1 Cywin安装步骤 4.2.2 Coturn安装步骤 4.2.3 验证引言 下文部署架构图为Corturn为解决互联网NAT环境下“找朋友”的部署架构,也是Coturn发挥其价值…

玩转 AIGC!使用 SD-WebUI 实现从文本到图像转换

节前&#xff0c;我们组织了一场算法岗技术&面试讨论会&#xff0c;邀请了一些互联网大厂朋友、参加社招和校招面试的同学&#xff0c;针对算法岗技术趋势、大模型落地项目经验分享、新手如何入门算法岗、该如何准备、面试常考点分享等热门话题进行了深入的讨论。 基于大家…