全排列函数next_permutation
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
int main()
{
//全排列函数next_permutation()
//1-4的全排列
int a[10];
for(int i=0;i<4;i++)
{
a[i]=i+1;
}
do
{
for(int i=0;i<4;i++)
{
cout<<a[i];
}
cout<<endl;
}
while(next_permutation(a,a+4));
return 0;
}
搜索
代表例题:迷宫问题
输入n,m表示地图大小为n行m列
列如:5 4
地图为:
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
输入起点和终点
0 0 3 2
DFS实现:
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
int a[50][50];
bool book[50][50];
int mi=99999;
int n,m;
int stax,stay,endx,endy;
int next[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; //方向数组
void dfs(int x,int y,int step)
{
int tx,ty;
if(x==endx&&y==endy) //到达终点
{
if(step<mi) //更新最小步数
{
mi=step;
}
return;
}
for(int i=0;i<4;i++) //枚举四个方向
{
tx=x+next[i][0];
ty=y+next[i][1];
if(tx<0||ty<0||tx>=n||ty>=m) continue; //判断是否越界
if(a[tx][ty]==0&&!book[tx][ty])
{
book[tx][ty]=true; //标记此点已走过
dfs(tx,ty,step+1); //递归
book[tx][ty]=false;
}
}
return ;
}
int main()
{
memset(book,false,sizeof(book)); //初始化book数组
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>a[i][j];
}
}
cin>>stax>>stay>>endx>>endy;
book[stax][stay]=true;
dfs(stax,stay,0);
cout<<mi<<endl;
return 0;
}
模拟队列实现BFS
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
struct note{
int x;
int y;
int s;
};
int a[50][50];
bool book[50][50];
int n,m;
int stax,stay,endx,endy;
int next[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; //方向数组
int main()
{
memset(book,false,sizeof(book));
int flag=0; //标记是否到达终点
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>a[i][j];
}
}
cin>>stax>>stay>>endx>>endy;
struct note p[2500];
int head,tail;
head=1;
tail=1;
//将起点入队
p[tail].x=stax;
p[tail].y=stay;
p[tail].s=0;
tail++;
while(head<tail) //当队列不为空
{
int tx,ty;
for(int i=0;i<4;i++) //枚举四个方向
{
tx=p[head].x+next[i][0];
ty=p[head].y+next[i][1];
if(tx<0||ty<0||tx>=n||ty>=m) continue; //判断越界
if(a[tx][ty]==0&&!book[tx][ty]) //判断是否能走且没走过
{
book[tx][ty]=true; //标记
//入队
p[tail].x=tx;
p[tail].y=ty;
p[tail].s=p[head].s+1;
tail++;
}
if(tx==endx&&ty==endy) //找到终点
{
flag=1;
break;
}
}
if(flag==1) break;
head++;//出队
}
//最后队尾指向空,需要减一
cout<<p[tail-1].s<<endl;
return 0;
}
C++STLqueue队列实现
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
struct note{
int x;
int y;
int s;
};
queue<note> q;//定义队列
int a[50][50];
bool book[50][50];
int n,m;
int stax,stay,endx,endy;
int next[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; //方向数组
int main()
{
memset(book,false,sizeof(book));
struct note v,w;
struct note now,New;
int flag=0;
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>a[i][j];
}
}
cin>>stax>>stay>>endx>>endy;
book[stax][stay]=true; //起点标记
//起点入队
v.x=stax;
v.y=stay;
v.s=0;
q.push(v);
while(!q.empty()) //队列不为空
{
now=q.front();//取队首
q.pop();
for(int i=0;i<4;i++) //枚举四个方向
{
New.x=now.x+next[i][0];
New.y=now.y+next[i][1];
if(New.x<0||New.y<0||New.x>=n||New.y>=m) continue;//判越界
if(!book[New.x][New.y]&&a[New.x][New.y]==0)
{
book[New.x][New.y]=true;
New.s=now.s+1;
q.push(New);//入队
}
if(New.x==endx&&New.y==endy)//到达终点
{
flag=1;
break;
}
}
if(flag==1) break;
}
w=q.back();//返回队尾
cout<<w.s<<endl;
return 0;
}
测试次数以及数据不太多,可能会有错误!!!