The Process Of BFS.

//This codes will show u how BFS algorithm works.<pre name="code" class="cpp">#include<iostream>
#include<string>
#include<list>
#include<windows.h>
using namespace std;
char map[50][50]={
						"                    ",
						"                    ",
						"                    ",
						"                    ",
						"                    ",
						"                    ",
						"                    ",
						"                    ",
						"                    ",
						"                    ",
	};
class coordinate
{
public:
	int x,y;
	coordinate(int xx,int yy):x(xx),y(yy){}
};
void show()
{
	int i,j;
	for(i=0;i<10;i++)
	{
		for(j=0;j<20;j++)
			cout<<map[i][j];
		cout<<endl;
	}
	Sleep(100);//flash time if u wanne see faster ,erase it.
	system("cls");
}
int main()
{

	list<coordinate>queue;
	int visited[100][100]={0},start_x,start_y,next_x,next_y,row,col,dir,direction[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
	cout<<"Input the start coordinate which x range from 0 to 10 and y range 0 to 20(eg intput: 5 2)"<<endl;
	cin>>start_x>>start_y;
	row=10;
	col=20;
	visited[start_x][start_y]=1;
	map[start_x][start_y]='*';
	queue.push_back(coordinate(start_x,start_y));
	while(!queue.empty())
	{
		for(dir=0;dir<=3;dir++)
		{
			next_x=queue.front().x+direction[dir][0];
			next_y=queue.front().y+direction[dir][1];
			if(next_x<0||next_y<0||next_x>=row||next_y>=col)
				continue;
			else if(visited[next_x][next_y]==0)
			{
				show();
				map[next_x][next_y]='*';
				visited[next_x][next_y]=1;
				queue.push_back(coordinate(next_x,next_y));
				show();
			}
		}
		queue.pop_front();
	}
	return 0;
}


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, here's a C++ program that meets the requirements you specified: ```c++ #include <iostream> #include <queue> #include <stack> using namespace std; const int N = 5; // Size of the graph bool graph[N][N] = { // Connectivity of the graph {0, 1, 1, 0, 0}, {1, 0, 1, 1, 0}, {1, 1, 0, 1, 1}, {0, 1, 1, 0, 1}, {0, 0, 1, 1, 0} }; void dfs(int start) { stack<int> s; bool visited[N] = {false}; s.push(start); visited[start] = true; while (!s.empty()) { int v = s.top(); s.pop(); cout << v << " "; for (int i = 0; i < N; i++) { if (graph[v][i] && !visited[i]) { s.push(i); visited[i] = true; } } } cout << endl; } void bfs(int start) { queue<int> q; bool visited[N] = {false}; q.push(start); visited[start] = true; while (!q.empty()) { int v = q.front(); q.pop(); cout << v << " "; for (int i = 0; i < N; i++) { if (graph[v][i] && !visited[i]) { q.push(i); visited[i] = true; } } } cout << endl; } int main() { // Choose a starting position based on the connectivity of the graph int start = 0; // For simplicity, we start at node 0 for (int i = 0; i < N; i++) { if (graph[start][i]) { start = i; break; } } cout << "Starting at node " << start << endl; cout << "Depth-first traversal: "; dfs(start); cout << "Breadth-first traversal: "; bfs(start); return 0; } ``` This program uses a 5x5 boolean two-dimensional array to represent the connectivity of the graph that needs to be traversed. It first chooses a starting position based on the connectivity of the graph by finding the first node that is connected to the starting node. Then, it uses depth-first and breadth-first algorithms to traverse the graph starting from the chosen node. Finally, it outputs the results of the traversals.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值