华为OD机试题:亲子游戏C++ 非递归解法

题目描述

宝宝和妈妈参加亲子游戏,在一个二维矩阵(N*N)的格子地图上,宝宝和妈妈抽签决定各自的位置,地图上每个格子有不同的糖果数量,部分格子有障碍物。 

游戏规则是妈妈必须在最短的时间(每个单位时间只能走一步)到达宝宝的位置,路上的所有糖果都可以拿走,不能走障碍物的格子,只能上下左右走。 

请问妈妈在最短到达宝宝位置的时间内最多拿到多少糖果(优先考虑最短时间到达的情况下尽可能多拿糖果)

 

输入

第一行输入为N(N <= 50),N标识二维矩阵的大小 之后N行,每行有N个值,表格矩阵每个位置的值 

其中: 

-3:妈妈 

-2:宝宝 

-1:障碍 

>=0:糖果数(0表示没有糖果,但是可以走)

输出

输出妈妈在最短到达宝宝位置的时间内最多拿到多少糖果,行末无多余空格


#include <stdio.h>
#include <map>
#include <vector>
#include <iostream>
#include <string>
using namespace std;


class GamePoint
{
public:
	unsigned int m_uiPointX;
	unsigned int m_uiPointY;
	bool	m_bEnable;
	unsigned int m_uiGameCandyNum;
	GamePoint* m_pGamePointParent;
	GamePoint(unsigned int x, unsigned int y,bool bEnable,unsigned int uiCandyNum)
		:m_uiPointX(x),m_uiPointY(y),m_bEnable(bEnable),m_uiGameCandyNum(uiCandyNum),m_pGamePointParent(NULL)
	{

	}
	bool operator==(const GamePoint& oPointRight)
	{
		if(this->m_uiPointX == oPointRight.m_uiPointX && this->m_uiPointY == oPointRight.m_uiPointY)
		{
			return true;
		}
		return false;
	}
	string PointOutput()
	{
		char strTemp[100];
		memset(strTemp,0x00,100);
		sprintf(strTemp,"Point(x:%d,y:%d) ",m_uiPointX,m_uiPointY);
		return strTemp;
	}
	string PointOutputToRoot()
	{
		string strOutPut = PointOutput();
		GamePoint* pTemp = m_pGamePointParent;
		while(pTemp != NULL)
		{
			strOutPut = pTemp->PointOutput() + strOutPut;
			pTemp = pTemp->m_pGamePointParent;
		}
		return strOutPut;
	}
	bool IsNewPoint(const GamePoint& oNewPoint)
	{
		if (*this == oNewPoint)
		{
			return false;
		}
		bool bNewPoint(true);
		GamePoint* pTemp = m_pGamePointParent;
		while(pTemp != NULL)
		{
			if ((*pTemp) == oNewPoint)
			{
				bNewPoint = false;
				break;
			}
			pTemp = pTemp->m_pGamePointParent;
		}
		return bNewPoint;
	}
};

bool GetPointInfo(const unsigned int x, const unsigned int y, bool& bWalkEnable,unsigned int& uiCandyNum)
{
    //此处需要根据实际的数据,自行补充,根据x/y值获取当前格子是否可以通行,奖励糖果数目
	bWalkEnable = true;
    uiCandyNum = 2;
	
	return true;
}

bool FindBaby(GamePoint oPointMom,GamePoint oPointbaby,size_t sXmay,size_t sYmax)
{
	typedef vector<GamePoint> VectorGamePoint;
	map<int,VectorGamePoint> mapFind;
	VectorGamePoint vecPoint;
	VectorGamePoint vecResult;
	vecPoint.push_back(oPointMom);
	mapFind[0] = vecPoint;

	map<int,VectorGamePoint>::iterator iterMapFind = mapFind.end();
	bool bFindBaby(false);
	while (!bFindBaby)
	{
		iterMapFind = mapFind.end();
		iterMapFind--;
		VectorGamePoint vecPointTemp = iterMapFind->second;
		VectorGamePoint vecNewPoin;
		
		for (size_t sCurSize = 0 ; sCurSize < vecPointTemp.size();sCurSize++)
		{
			GamePoint oPointTemp = vecPointTemp[sCurSize];
			if (oPointTemp == oPointbaby)
			{
				bFindBaby = true;
				vecResult.push_back(oPointTemp);
				continue;
			}
			else if (bFindBaby == false)
			{
				bool bEnable(false);
				unsigned int uiCandyNum(0);
				if (oPointTemp.m_uiPointX > 0)
				{
					GetPointInfo(oPointTemp.m_uiPointX - 1,oPointTemp.m_uiPointY,bEnable,uiCandyNum);
					GamePoint oPointLeft(oPointTemp.m_uiPointX - 1,oPointTemp.m_uiPointY,bEnable,uiCandyNum);
					oPointLeft.m_pGamePointParent = &((iterMapFind->second)[sCurSize]);
					if (bEnable == true && oPointTemp.IsNewPoint(oPointLeft))
					{
						vecNewPoin.push_back(oPointLeft);
					}
					
				}
				if (oPointTemp.m_uiPointX < sXmay)
				{
					GetPointInfo(oPointTemp.m_uiPointX + 1,oPointTemp.m_uiPointY,bEnable,uiCandyNum);
					GamePoint oPointRight(oPointTemp.m_uiPointX + 1,oPointTemp.m_uiPointY,bEnable,uiCandyNum);
					oPointRight.m_pGamePointParent = &((iterMapFind->second)[sCurSize]);
					if (bEnable == true && oPointTemp.IsNewPoint(oPointRight))
					{
						vecNewPoin.push_back(oPointRight);
					}
				}
				if (oPointTemp.m_uiPointY < sYmax)
				{
					GetPointInfo(oPointTemp.m_uiPointX,oPointTemp.m_uiPointY + 1,bEnable,uiCandyNum);
					GamePoint oPointTop(oPointTemp.m_uiPointX,oPointTemp.m_uiPointY + 1,bEnable,uiCandyNum);
					oPointTop.m_pGamePointParent = &((iterMapFind->second)[sCurSize]);
					if (bEnable == true && oPointTemp.IsNewPoint(oPointTop))
					{
						vecNewPoin.push_back(oPointTop);
					}
				}
				if (oPointTemp.m_uiPointY > 0)
				{
					GetPointInfo(oPointTemp.m_uiPointX,oPointTemp.m_uiPointY - 1,bEnable,uiCandyNum);
					GamePoint oPointBottom(oPointTemp.m_uiPointX,oPointTemp.m_uiPointY - 1,bEnable,uiCandyNum);
					oPointBottom.m_pGamePointParent = &((iterMapFind->second)[sCurSize]);
					if (bEnable == true && oPointTemp.IsNewPoint(oPointBottom))
					{
						vecNewPoin.push_back(oPointBottom);
					}
				}


			}
		}
		if (bFindBaby == false)
		{
			if (!vecNewPoin.empty())
			{
				mapFind[mapFind.size()] = vecNewPoin;
			}
			else
			{
				break;
			}
			
		}

	}
	for(size_t sCurSize = 0;sCurSize < vecResult.size();sCurSize++)
	{
		cout << vecResult[sCurSize].PointOutputToRoot() << endl;
	}
	return bFindBaby;

}

int main()
{
	GamePoint oPointMom(10,15,true,0);
	GamePoint oPointbaby(12,9,true,0);
	FindBaby(oPointMom,oPointbaby,20,20);
    //由于每个格子障碍物和糖果数目并未录入,所以此处会输出所有妈妈找到孩子的路径
}

 

 

宝宝和妈妈参加亲子游戏游戏规则是在一个 n * n 的二维矩阵格子地图上进行。宝宝和妈妈需要先抽签决定谁先开始。 首先,让我们来讲述这个亲子游戏。在这个游戏中,宝宝和妈妈要分别从地图的左上角(起点)出发,通过移动,尽量多地踩到地图上的格子。他们只能向下或向右移动,不能向左或向上移动。他们只能在格子中间的线上交替移动,也就是说,宝宝在一次移动后,妈妈才能移动,然后再轮到宝宝移动,以此类推。当两者都无法继续移动时,游戏结束。 现在,让我们来解决谁先开始这个问。宝宝和妈妈可以通过抽签来决定。他们可以使用抛硬币的方式来决定。例如,宝宝表示正面,妈妈表示反面,然后抛硬币。如果正面朝上,宝宝先开始;如果反面朝上,妈妈先开始。 另一种方法是使用随机数生成器。宝宝和妈妈分别选择一个数,然后使用产生随机数的函数来生成一个数。比较两个随机数的大小,较大的一方先开始。 无论是抛硬币还是使用随机数生成器,都是公平的方法来决定谁先开始这个亲子游戏。这样,宝宝和妈妈都有相同的机会来开始游戏,增加了公平性和趣味性。 总之,选择谁先开始这个亲子游戏可以通过抛硬币或使用随机数生成器来决定。这样可以保证游戏的公平性,让宝宝和妈妈都有机会享受游戏的乐趣。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值