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

一次AC——第一次贴代码。好规矩的模拟。。。今天服务器特别忙。

Posted by ULTMaster at 2014-07-30 20:25:38 on Problem 2632
#include <iostream>
using namespace std;

struct Location {
	int X;
	int Y;
	char Face;
};

Location* loc;
int K;
int A, B, N, M;

#define OK -2
#define WALL_CRASH -1

inline int robot_crash_detect(int m, int n)
{
	for (int i = 0; i < N; ++i)
	{
		if (loc[i].X == m && loc[i].Y == n)
			return i;
	}
	return OK;
}

inline void turn_left(const int id)
{
	switch (loc[id].Face)
	{
	case 'N': loc[id].Face = 'W'; break;
	case 'W': loc[id].Face = 'S'; break;
	case 'S': loc[id].Face = 'E'; break;
	case 'E': loc[id].Face = 'N'; break;
	}
}

inline void turn_right(const int id)
{
	switch (loc[id].Face)
	{
	case 'N': loc[id].Face = 'E'; break;
	case 'W': loc[id].Face = 'N'; break;
	case 'S': loc[id].Face = 'W'; break;
	case 'E': loc[id].Face = 'S'; break;
	}
}

int one_step_forward(const int id)
{
	int target_x, target_y;
	switch (loc[id].Face)
	{
	case 'N':
		target_x = loc[id].X;
		target_y = loc[id].Y + 1;
		if (target_y == B + 1)
			return WALL_CRASH;
		break;
	case 'W':
		target_x = loc[id].X - 1;
		target_y = loc[id].Y;
		if (target_x == 0)
			return WALL_CRASH;
		break;
	case 'S':
		target_x = loc[id].X;
		target_y = loc[id].Y - 1;
		if (target_y == 0)
			return WALL_CRASH;
		break;
	case 'E':
		target_x = loc[id].X + 1;
		target_y = loc[id].Y;
		if (target_x == A + 1)
			return WALL_CRASH;
		break;
	}
	int detect = robot_crash_detect(target_x, target_y);
	if (detect != OK)
		return detect;
	loc[id].X = target_x;
	loc[id].Y = target_y;
	return OK;
}

int main()
{
	
	cin >> K;
	while (K--)
	{
		cin >> A >> B >> N >> M;
		loc = new Location[N];
		for (int i = 0; i < N; ++i)
			cin >> loc[i].X >> loc[i].Y >> loc[i].Face;
		bool flag = true;
		for (int i = 0; i < M; ++i)
		{
			int id, step;
			char direc;
			cin >> id >> direc >> step;
			if (flag)
			{
				--id;
				if (direc == 'L')
				{
					while (step--)
						turn_left(id);
				}
				else if (direc == 'R')
				{
					while (step--)
						turn_right(id);
				}
				else
				{
					for (int t = 0; t < step; ++t)
					{
						int temp = one_step_forward(id);
						if (temp != OK)
						{
							if (temp == WALL_CRASH)
								cout << "Robot " << id + 1 << " crashes into the wall" << endl;
							else
								cout << "Robot " << id + 1 << " crashes into robot " << temp + 1 << endl;
							flag = false;
							break;
						}
					}
				}
			}
		}
		if (flag)
			cout << "OK" << endl;
		
		delete[] loc;
	}
}

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