当前位置:网站首页>7-13 地下迷宫探索(邻接表)
7-13 地下迷宫探索(邻接表)
2022-06-12 09:30:00 【R_1220】
地道战是在抗日战争时期,在华北平原上抗日军民利用地道打击日本侵略者的作战方式。地道网是房连房、街连街、村连村的地下工事,如下图所示。
我们在回顾前辈们艰苦卓绝的战争生活的同时,真心钦佩他们的聪明才智。在现在和平发展的年代,对多数人来说,探索地下通道或许只是一种娱乐或者益智的游戏。本实验案例以探索地下通道迷宫作为内容。
假设有一个地下通道迷宫,它的通道都是直的,而通道所有交叉点(包括通道的端点)上都有一盏灯和一个开关。请问你如何从某个起点开始在迷宫中点亮所有的灯并回到起点?
输入格式:
输入第一行给出三个正整数,分别表示地下迷宫的节点数N(1<N≤1000,表示通道所有交叉点和端点)、边数M(≤3000,表示通道数)和探索起始节点编号S(节点从1到N编号)。随后的M行对应M条边(通道),每行给出一对正整数,分别是该条边直接连通的两个节点的编号。
输出格式:
若可以点亮所有节点的灯,则输出从S开始并以S结束的包含所有节点的序列,序列中相邻的节点一定有边(通道);否则虽然不能点亮所有节点的灯,但还是输出点亮部分灯的节点序列,最后输出0,此时表示迷宫不是连通图。
由于深度优先遍历的节点序列是不唯一的,为了使得输出具有唯一的结果,我们约定以节点小编号优先的次序访问(点灯)。在点亮所有可以点亮的灯后,以原路返回的方式回到起点。
输入样例1:
6 8 1 1 2 2 3 3 4 4 5 5 6 6 4 3 6 1 5输出样例1:
1 2 3 4 5 6 5 4 3 2 1输入样例2:
6 6 6 1 2 1 3 2 3 5 4 6 5 6 4输出样例2:
6 4 5 4 6 0
#include<bits/stdc++.h>
using namespace std;
#define MAXN 1010
int vis[MAXN];
vector<int> road;
bool cmp(const int & a , const int & b){
return a < b;
}
void dfs(int v , vector<int> Adj[]);
int main(){
int N , M , S;
cin >> N >> M >> S;
vector<int> Adj[N + 1];
for(int i = 0;i < M;i ++){
int v1 , v2;
cin >> v1 >> v2;
Adj[v1].emplace_back(v2);
Adj[v2].emplace_back(v1);
}
for(int i = 1;i <= N;i ++){
sort(Adj[i].begin() , Adj[i].end() , cmp);
}
dfs(S , Adj);
int flag = 0;
for(int i = 1;i <= N;i ++){
if(!vis[i]){
flag = 1;
break;
}
}
if(flag == 1){
for(int i = 0;i < road.size();i ++){
cout << road[i] << " ";
}
cout << "0";
}else{
for(int i = 0;i < road.size();i ++){
cout << road[i];
if(i != road.size() - 1){
cout << " ";
}
}
}
}
void dfs(int v , vector<int> Adj[]){
vis[v] = 1;
road.emplace_back(v);
for(int i = 0;i < Adj[v].size();i ++){
int u = Adj[v][i];
if(!vis[u]){
dfs(u , Adj);
road.emplace_back(v); //记录回溯节点 (递归 先进后出)
}
}
}
边栏推荐
- Crazy temporary products: super low price, big scuffle and new hope
- 自动化测试学习路线,快来学吧
- ABC253F Operations on a Matrix
- Ceil, floor and round functions
- After going to the bathroom, I figured out the ES search server
- Auto.js学习笔记8:常用且重要的一些API
- Introduction Fibonacci series
- Principle analysis of mongodb storage engine wiredtiger
- MySQL installation
- MySQL-MVCC
猜你喜欢

004:AWS数据湖解决方案

Microservice gateway

JVM virtual machine
测试用例和bug描述规范参考

Basic exercise letter graphics

Es6-- common basic knowledge

软件定义存储概览(一篇就够)
What are the design principles of an automated test framework? I'll sum it up for you. Come and see

Do you know the meaning behind these questions?
Database common interview questions are ready for you
随机推荐
[cloud native] what exactly does it mean? This article shares the answer with you
【极术公开课预告】Arm最强MCU内核Cortex-M85处理器,全方位助力物联网创新(有抽奖)
Basic SQL syntax i
使用C语言代码实现工厂端LCD RGB的测试程序
《真北》读书笔记
Ceph如何改善存储性能以及提升存储稳定性
ADB command collection, let's learn together
MySQL index
NiO principle
Subtractive integer (number theory)
Test case and bug description specification reference
Summary of APP test interview questions, which must be taken in the interview
硬盘 SMART 检测参数详解
Common omissions in software test reports, give yourself a wake-up call
Auto.js学习笔记9:脚本引擎使用,启动指定路径脚本文件和关闭等基础方法
001:数据湖是什么?
Quick sort
软件测试需求分析方法有哪些,一起来看看吧
++ problems in C language
005:数据湖与数据仓库的区别
