#include <cmath>
#include <iostream>

using namespace std;

const size_t xsize = 8;
const size_t ysize = 8;

unsigned int scoresquares(const bool tokenarray[xsize][ysize], const short lastpointx, const short lastpointy);

int main() {
	//Initialize some points.
	bool tokenarray[xsize][ysize] = {false};

	tokenarray[1][0] = true;
	tokenarray[7][1] = true;
	tokenarray[6][7] = true;
	tokenarray[0][6] = true;

	tokenarray[1][2] = true;
	tokenarray[3][2] = true;
	tokenarray[3][0] = true;

	//Two squares for 73 points (64 + 9).
	cout << scoresquares(tokenarray, 1, 0) << endl;
	return 0;
}

//Pass the current player's token array, which is a boolean 8x8 array of occupied spaces.
//Also input the last move by the player.
unsigned int scoresquares(const bool tokenarray[xsize][ysize], const short lastpointx, const short lastpointy) {
	unsigned int score = 0;

	for (short selx = 0; selx < xsize; selx++) {
		for (short sely = 0; sely < ysize; sely++) {
			//Only look at spaces that are taken by the current player and not the current point.
			if (!tokenarray[selx][sely] || (selx == lastpointx && sely == lastpointy))
				continue;

			short xdist = lastpointx - selx;
			short ydist = lastpointy - sely;

			short curx = lastpointx;
			short cury = lastpointy;

			for (unsigned short vertex = 0; vertex<4; vertex++) {

				//This exchange and negation corresponds to the matrix of a 90 degree rotation transformation.
				const short tempxdist = xdist;
				xdist = ydist * -1;
				ydist = tempxdist;

				curx += xdist;
				cury += ydist;

				if (curx < 0 || curx >= xsize || cury < 0 || cury >= ysize || !tokenarray[curx][cury])
					//No square here.
					break;
			}

			if (curx >= 0 && curx < xsize && cury >= 0 && cury < ysize && tokenarray[curx][cury])
				//Found a square. Score it using the squared Manhattan distance.
				score += (unsigned int)(pow(float(abs(selx - lastpointx) + abs(sely - lastpointy)+1), 2));

		}
	}

	return score;
}
