Android & IPhone Fling 游戏求解算法

//运行结果从下往上看
// Fling.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>

const int xCount = 7;
const int yCount = 8;

using namespace std;

typedef vector<pair<int,int>> FPOSArray;

enum FDir
{
	F_UP = 0,
	F_DOWN,
	F_LEFT,
	F_RIGHT
};

bool CanBeFling(pair<int,int> pos,FDir fdir,FPOSArray& tfPos,bool bFirst);
bool FDirection(pair<int,int> pos,FDir fdir,const FPOSArray& fPos,FPOSArray& tfPos);
bool Fling(const FPOSArray& fPos);

int _tmain(int argc, _TCHAR* argv[])
{
	FPOSArray fPos;	
	int x,y;
	cout  << "请输入坐标值。。。,输入-1 -1 结束 -2 -2 删除上一个" << endl;
	while(true)
	{
		cin >> x >> y;
		if (x == -1 && y == -1)
		{
			Fling(fPos);
			break;
		}
		else if (x == -2 && y == -2)
		{
			fPos.erase(fPos.end() - 1);
		}
		else if (x < 1 || y < 1 || x > 7 || y > 8)
		{
			cout  << "输入有误,请便新输入" << endl;
		}
		else
		{
			fPos.push_back(make_pair(x - 1, y - 1)); 
		}
	}

	cin >> x;
	return 0;
}

bool Fling(const FPOSArray& fPos)
{
	if (fPos.size() == 1)
	{
		return true;
	}
	FPOSArray tfpos;
	for (size_t i = 0; i < fPos.size(); i++)
	{
		pair<int,int> pos = fPos.at(i);
		if (FDirection(fPos.at(i),F_UP,fPos,tfpos) && Fling(tfpos))
		{
			cout << "F_UP " << pos.first + 1 << "," << pos.second + 1 << endl;
			return true;
		}
		if (FDirection(fPos.at(i),F_DOWN,fPos,tfpos) && Fling(tfpos))
		{
			cout << "F_DOWN " << pos.first + 1<< "," << pos.second + 1<< endl;
			return true;
		}
		if (FDirection(fPos.at(i),F_LEFT,fPos,tfpos) && Fling(tfpos))
		{
			cout << "F_LEFT " << pos.first + 1 << "," << pos.second + 1<< endl;
			return true;
		}
		if (FDirection(fPos.at(i),F_RIGHT,fPos,tfpos) && Fling(tfpos))
		{
			cout << "F_RIGHT " << pos.first + 1<< "," << pos.second + 1<< endl;
			return true;
		}
	}

	return false;
}

bool FDirection(pair<int,int> pos,FDir fdir,const FPOSArray& fPos,FPOSArray& tfPos)
{
	tfPos.clear();
	tfPos = fPos;
	return CanBeFling(pos,fdir,tfPos,true);
}

bool CanBeFling(pair<int,int> pos,FDir fdir,FPOSArray& tfPos,bool bFirst)
{
	switch (fdir)
	{
	case F_UP:
		{
			int iIndex = pos.second - 1;
			for (int i = iIndex; i >= 0; i--)
			{
				pair<int,int> tpos = make_pair(pos.first,i);
				FPOSArray::iterator iterPos = find(tfPos.begin(),tfPos.end(),tpos);
				if (iterPos != tfPos.end())
				{
					if (i == iIndex && bFirst)
					{
						return false;
					}
					FPOSArray::iterator& iterCurrPos = find(tfPos.begin(),tfPos.end(),pos);
					iterCurrPos->second = i + 1;
					if (!CanBeFling(tpos,fdir,tfPos,false))
					{
						tfPos.erase(iterPos);
					}
					return true;
				}
			}

		}
		break;
	case F_DOWN:
		{
			int iIndex = pos.second + 1;
			for (int i = iIndex; i < yCount; i++)
			{
				pair<int,int> tpos = make_pair(pos.first,i);
				FPOSArray::iterator iterPos = find(tfPos.begin(),tfPos.end(),tpos);
				if (iterPos != tfPos.end())
				{
					if (i == iIndex && bFirst)
					{
						return false;
					}
					FPOSArray::iterator& iterCurrPos = find(tfPos.begin(),tfPos.end(),pos);
					iterCurrPos->second = i - 1;
					if (!CanBeFling(tpos,fdir,tfPos,false))
					{
						tfPos.erase(iterPos);
					}
					return true;
				}
			}
		}
		break;
	case  F_LEFT:
		{
			int iIndex = pos.first - 1;
			for (int i = iIndex; i >= 0; i--)
			{
				pair<int,int> tpos = make_pair(i,pos.second);
				FPOSArray::iterator iterPos = find(tfPos.begin(),tfPos.end(),tpos);
				if (iterPos != tfPos.end())
				{
					if (i == iIndex && bFirst)
					{
						return false;
					}
					FPOSArray::iterator& iterCurrPos = find(tfPos.begin(),tfPos.end(),pos);
					iterCurrPos->first = i + 1;
					if (!CanBeFling(tpos,fdir,tfPos,false))
					{
						tfPos.erase(iterPos);
					}
					return true;
				}
			}

		}
		break;
	case  F_RIGHT:
		{
			int iIndex = pos.first + 1;
			for (int i = iIndex; i < yCount; i++)
			{
				pair<int,int> tpos = make_pair(i,pos.second);
				FPOSArray::iterator iterPos = find(tfPos.begin(),tfPos.end(),tpos);
				if (iterPos != tfPos.end())
				{
					if (i == iIndex && bFirst)
					{
						return false;
					}
					FPOSArray::iterator& iterCurrPos = find(tfPos.begin(),tfPos.end(),pos);
					iterCurrPos->first = i - 1;
					if (!CanBeFling(tpos,fdir,tfPos,false))
					{
						tfPos.erase(iterPos);
					}
					return true;
				}
			}
		}
		break;
	}

	return false;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值