奇怪的电梯 (洛谷)宽度优先遍历

 

 

//dfs 深度优先遍历 

//bfs 宽度优先遍历 

//点 边
//1 - n
// 邻接表 邻接矩阵
// n^2 边多 稠密图 邻接矩阵 
//    a  b  c
// a  0  1  0
// b  1  0  1
// c  1  0  0

// n m    n == m  稀疏图  邻接表  vector存图 

// a -> c -> d -> null
// b -> a -> null
// c -> d -> null
// d -> null
 
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue> 

using namespace std;

const int N = 210;
vector<int> es[N];
int d[N];
int n, a, b;

int bfs()
{
 memset(d, -1, sizeof d);
 d[a] = 0;
 
 queue<int> q;//建立一个队列用来放边
 q.push(a);
 
 while (!q.empty())
 {
  int t = q.front();//取队列的头
  q.pop();
  
  if (t == b) return d[t];
  
  for (int v : es[t])
  {
   if (d[v] != -1) continue;
   d[v] = d[t] + 1;
   q.push(v);
  }
 }
 
 return -1;
}

int main()
{
 scanf("%d%d%d", &n, &a, &b);
 
 for (int i = 1; i <= n; i++)
 {
  int x;
  scanf("%d", &x);
  if (i - x >= 1)
   es[i].push_back(i - x);
  if (i + x <= n)
   es[i].push_back(i + x);
 }
 
 printf("%d", bfs());
 
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是图的深度优先遍宽度优先的介绍: 深度优先遍(DFS): 深度优先遍是一种用于遍或搜索树或图的算法。在这种搜索方法中,从根结点开始,尽可能深地搜索每个分支,直到到达最深的结点,然后回溯到前一个结点,继续搜索另一个分支,直到所有结点都被访问为止。深度优先遍使用栈来实现,因此也称为“栈搜索”。 以下是深度优先遍的Python代码示例: ```python # Python3 program to print DFS traversal # from a given given graph from collections import defaultdict # This class represents a directed graph using # adjacency list representation class Graph: # Constructor def __init__(self): # default dictionary to store graph self.graph = defaultdict(list) # function to add an edge to graph def addEdge(self, u, v): self.graph[u].append(v) # A function used by DFS def DFSUtil(self, v, visited): # Mark the current node as visited # and print it visited.add(v) print(v, end=' ') # Recur for all the vertices # adjacent to this vertex for neighbour in self.graph[v]: if neighbour not in visited: self.DFSUtil(neighbour, visited) # The function to do DFS traversal. It uses # recursive DFSUtil() def DFS(self, v): # Create a set to store visited vertices visited = set() # Call the recursive helper function # to print DFS traversal self.DFSUtil(v, visited) ``` 宽度优先(BFS): 宽度优先是一种用于遍或搜索树或图的算法。在这种搜索方法中,从根结点开始,逐层遍每个结点的所有子结点,直到到达最深的结点。宽度优先使用队列来实现。 以下是宽度优先的Python代码示例: ```python # Python3 program to print BFS traversal # from a given source vertex. BFS(int s) # traverses vertices reachable from s. from collections import defaultdict # This class represents a directed graph # using adjacency list representation class Graph: # Constructor def __init__(self): # default dictionary to store graph self.graph = defaultdict(list) # function to add an edge to graph def addEdge(self, u, v): self.graph[u].append(v) # Function to print a BFS of graph def BFS(self, s): # Mark all the vertices as not visited visited = [False] * (max(self.graph) + 1) # Create a queue for BFS queue = [] # Mark the source node as # visited and enqueue it queue.append(s) visited[s] = True while queue: # Dequeue a vertex from # queue and print it s = queue.pop(0) print(s, end=" ") # Get all adjacent vertices of the # dequeued vertex s. If a adjacent # has not been visited, then mark it # visited and enqueue it for i in self.graph[s]: if visited[i] == False: queue.append(i) visited[i] = True ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值