队列queue
●只允许对前端(front)队首进行删除操作
●对后端(rear)队尾进行插入操作
入队(push)
出队(pop)
判断队列是否为空(empty)
统计队列元素个数(size)
访问队首元素(front)
用队列输出元素:
#include#include #include using namespace std; int main() { queue q; q.push("zhangshan"); q.push("lisi"); q.push("wangwu"); while(!q.empty()){ cout< 报数游戏:
广度优先搜索
迷宫游戏最短路
#include#include #include using namespace std; int n,m; string maze[110]; bool vis[110][110]; int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}}; bool in(int x,int y){ return 0<=x&&x q; q.push(node(sx,sy,0)); vis[sx][sy]=true; while(!q.empty()){ node now=q.front(); q.pop(); for(int i=0;i<4;i++){ int tx=now.x+dir[i][0]; int ty=now.y+dir[i][1]; if(in(tx,ty)&&maze[tx][ty]!='*'&&!vis[tx][ty]) if(maze[tx][ty]=='T'){ return now.d+1; }else{ vis[tx][ty]=true; q.push(node(tx,ty,now.d+1)); } } } return -1; } int main(){ cin>>n>>m; for(int i=0;i >maze[i]; } int x,y; for(int i=0;i



