struct Point 
{
	int x;
	int y;
};
Point people,destination,box;
void moveBack(Point &point,char c)
{
	switch(c)
	{
	case 'd':
		--point.y;
		break;
	case 'u':
		++point.y;
		break;
	case 'l':
		++point.x;
		break;
	case 'r':
		--point.x;
		break;
	}
}

void moveOn(Point &point,char c)
{
	switch(c)
	{
	case 'd':
		++point.y;
		break;
	case 'u':
		--point.y;
		break;
	case 'l':
		--point.x;
		break;
	case 'r':
		++point.x;
		break;
	}
}

bool check(vector<vector<char> > board,int steps,string move)
{
	bool result = false;
	for(int i = 0;i < steps;++i)
	{
		moveOn(people,move[i]);
		if(people.x < 0 || people.x >= board[0].size() || people.y < 0 || people.y >= board.size())
		{
			moveBack(people,move[i]);
			continue;
		}

		char state = board[people.y][people.x];
		if('4' == state)
		{
			moveBack(people,move[i]);
		}
		else if(box.y == people.y && box.x == people.x)
		{
			moveOn(box,move[i]);
			if(box.x < 0 || box.x >= board[0].size() || box.y < 0 || box.y >= board.size())
			{
				moveBack(people,move[i]);
				moveBack(box,move[i]);
				continue;
			}
			if('4' == board[box.y][box.x])
			{
				moveBack(people,move[i]);
				moveBack(box,move[i]);
			}
		}
	}

	if('2' == board[box.y][box.x])
		return true;

	return result;
}

int _tmain(int argc, _TCHAR* argv[])
{
	int width = 0;
	int height =0;
	int testTime = 0;
	cin >> width >> height >> testTime;

	vector<vector<char> > board(height,vector<char>(width));
	for(int i = 0;i < height;++i)
	{
		string tmp;
		cin >> tmp;
		for(int j = 0;j < width;++j)
		{
			board[i][j] = tmp[j];
			if(tmp[j] == '1')
			{
				people.x = j;
				people.y = i;
			}
			if(tmp[j] == '3')
			{
				box.x = j;
				box.y = i;
			}
			if(tmp[j] == '2')
			{
				destination.x = j;
				destination.y = i;
			}
		}
	}

	Point peopleTmp = people,boxTmp = box;
	for(int i = 0;i < testTime;++i)
	{
		int steps ;
		string move,tmp;
		cin >> steps >> move;
		people = peopleTmp;
		box = boxTmp;
		if(check(board,steps,move))
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}

        return 0;    
}

 

posted on 2015-04-09 10:45  风云逸  阅读(39)  评论(0)    收藏  举报