程序可以写的更标准点,但俺这里已经夜里2点了,回头再修正吧。没什么算法,就是把点们从作到又排列一下,吃完的跳过,两个PAC MAN各来吃一次。
http://www.bc-cn.net/bbs/dispbbs.asp?boardid=5&id=63515&star=1#135076
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
class CPoint
{
public:
CPoint(int x=0, int y=0) :m_intX(x), m_intY(y) {}
int GetX() {return m_intX;}
int GetY() {return m_intY;}
bool CanConnect(CPoint p) { return(GetX() <= p.GetX() && GetY() <= p.GetY());}
private:
int m_intX;
int m_intY;
};
bool ComparePointPosition(CPoint p1, CPoint p2)
{
return p1.GetX() < p2.GetX();
}
int main(int argc, char* argv[])
{
CPoint BeanPoints[] =
{
CPoint(8, 1),
CPoint(1, 5),
CPoint(5, 7),
CPoint(2, 2),
CPoint(7, 8),
CPoint(4, 6),
CPoint(3, 3),
CPoint(6, 4)
};
CPoint *pCurrent;
const int intBeanNum = sizeof(BeanPoints) / sizeof(CPoint);
long lTryCount1, lTryCount2, lCurrentTry, lTryTimes = pow(2, intBeanNum);
int intEatenBeanNum, intPointCount, intMaxEaten = 0;
bool intPointStatus[intBeanNum]; // eaten or not, true is eaten
sort(&BeanPoints[0], &BeanPoints[8], ComparePointPosition);
// man 1
for (lTryCount1 = 0; lTryCount1 < lTryTimes; lTryCount1++)
{
intEatenBeanNum = 0;
pCurrent = &BeanPoints[0];
lCurrentTry = lTryCount1;
// init eaten point array.
for (intPointCount = 0; intPointCount < intBeanNum; intPointCount++)
intPointStatus[intPointCount] = false;
for (intPointCount = 0; intPointCount < intBeanNum; intPointCount++)
{
if (lCurrentTry % 2 && intPointStatus[intPointCount] == false)
{
// this point in count.
if (intEatenBeanNum == 0 || pCurrent->CanConnect(BeanPoints[intPointCount]))
{
pCurrent = &BeanPoints[intPointCount];
intPointStatus[intPointCount] = true;
intEatenBeanNum ++;
}
}
lCurrentTry /= 2;
}
// man 2, not status clean up
for (lTryCount2 = 0; lTryCount2 < lTryTimes; lTryCount2++)
{
lCurrentTry = lTryCount2;
pCurrent = &BeanPoints[0];
for (intPointCount = 0; intPointCount < intBeanNum; intPointCount++)
{
if (lCurrentTry % 2 && intPointStatus[intPointCount] == false)
{
// this point in count.
if (intEatenBeanNum == 0 || pCurrent->CanConnect(BeanPoints[intPointCount]))
{
pCurrent = &BeanPoints[intPointCount];
intPointStatus[intPointCount] = true;
intEatenBeanNum ++;
}
}
lCurrentTry /= 2;
}
}
if (intMaxEaten < intEatenBeanNum)
intMaxEaten = intEatenBeanNum;
}
cout << "PAC Man ate " << intMaxEaten << " beans\n";
return 0;
}