题目描述
宝宝和妈妈参加亲子游戏,在一个二维矩阵(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);
//由于每个格子障碍物和糖果数目并未录入,所以此处会输出所有妈妈找到孩子的路径
}