poj2632

本题的图与其他题稍有不同,本题以左下角为(0,0)点,列号对应x,行号对应y。

完全模拟即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

const int maxa = 101;

struct Robot
{
	int x, y, d;
} robot[maxa];

int dir[4][2] =
{
{ 0, 1 },//e
{ -1, 0 }, //s
{ 0, -1 }, //w
{ 1, 0 } }; //n


int n, m, cx, cy;
int map[maxa][maxa];

void init()
{
	memset(map, 0, sizeof(map));
	cin >> cy >> cx >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		cin >> robot[i].y >> robot[i].x;
		getchar();
		switch (getchar())
		{
		case 'E':
			robot[i].d = 0;
			break;
		case 'S':
			robot[i].d = 1;
			break;
		case 'W':
			robot[i].d = 2;
			break;
		case 'N':
			robot[i].d = 3;
			break;
		}
		map[robot[i].x][robot[i].y] = i;
	}
}

bool forward(int a)
{
	map[robot[a].x][robot[a].y] = 0;
	robot[a].x += dir[robot[a].d][0];
	robot[a].y += dir[robot[a].d][1];
	int x = robot[a].x;
	int y = robot[a].y;
	if (x == 0 || x > cx || y == 0 || y > cy)
	{
		printf("Robot %d crashes into the wall\n", a);
		return false;
	}
	if (map[x][y] != 0)
	{
		printf("Robot %d crashes into robot %d\n", a, map[x][y]);
		return false;
	}
	map[x][y] = a;
	return true;
}

bool operate(int a, char op, int times)
{
	if (op == 'F')
	{
		for (int i = 0; i < times; i++)
			if (!forward(a))
				return false;
		return true;
	}
	if (op == 'L')
		robot[a].d -= times;
	else
		robot[a].d += times;
	robot[a].d %= 4;
	if (robot[a].d < 0)
		robot[a].d += 4;
	return true;
}

void finish(int times)
{
	string st;
	while (times--)
		getline(cin, st);
}

void work()
{
	for (int i = 0; i < m; i++)
	{
		int num, time;
		char op;
		cin >> num;
		getchar();
		cin >> op >> time;
		getchar();
		if (!operate(num, op, time))
		{
			finish(m - i - 1);
			return;
		}
	}
	cout << "OK\n";
}

int main()
{
	//freopen("D:\\t.txt", "r", stdin);
	int t;
	cin >> t;
	while (t--)
	{
		init();
		work();
	}
	return 0;
}
原文地址:https://www.cnblogs.com/rainydays/p/1948677.html