广度优先搜索-BFS

参考

In this problem, we need to find the hops of each island from island M. That fits exactly the Breadth First Search (BFS) algorithm introduced in the USACO training pages. The algorithm is summarized as follows:

  1. Insert M into the queue.
  2. Get the next island, I from the queue.
  3. Insert the unmarked neigbors of I into the queue, record their hops (add 1 to I's hop) and mark them.
  4. If the queue is not empty, go to step 2.

For the example in the problem, the steps can be demonstrated as follows:


		mark array
step 	queue	1 2 3 4 => islands
====	=====	=======
0	1	0
1	2 4	0 1   1	=> hopes in each step
2	4 3	0 1 2 1
3       3	0 1 2 1
4 		0 1 2 1


Notice that we don't use an array for hops but use the mark array also to record the hops of the islands. While outputting the islands, we need to be careful about the order to be ascending. So, what we need to do is to search the mark array for 0 hops and output the first line, search for 1 hop and output the second line, search for 2 hops and output the third line, and so on... Since we search the islands from left to right, the output will be in ascending order.

Below is an example solution:

#include <iostream>
#include <fstream>
#define MAX 101

using namespace std;

int n, m, mat[MAX][MAX],mark[MAX],que[MAX],front,rear;

int main()
{
	//freopen("1.txt", "r", stdin);
	cin >> n >> m;
	for (int i=1; i <= n; i++)
		for (int j=1; j <= n; j++)
			cin >> mat[i][j];

	que[rear++]=m;		// add M into the queue
	mark[m]=1;		// start the hops from 1 not 0
				//	since all the islands will be initially 0
	// continue until the queue is empty
	while (front < rear)
	{
		// get the next island in the queue
		int island=que[front++];

		// add all the unmarked neighbors of the island into the queue
		for (int i=1; i <= n; i++)
			if (!mark[i] && mat[island][i])
				que[rear++]=i,mark[i]=mark[island]+1;
	}

	
	// start searching the islands with 1 hop, 2 hops, etc...
	// 	until the last island that was in the queue.
	for (int i=1; i <= mark[que[rear-1]]; i++)
	{
		for (int j=1, k=0; j<=n; j++)
			// if the hop of the island is equal the current hop
			if (mark[j] == i) 
			{
				// be careful when outputting
				// if it is not the first island in the line, 
				// 	put space after that (k is used for that)
				if (k) cout << " ";
				cout << j;
				k++;
			}
		cout << endl;	// end of line
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值