Source code bfs_dfs_v1.6.hpp

Source: Internet
Author: User

This is another small modification, mainly about the second template parameter of statespacetreesearch. It is used to specify whether to use BFS or DFS. I used to allow users to select either bfsinserter or dfsinserter. Later I thought about it. It seems that the user should not see the insertion policies of the two nodes. After all, the user does not care about this. Users only care about how to select one between BFS and DFS, so I changed the method to a bool constant to specify.

Change the original searchinserter template parameter to a non-type template parameter and name it usebfs. If it is true, BFs is used; otherwise, DFS is used. Define a searchinserter template to select the appropriate bfsinserter or dfsinserter Based on the bool parameters. The Code is as follows:


// Filename: bfs_dfs_v1.6.hpp
# Ifndef _ bfs_dfs_hpp
# DEFINE _ bfs_dfs_hpp

# Include <list>
# Include <vector>
# Include <set>
# Include <EXT/hash_set>

Using STD: List;
Using STD: vector;
Using STD: Set;
Using _ gnu_cxx: hash_set;
Using _ gnu_cxx: Hash;

// Similar to the function, used to check whether the status node is repeated
Template <class T>
Struct nocheckdup: STD: unary_function <t, bool>
{
Bool operator () (const T &) const
{
Return false;
}
};

// Similar to the function, the vector container is used to check whether the State node is repeated, and the linear complexity is
// Requires the status class to provide operator =
Template <class T>
Class sequencecheckdup: STD: unary_function <t, bool>
{
Typedef vector <t> cont;
Cont States _;
Public:
Bool operator () (const T & S)
{
Typename cont: iterator I =
Find (States _. Begin (), States _. End (), S );
If (I! = States _. End () // The status already exists and is repeated.
{
Return true;
}
States _. push_back (s); // The status is not repeated and the status is recorded
Return false;
}
};

// Use the set container to check whether the status node is repeated and the log complexity is logarithm.
// Requires the status class to provide operator <
Template <class T>
Class ordercheckdup: STD: unary_function <t, bool>
{
Typedef set <t> cont;
Cont States _;
Public:
Bool operator () (const T & S)
{
Typename cont: iterator I = States _. Find (s );
If (I! = States _. End () // The status already exists and is repeated.
{
Return true;
}
States _. insert (I, S); // The status is not repeated and the status is recorded
Return false;
}
};

// Use the hash_set container to check whether the status node is repeated.
// Requires the status class to provide operator = and hash () member functions
Template <class T>
Class hashcheckdup: STD: unary_function <t, bool>
{
Struct hashfcn
{
Size_t operator () (const T & S) const {return S. Hash ();}
};
Typedef hash_set <t, hashfcn> cont;
Cont States _;
Public:
Hashcheckdup (): States _ (100, hashfcn ()){}
Bool operator () (const T & S)
{
If (States _. Find (s )! = States _. End () // The status already exists and is repeated.
{
Return true;
}
States _. insert (s); // The status is not repeated and the status is recorded.
Return false;
}
};

// New node insertion policy corresponding to the BFS Algorithm
Template <class cont>
Class bfsinserter
{
Public:
Bfsinserter (cont & C): C _ (c ){}
Typedef typename cont: iterator;
Typedef typename cont: value_type;
Void operator () (iterator it, const value_type & V ){
C _. push_back (V); // Insert the new node to the end of the list, that is, after the unsearched Node
}
PRIVATE:
Cont & C _;
};

// New node insertion policy corresponding to the DFS Algorithm
Template <class cont>
Class dfsinserter
{
Public:
Dfsinserter (cont & C): C _ (c ){}
Typedef typename cont: iterator;
Typedef typename cont: value_type;
Void operator () (iterator it, const value_type & V ){
C _. insert (++ it, V); // Insert a new node to a node not searched.
}
PRIVATE:
Cont & C _;
};

// Status space tree search Template
// State: the problem state class, which provides nextstep () and istarget () member functions.
// Usebfs: You can select BFS or DFS. "True" indicates "BFS", "false" indicates "DFS", and "BFS" indicates the default value.
// Checkdup: status check algorithm. You can select nocheckdup, hashcheckdup, and ordercheckdup.
Template <class state, bool usebfs = true,
Template <class> class checkdup = nocheckdup>
Class statespacetreesearch
{
Template <class cont, bool>
Struct searchinserter
{
Typedef dfsinserter <cont> inserter;
};

Template <class cont>
Struct searchinserter <cont, true>
{
Typedef bfsinserter <cont> inserter;
};

Public:
Typedef list <State> cont;
Typedef typename cont: iterator;
Typedef typename searchinserter <cont, usebfs >:: inserter;

Template <class func>
Int operator () (const State & initstate, func afterfindsolution) const
// Initstate: initialization state. The class State should provide the member functions nextstep () and istarget (),
// Nextstep () returns all possible states in the Next Step Using Vector <State>,
// Istarget () is used to determine whether the current status meets the requirements;
// Afterfindsolution: it is called after a valid answer is found, and it accepts
// Const State &, and return a bool value. True indicates that the search is stopped,
// False indicates continued search
// Return: number of answers found
{
Checkdup <State> checkdup;
Cont states;
Inserter (States );
States. push_back (initstate );
Iterator head = states. Begin (); // point to the node of the next search
Vector <State> nextstates;
Int n = 0; // number of answers found
Bool stop = false;
While (! Stop & head! = States. End ())
{
State S = * head; // search for a node
Nextstates. Clear ();
S. nextstep (nextstates); // generate the next node from the search point
For (typename vector <state >:: iterator I = nextstates. Begin ();
I! = Nextstates. End (); ++ I)
{
If (I-> istarget ())
{// Locate a target status
++ N;
If (stop = afterfindsolution (* I) // process the result and decide whether to stop
{
Break;
}
} Else {// It is not the target status and determines whether to put it in the search queue
If (! Checkdup (* I) // only puts non-repeated statuses in the search queue
{
Inserter (Head, * I );
}
}
}
++ Head; // move the pointer to the next element
}
Return N;
}
};

# Endif


Correspondingly, the user's code has also changed. The example of a push box is as follows:

// Soko. cpp -- push the main program

# Include <iostream>
# Include "bfs_dfs_v1.6.hpp"
# Include "sokostate_v1.h"

Using namespace STD;

Bool printanswer (const sokostate & S)
{
S. printanswer (cout );
Cout <Endl;
Return true;
}

Template <class T>
Bool stopsearch (const T &)
{
Return true;
}

Int main (INT argc, char * argv [])
{
Sokostate initstate;
Cin> initstate;
Cout <initstate;
Statespacetreesearch <sokostate, true, ordercheckdup> sokosearch;
Int n = sokosearch (initstate, printanswer );
If (n = 0)
{
Cout <"no answer." <Endl;
}
Return 0;
}

 

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.