笨笨熊搬家交通篇

笨笨熊要搬家,它现在的家在B点,新的豪宅在H点,现在要从B点到H点

地图R表示行,C表示列,其中:

-表示能通过

#表示不能通过

B表示笨笨熊原来的家

H表示笨笨熊的新豪宅

输入

R

C

R×C矩阵

输出

Y//表示可以通过

N//表示不可以通过


#include <iostream>
#include <queue>

using namespace std;

//Point in the grid
typedef struct 
{
	int x;
	int y;
}Pos;

//record the BSF progress , then go back to find the right road
typedef struct
{
Pos curpoint;
int  prepoint_pos;//previous in records  (parent node)
}Record;

//记录遍历节点
int record_len = 0;
int dequeue_count = -1;//出队列的节点在记录数组中的位置
//

const int M = 5;
const int N = 4;
char grid[M][N] = {
	'-','-','-','-',
	'B','-','#','H',
	'-','-','#','-',
	'-','-','#','-',
	'#','-','-','-',
};

Record BFS_Record[M*N];//
//其目的是为了在找到出口后沿这个下标返回到入口处,方便最后显示这条最短路径。

queue<Pos> roadqueue; 
bool map[M][N] = {false};

Pos GetDataPos(char c);
bool IsHome(Pos pos,char c);
int EnqueueAdjacentPos(Pos pos);
int VisitAndPush(int i,int j);

int main()
{
	Pos beg;
	bool flag = false;

	cout << "***当前地图***" << endl;
	for(int m = 0;m<M;m++)
	{
		for(int n = 0;n<N;n++)
			cout << grid[m][n];
		cout<< endl;
	}
	

	beg = GetDataPos('B');
	roadqueue.push(beg);

	//有元素入队列 就记录  遍历节点与父节点的信息
	BFS_Record[record_len].curpoint = beg;
	BFS_Record[record_len].prepoint_pos = dequeue_count;
	//cout << "record_len:" << record_len << endl;
	record_len ++;
	//
	int i = beg.x, j = beg.y;//may existed non-conncted graph
	cout << "*****能否从B到达H*****" << endl;
	if(!map[i][j])
	{
		//cout << "(" << i << "," << j << "): " << grid[i][j] << endl;//visit
		map[i][j] = true;
		while(!roadqueue.empty())
		{
			Pos tmp = roadqueue.front();

			dequeue_count++;//又一个元素出队列		

			roadqueue.pop();
			if(EnqueueAdjacentPos(tmp))//  right down left up 
			{
				cout << "YES" << endl;
				flag = true;
				break;
			}
		}
	}
	if(!flag)
		cout << "NO" << endl;//roadqueue is empty , but still not reach the pos with  'H'

	//end = GetDataPos('H');
	cout << endl << "****记录遍历的坐标点*******************************" << endl;
	cout << grid[ BFS_Record[0].curpoint.x ][ BFS_Record[0].curpoint.y ] << endl;
	for(i = 0;i<record_len;i++)
	{
		cout << "[" << i << "]:" << "(" << BFS_Record[i].curpoint.x << "," << BFS_Record[i].curpoint.y << ") " <<  BFS_Record[i].prepoint_pos << endl;
	}
	cout << grid[ BFS_Record[record_len - 1].curpoint.x ][ BFS_Record[record_len - 1].curpoint.y ] << endl;

	cout << endl << "******H<--B的一条最短路径******" << endl << endl;
	for(i = record_len - 1;i>0;)
	{
		cout <<  "(" << BFS_Record[i].curpoint.x << "," << BFS_Record[i].curpoint.y << ")";
		cout << "<--";
		i = BFS_Record[i].prepoint_pos;
	}
	cout <<  "(" << BFS_Record[i].curpoint.x << "," << BFS_Record[i].curpoint.y << ")";
	cout << endl << endl;
	return 0;
}

Pos GetDataPos(char c)
{
	Pos tmp;
	tmp.x = tmp.y = -1;
	for(int i = 0;i<M;i++)
		for(int j = 0;j<N;j++)
			if(grid[i][j] == c)
			{
				tmp.x = i;
				tmp.y = j;
				return tmp;
			}
	return tmp;
}

bool IsHome(Pos pos,char c)
{
	if(grid[pos.x][pos.y] == c)
		return true;
	else
		return false;
}

int EnqueueAdjacentPos(Pos cur_pos)
{/* right down left up */
	int cur_x = cur_pos.x,cur_y = cur_pos.y;
	int i,j;
	//has right pos
	if(cur_y >=0 && cur_y < N-1)
	{
		i = cur_x;
		j = cur_y + 1;
		if(VisitAndPush(i,j))
			return 1;
	}
	//has down pos
	if(cur_x >=0 &&cur_x < M-1)
	{
		i = cur_x +1;
		j = cur_y;
		if(VisitAndPush(i,j))
			return 1;
	}
	//has left pos
	if(cur_y > 0 && cur_y < M)
	{
		i = cur_x;
		j = cur_y -1;
		if(VisitAndPush(i,j))
			return 1;
	}
	//has up pos
	if(cur_x > 0 && cur_x < M)
	{
		i = cur_x - 1;
		j = cur_y;
		if(VisitAndPush(i,j))
			return 1;
	}
	return 0;
}

int VisitAndPush(int i,int j)
{
	Pos tmp;
	if(!map[i][j] && grid[i][j] != '#')//*****unvisited and available pos  ( - , H)
	{
		map[i][j] = true;
		tmp.x = i;
		tmp.y = j;
		//有元素入队列 就记录  遍历节点与父节点的信息
		BFS_Record[record_len].curpoint = tmp;
		BFS_Record[record_len].prepoint_pos =dequeue_count;
		//cout << "record_len:" << record_len << endl;
		record_len ++;	
		//
		roadqueue.push(tmp);
		if(grid[i][j] == 'H')//arrive the end pos  [最后一个结点H进入队列 算法终止]
			return 1;
	}
	return 0;
}






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值