思想
题目:问123的排列有多少种
将1,2,3放在一个队列上一次向后加数--具体看图示与树形图示
![](https://img-blog.csdnimg.cn/img_convert/35affef0c14b54cbb9bb9d07c8d18254.png)
树形图示
![](https://img-blog.csdnimg.cn/img_convert/5a42e0f8b8829067cc3a3172f3bcf561.png)
代码模板(c/c++)
struct Node{
int x,y;
Node(){}
Node(int_x,int_y):x(_x),y(_y){}
};
bool vis[...][...];
viod bfs(int x,int y){
queue<Node> q;
q.push(Node(x,y));
vis[x][y]=true;
while(!q.empty()){
Node f = q.front();
q.pop();
cnt++;
for(int i=0;i<4;i++){
int tx=f.x+dx[i];
int ty=f.y+dy[i];
if(tx>=0 && tx<n && ty>=0 && ty<m && !vis[tx][ty] && g[tx][ty]=='.'){
q.push(Node(tx,ty));
vis[tx][ty]=ture;
}
}
}
}
广搜蔓延障碍物
![](https://img-blog.csdnimg.cn/img_convert/e14b4da460d255c5cc741415e97989d9.png)
代码模板
struct Node{
int x,y;
Node(){}
Node(int_x,int_y):x(_x),y(_y){}
};
bool vis[...][...];
viod bfs(int x,int y){
queue<Node> q;
q.push(Node(x,y,0));
vis[x][y]=true;
while(!q.empty()){
Node f = q.front();
q.pop();
cnt++;
for(int i=0;i<4;i++){
int tx=f.x+dx[i];
int ty=f.y+dy[i];
if(tx>=0 && tx<n && ty>=0 && ty<m && !vis[tx][ty] && g[tx][ty]=='.'){
if(tx==fx &&ty==ty ==fy) return f.step+1;
q.push(Node(tx,ty,f.step +1));
vis[tx][ty]=ture;
}
}
}
}