#ifndef _SHARED_DATA
#define _SHARED_DATA

#include <vector>

const static int dx[] = { 0, -1, 1, 0, 0 };
const static int dy[] = { 0, 0, 0, -1, 1 };

int encode(int _dx, int _dy)
{
	for (int move = 0; move < 5; move ++)
		if (dx[move] == _dx && dy[move] == _dy)
			return move;
	return 0;
}

struct SharedData
{
	int m,n;
	int hedgehogX, hedgehogY;
	int totalHorseDX;
	int totalHorseDY;
	int validpositions;
	std::vector<std::vector<bool> > can;
	void init() 
	{
		validpositions = n * m;
		totalHorseDX = totalHorseDY = 0;
		can.resize(m + 1);
		for (int i = 1; i <= m; i ++)
		{
			can[i].resize(n + 1);
			for (int j = 1; j <= n; j ++)
				can[i][j] = 1;
		}
	}
	bool isValidPosition(int x, int y) {
		return 1 <= x && x <= m && 1 <= y && y <= n;
	}
	bool canHorseBeAt(int x, int y) {
		return isValidPosition(x,y) && can[x][y];
	}
	void makeHorseMove(int dx, int dy) {
		std::vector<std::vector<bool> > old = can;
		for (int i = 1; i <= m; i ++)
			for (int j = 1; j <= n; j ++)
				can[i][j] = 0;
		for (int i = 1; i <= m; i ++)
			for (int j = 1; j <= n; j ++)
				if (old[i][j] && isValidPosition(i + dx, j + dy))
					can[i + dx][j + dy] = 1;
		validpositions = 0;
		for (int i = 1; i <= m; i ++)
			for (int j = 1; j <= n; j ++)
				validpositions += can[i][j];
	}

	bool catchHorse() {
		if (can[hedgehogX][hedgehogY]) validpositions --;
		can[hedgehogX][hedgehogY] = 0;
		return !validpositions;
	}
	bool isValidHorseMove(int horseMove) {
		for (int i = 1; i <= m; i ++)
			for (int j = 1; j <= n; j ++)
				if (can[i][j] && isValidPosition(i + dx[horseMove], j + dy[horseMove]))
					return true;
		return false;
	}
};

#endif
