BFS

q.push(head);
while(!q.empty())
{
temp=q.front();
q.pop();
if(tempÎ为目标状态)
输出或记录
if(temp不合法 )
continue;
if(temp合法)
q.push(temp+¦Δ );
}

DFS

void dfs(状态A)
{
if(A不合法)
return;
if(A为目标状态)
输出或记录路径
if(A不为目标状态)

dfs(A+Δ )
}
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
/*
DFS
*/
struct Node{
int pos;
Node *left,*right;
Node(int pos){
this->pos = pos;
this->left = NULL;
this->right = NULL;
}
};
const int N = 1000;
//1. 图的构建第一步
vector<int> m[N],v;
void BFS(Node *map){
queue<Node*> q;
q.push(map);
while(!q.empty()){
Node *p = q.front();
q.pop();
if(p != NULL){
cout << p->pos <<endl;
if(p->left != NULL)
q.push(p->left);
if(p->right != NULL)
q.push(p->right);
}
}
}
/*
void dfs(状态A)
{
if(A不合法)
return;
if(A为目标状态)
输出或记录路径
DFS(遍历路径)

}
*/
//DFS 表示打印 map
void DFS(Node *map){
if(map == NULL)
return ;
else
{
DFS(map->left);
DFS(map->right);
cout << map->pos << endl;
}
}
int main(){
Node *map;
map = new Node(1);
map->left = new Node(2);
map->right = new Node(3);
map->left->left = new Node(4);
map->right->right = new Node(5);

DFS(map);
return 0;
}