Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

哪位大哥帮忙看一下啊。。。。找不到哪里wa

Posted by fiona7 at 2011-08-31 22:27:30 on Problem 1098
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
#define  MAX 31

class Pos
{
public:
	int nRow;
	int nCol;
	Pos(int row = 0, int col = 0)
	{
		nRow = row;
		nCol = col;
	}
	bool operator==(Pos p)
	{
		return ((nCol == p.nCol) && (nRow == p.nRow));
	}
};
int nRobot, nTransPos;
vector<Pos> vRobot;
vector<Pos> vTrans;
vector<Pos> vDebris;
Pos MyPos;

bool MovePosition(int nDirection, vector<Pos> &vDebrisPos, vector<Pos> &vRobotPos, Pos &myPosition, int &distance)
{
	myPosition.nRow += ((nDirection - 1) / 3 - 1);
	myPosition.nCol += ((nDirection - 1) % 3 - 1);
	if (myPosition.nCol <= 0 || myPosition.nCol > MAX || myPosition.nRow <= 0 || myPosition.nRow > MAX)
	{
		return false;
	}
	vector<Pos>::iterator it;
	it = find(vDebrisPos.begin(), vDebrisPos.end(), myPosition);
	if (it != vDebrisPos.end())
	{
		it->nRow += ((nDirection - 1) / 3 - 1); 
		it->nCol += ((nDirection - 1) % 3 - 1);
		if (it->nCol <= 0 || it->nCol > MAX || it->nRow <= 0 || it->nRow > MAX)
		{
			return false;
		}
	}
	bool bSuccess = true;
	for (int i = 0; i < vRobotPos.size(); i++)
	{
		if (vRobotPos[i].nRow < myPosition.nRow)
		{
			vRobotPos[i].nRow++;
			if (vRobotPos[i].nCol < myPosition.nCol)
			{				
				vRobotPos[i].nCol++;
			}
			else if (vRobotPos[i].nCol > myPosition.nCol)
			{
				vRobotPos[i].nCol--;
			}
		}
		else if (vRobotPos[i].nRow == myPosition.nRow)
		{
			if (vRobotPos[i].nCol < myPosition.nCol)
			{				
				vRobotPos[i].nCol++;
			}
			else if (vRobotPos[i].nCol > myPosition.nCol)
			{
				vRobotPos[i].nCol--;
			}
		}
		else if (vRobotPos[i].nRow > myPosition.nRow)
		{
			vRobotPos[i].nRow--;
			if (vRobotPos[i].nCol < myPosition.nCol)
			{				
				vRobotPos[i].nCol++;
			}
			else if (vRobotPos[i].nCol > myPosition.nCol)
			{
				vRobotPos[i].nCol--;
			}
		}
		if (vRobotPos[i] == myPosition)
		{
			bSuccess = false;
		}
		if (find(vDebrisPos.begin(), vDebrisPos.end(), Pos(vRobotPos[i].nRow, vRobotPos[i].nCol)) != vDebrisPos.end())
		{
			vRobotPos.erase(vRobotPos.begin() + i);
			i--;
		}
		else 
		{
			for (int j = 0; j < vRobotPos.size(); j++)
			{
				if (j == i)
				{
					continue;
				}
				if (vRobotPos[j] == vRobotPos[i])
				{
					vDebrisPos.push_back(vRobotPos[i]);
					if (j > i)
					{
						vRobotPos.erase(vRobotPos.begin() + j);
						vRobotPos.erase(vRobotPos.begin() + i);
						i -= 1;
					}
					else if (j < i)
					{
						vRobotPos.erase(vRobotPos.begin() + i);
						vRobotPos.erase(vRobotPos.begin() + j);
						i -= 2;
					}					
					break;
				}
			}
			if (vRobotPos.size() == 0)
			{
				break;
			}
		}
	}
	if (!bSuccess)
	{
		return false;
	}
	distance = 70;
	for (int i = 0; i < vRobotPos.size(); i++)
	{
		if (abs(myPosition.nRow - vRobotPos[i].nRow) + abs(myPosition.nCol - vRobotPos[i].nCol) < distance)
		{
			distance = abs(myPosition.nRow - vRobotPos[i].nRow) + abs(myPosition.nCol - vRobotPos[i].nCol);
		}
	}
	return bSuccess;
}

bool DecideMove()
{
	int nDeleteRobot = -1;
	vector<Pos> resultDebris;
	vector<Pos> resultRobot;
	Pos resultPos;
	int nMaxDis = 0;
	for (int i = 1; i <= 9; i++)
	{
		vector<Pos> vRobotPosition(vRobot);
		vector<Pos> vDebrisPos(vDebris);
		Pos myPosition = MyPos;
		int nDistance = 0;
		if (MovePosition(i, vDebrisPos, vRobotPosition, myPosition, nDistance))
		{					
			int nDelete = vRobot.size() - vRobotPosition.size();
			if (nDelete > nDeleteRobot)
			{
				nDeleteRobot = nDelete;
				resultDebris = vDebrisPos;
				resultPos = myPosition;
				resultRobot = vRobotPosition;
				nMaxDis = nDistance;
			}
			else if (nDelete == nDeleteRobot)
			{
				if (nDistance > nMaxDis)
				{
					resultDebris = vDebrisPos;
					resultPos = myPosition;
					resultRobot = vRobotPosition;
					nMaxDis = nDistance;
				}
			}
		}
	}
	if (nDeleteRobot == -1)
	{
		return false;
	}
	vRobot = resultRobot;
	vDebris = resultDebris;
	MyPos = resultPos;
	return true;
}

int main()
{
	cin>>nRobot>>nTransPos;
	int nCase = 0;
	while(nRobot != 0)
	{
		nCase++;
		int nMove = 0;	
		MyPos.nRow = 15;
		MyPos.nCol = 15;
		vRobot.clear();
		vTrans.clear();
		vDebris.clear();
		int row, col;
		for (int i = 0; i < nRobot; i++)
		{
			cin>>row>>col;
			vRobot.push_back(Pos(row, col));
		}
		for (int i = 0; i < nTransPos; i++)
		{
			cin>>row>>col;
			vTrans.push_back(Pos(row, col));
		}
		cout<<"Case "<<nCase<<":"<<endl;
		while(true)
		{
			nMove++;
			if (DecideMove())
			{
				if (vRobot.size() == 0)
				{
					cout<<"Won game after making "<<nMove<<" moves."<<endl;
					cout<<"Final position: ("<<MyPos.nRow<<","<<MyPos.nCol<<")"<<endl;
					cout<<"Number of cells with debris: "<<vDebris.size()<<endl<<endl;
					break;
				}
			}
			else
			{
				bool bMove = false;
				for (int i = 0; i < vTrans.size(); i++)
				{
					if (find(vRobot.begin(), vRobot.end(), Pos(vTrans[i].nRow, vTrans[i].nCol)) == vRobot.end()
						&& find(vDebris.begin(), vDebris.end(), Pos(vTrans[i].nRow, vTrans[i].nCol)) == vDebris.end())
					{
						Pos savePos = MyPos;
						MyPos.nRow = vTrans[i].nRow;
						MyPos.nCol = vTrans[i].nCol;
						int nDis = 0;
						if (MovePosition(5, vDebris, vRobot, MyPos, nDis))
						{
							bMove = true;
							cout<<"Move "<<nMove<<": teleport to ("<<MyPos.nRow<<","<<MyPos.nCol<<")"<<endl;
							vTrans.erase(vTrans.begin() + i);
							break;
						}
						else
						{
							MyPos = savePos;
						}					
					}
				}
				if (!bMove)
				{
					int nDis = 0;
					MovePosition(5, vDebris, vRobot, MyPos, nDis);
					cout<<"Lost game after making "<<nMove<<" moves."<<endl;
					cout<<"Final position: ("<<MyPos.nRow<<","<<MyPos.nCol<<")"<<endl;
					cout<<"Number of cells with debris: "<<vDebris.size()<<endl;
					cout<<"Number of robots remaining: "<<vRobot.size()<<endl<<endl;
					break;
				}
			}
		}	
		cin>>nRobot>>nTransPos;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator