//运行结果从下往上看
// 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;
}