0035 algorithm note-[branch restriction method] wiring problems

Source: Internet
Author: User

Problem description

 The printed circuit board divides the wiring area into n × m squares, as shown in. Accurate Circuit Wiring problems require determining the shortest Wiring Scheme connecting the midpoint of Square A to the midpoint of square B. During cabling, the circuit can only be routed along a straight line or right angle, as shown in B. To avoid line intersection, the square of the line is blocked, and other lines are not allowed to pass through the blocked square.


An example of wiring: The picture contains obstacles. The start point is A, and the target point is B.


Algorithm IDEA

To solve this problem, the queuing branch restriction method starts from the starting position a and uses it as the first extension node. The squares adjacent to the extended node become feasible nodes are added to the active node queue and marked as 1, that is, the distance from the starting Square A to the square is 1.

Then, the algorithm extracts the first node of the queue from the active node queue as the next expansion node, marks the square adjacent to the current expansion node and unmarked as 2, and stores it in the active node queue. This process continues until the algorithm searches for the target square B or the active node queue is empty. That is, the breadth search with pruning is given priority.

The algorithm code is as follows:

1. queue. h

 

#include<iostream>using namespace std;template <class T>class Queue{public:Queue(int MaxQueueSize=50);~Queue(){delete [] queue;}bool IsEmpty()const{return front==rear;}bool IsFull(){return ( (  (rear+1)  %MaxSize==front )?1:0);}T Top() const;T Last() const;Queue<T>& Add(const T& x);Queue<T>& AddLeft(const T& x);Queue<T>& Delete(T &x);void Output(ostream& out)const;int Length(){return (rear-front);}private:int front;int rear;int MaxSize;T *queue;};template<class T>Queue<T>::Queue(int MaxQueueSize){MaxSize=MaxQueueSize+1;queue=new T[MaxSize];front=rear=0;}template<class T >T Queue<T>::Top()const{if(IsEmpty()){cout<<"queue:no element,no!"<<endl;return 0;}else return queue[(front+1) % MaxSize];}template<class T>T Queue<T> ::Last()const{if(IsEmpty()){cout<<"queue:no element"<<endl;return 0;}else return queue[rear];}template<class T>Queue<T>&  Queue<T>::Add(const T& x){if(IsFull())cout<<"queue:no memory"<<endl;else{rear=(rear+1)% MaxSize;    queue[rear]=x;}return *this;}template<class T>Queue<T>&  Queue<T>::AddLeft(const T& x){if(IsFull())cout<<"queue:no memory"<<endl;else{front=(front+MaxSize-1)% MaxSize;queue[(front+1)% MaxSize]=x;}return *this;}template<class T>Queue<T>&  Queue<T> ::Delete(T & x){if(IsEmpty())cout<<"queue:no element(delete)"<<endl;else {front=(front+1) % MaxSize;x=queue[front];}return *this;}template<class T>void Queue <T>::Output(ostream& out)const{for(int i=rear%MaxSize;i>=(front+1)%MaxSize;i--)   out<<queue[i];}template<class T>ostream& operator << (ostream& out,const Queue<T>& x){x.Output(out);return out;}

2. 6d4. cpp

 

 

// Solve the wiring problem using the queuing branch restriction method # include "stdafx. H "# include" queue. H "# include <fstream >#include <iostream> using namespace STD; ifstream fin (" 6d4.txt "); const int n = 7; const int M = 7; int grid [n + 2] [M + 2]; struct position {int row; int Col ;}; bool findpath (position start, position finish, Int & pathlen, position * & Path); int main () {int pathlen; position start, finish, * path; start. row = 3; start. col = 2; finish. row = 4; Fin Ish. col = 6; cout <"wiring start point" <Endl; cout <start. col <"" <start. row <Endl; cout <"wiring end point" <Endl; cout <finish. col <"" <finish. row <Endl; cout <"The following is the wiring Grid Array (0 indicates wiring is allowed, 1 indicates wiring is not allowed):" <Endl; For (INT I = 1; I <= m; I ++) {for (Int J = 1; j <= N; j ++) {fin> grid [I] [J]; cout <grid [I] [J] <";}cout <Endl;} findpath (START, finish, pathlen, PATH ); cout <"wiring length:" <pathlen <Endl; cout <"wiring path:" <Endl; For (INT I = 0; I <pathlen; I ++) {cout <Path [I]. col <"" <path [I]. row <Endl;} return 0;} bool findpath (position start, position finish, Int & pathlen, position * & Path) {// calculate the shortest path from start to finish at the target position if (start. row = finish. row) & (start. col = finish. COL) {pathlen = 0; return true;} // set the square array "wall" for (INT I = 0; I <= m + 1; I ++) {grid [0] [I] = grid [n + 1] [I] = 1; // top and bottom} For (INT I = 0; I <= n + 1; I ++) {grid [I] [0] = grid [I] [M + 1] = 1; // left and right} // initialize the relative position offset [4]; offset [0]. row = 0; offset [0]. col = 1; // right offset [1]. row = 1; offset [1]. col = 0; // lower offset [2]. row = 0; offset [2]. col =-1; // left offset [3]. row =-1; offset [3]. col = 0; // int numofnbrs = 4; // Number of adjacent squares position here, NBR; here. row = start. row; here. col = start. col; grid [start. row] [start. col] = 2; // mark the location of the reachable square queue <position> q; do {// mark the adjacent reachable square for (INT I = 0; I <numofnbrs; I ++) {NBR. row = here. row + offset [I]. row; NBR. col = here. col + offset [I]. col; If (Grid [NBR. row] [NBR. col] = 0) // This square is not marked {grid [NBR. row] [NBR. col] = grid [here. row] [here. col] + 1; if (NBR. row = finish. row) & (NBR. col = finish. COL) {break; // complete wiring} Q. add (NBR) ;}/// do you want to finish the target location? If (NBR. row = finish. row) & (NBR. col = finish. COL) {break; // wiring completed} // is the live node queue not empty? If (Q. isempty () {return false; // no solution} Q. delete (here); // get the next extension node} while (true); // construct the Shortest Path pathlen = grid [finish. row] [finish. col]-2; Path = new position [pathlen]; // start from the target position finish back to the starting position here = finish; For (Int J = PathLen-1; j> = 0; J --) {path [J] = here; // find the precursor position for (INT I = 0; I <numofnbrs; I ++) {NBR. row = here. row + offset [I]. row; NBR. col = here. col + offset [I]. col; If (grid [NBR. row] [NBR. col] = J + 2) {break;} Here = NBR; // move forward} return true ;}

Program running result

 

 

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.