Tuesday, June 16, 2015

UVa 10116 - Robot Motion

// UVa 10116 - Robot Motion

#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
using namespace std;

int n, m, j, i;
int mat[10][10];
string dir[10];

void move() {
	switch (dir[i][j]) {
	case 'N':
		i--;
		break;
	case 'S':
		i++;
		break;
	case 'W':
		j--;
		break;
	case 'E':
		j++;
		break;
	}
}

int main() {
	while ((cin >> n >> m >> j) && n && m && j) {
		i = 0;
		j--;
		for (int z = 0; z < n; z++)
			cin >> dir[z];
		memset(mat, 0, sizeof(mat));
		mat[i][j] = 1;
		int k;
		bool out = false;
		for (k = 2;; k++) {
			move();
			if (i < 0 || j < 0 || i >= n || j >= m) {
				out = true;
				break;
			}
			if (mat[i][j])
				break;
			mat[i][j] = k;
		}
		if (out)
			printf("%d step(s) to exit\n", k - 1);
		else
			printf("%d step(s) before a loop of %d step(s)\n", mat[i][j] - 1, k - mat[i][j]);
	}
	return 0;
}

No comments:

Post a Comment