The shortest path problem in breadth-first search

Source: Internet
Author: User
Breadth-First search to find the shortest path problem, dealing with the following issues:
Title: Given an array, the inside is full of positive integers. The number size indicates that this step can move up to several nodes backward. Total
is to move from the first element of the array. Asked how to move, you can move to the last node with a minimum number of steps.
For example: [3, 4, 2, 1, 3, 1] The initial state points to 3, which means that the next step is to move 1, or 2, or 3 lattices.
The best way is to point to 3 when moving one step, the second choice to move 4 steps, altogether only need two steps to move to the end of the array.
//
Input: 3, 4, 2, 1, 3, 1
Output: Step by point 3, 4, 1
Only the first set of shortest paths can be returned
#include <iostream>
#include <vector>
#include <queue>
#include <string.h>
#include <algorithm>//algorithm function
using namespace Std;
Class graph//Draw the node line graph
{
Private
int *bulb;//node is accessed, 0 is not accessed, 1 is access
Vector<int> *edges;//node adjacent to this node (adjacency table)
int *parent;//The parent node of the desired node
Vector<int> v;
Public
Graph (int n)//external input graph number of nodes, used to initialize an array of pointers within the class
{
The reason for bulb = new int[n+1];//n+1 is that bulb[n] is the highest starting from Bulb[0 only bulb[n-1], so for the nth bulb corresponding to bulb[n], initialize bulb[n+1.
for (int i = 0; I <=n; i++)
{
Bulb[i] = 0;
}
edges = new vector<int>[n];
Parent = new Int[n];
}
~graph ()//Free pointer space
{
Delete[] Bulb;
Delete[] edges;
Delete[] Parent;
}
void drawgraph (int prt,int son)/Start drawing node graph, external input each node corresponding child node
{
Edges[prt].push_back (son);//Insert son into array team tail
}
int BFS (int start_top,int end_bottom)//bfs Breadth First search, the so-called breadth search is to determine the order of the data in each layer,
The value of the search was assigned to 1.
{
Queue<int> bfs_queue;//creates a queue, and the queue is empty.
Queue, advanced first Out, Q.push () press the tail, Q.pop () team head,
Also can only access not pop-up, Q.front () Visit Team Head, Q.back () Visit Team tail,
Q.size () accesses the number of queue elements. You can also define a priority queue, which is based on the weight of the team
No need to first out, priority_queue objects.
Bfs_queue.push (start_top);//Press the starting vertex of the graph into the end of the queue.
Bulb[start_top] = 1;//to the starting vertex of the search graph to determine the search.
while (!bfs_queue.empty ())
{
int top = Bfs_queue.front ();//Read team first element.
for (Vector<int>::iterator it = Edges[top].begin (); it!= edges[top].end (); it++)
All the sub nodes under the number top are judged by whether the search has been made, and none of the searched are pressed into the queue and then set to be determined.
{
if (bulb[*it]==0)
{
Bulb[*it] = 1;
Bfs_queue.push (*it);
Parent[*it] = top;//Note the parent node of the child node
if (*it = = End_bottom)
{
return *it;
}
}
}

Bfs_queue.pop ();//eject team head;
}
}
vector<int> findpath (int p)
{
V.push_back (Parent[p]);
if (V.back ()!= 1)//Determine if a header has been found when finding a path
{
Findpath (Parent[p]);
}
return v;
}
};
int main ()
{
cout << "Please input array:" << Endl;
Vector<int> sz;
int num; int i = 1;
while (Cin>>num)
{
char C = cin.get ();//Read the next INPUT element
Sz.push_back (num);
if (c!= ', ')
{
Break
}

I++;//entered a few numbers
}

Graph g (i);
for (int j = 1; j < I; j + +)//The first node of an array
{
for (int tn = 1; tn <= sz[j-1]/tn++)/all child nodes corresponding to each node
{
if (J + TN <= i)
{
G.drawgraph (J, J + tn);//Draw the connection diagram of node and child node
}
}
}
int Terminal=g.bfs (1,i);//bfs returns the first node to reach the end point
Vector<int> v;
v = g.findpath (terminal);
The following is to be found in the path flashback
Vector<int> TV;
int t = v.size ();
for (int i = 0; i < t;i++)
{
Tv.push_back (V.back ());
V.erase (V.end ()-1);//erase deletes the element by address, deletes the end element, minus one because the array is the end character.
}
Above
Tv.push_back (terminal);
cout << Sz[tv[0]-1];
for (int i = 1; i < tv.size (); i++)
{
cout << ', ' << sz[tv[i]-1];
}
cout << Endl;
System ("pause");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.