UVA – 118 – Mutant Flatworld Explorers

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int dy[] = { +1, +0, -1, +0 }, dx[] = { +0, +1, +0, -1 };
char dr[] = { 'N', 'E', 'S', 'W' };
bool bad[54][54];
int I, J, x0, y0;
char d0;

bool nextState(int& x1, int& y1, char& d1, int i, string p) {
	if (i >= p.size())	return false;
	if (p[i] != 'F')	d1 = (d1 + (p[i] == 'R' ? 1 : 3)) % 4;
	if (p[i] == 'F') {
		int x2 = x1 + dx[d1], y2 = y1 + dy[d1];
		if (x2 >= 0 && x2 <= J && y2 >= 0 && y2 <= I) x1 = x2, y1 = y2;
		else if (!bad[x1][y1]) 	return (bad[x1][y1] = true);
	}
	return nextState(x1, y1, d1, i + 1, p);
}

int main() {
	map<char, int> m;
	m['N']=0, m['E']=1, m['S']=2, m['W']=3;
	memset(bad, 0, sizeof bad);
	cin >> J >> I;
	string s;
	while ((cin >> x0 >> y0 >> d0 >> s)) {
		d0 = m[d0];
		bool lost = nextState(x0, y0, d0, 0, s);
		printf("%d %d %c", x0, y0, dr[d0]);
		cout << (lost ? " LOST" : "") << endl;
	}
	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: