Monday, May 4, 2015

UVa 10920 - Spiral Tap

// UVa 10920 - Spiral Tap

#include <iostream>
#include <cmath>
using namespace std;

int move[4][2] = { { 0, -1 }, { -1, 0 }, { 0, 1 }, { 1, 0 } };

int main() {
	int sz, p;
	while ((cin >> sz >> p) && (sz || p)) {
		int mr = (sz + 1) / 2, mc = (sz + 1) / 2;
		int root = floor(pow(p, 0.5));
		if (root % 2 == 0)
			root--;
		int delta = (root - 1) / 2;
		int r = mr + delta;
		int c = mc + delta;
		int left = p - root * root;
		if (left > 0) {
			r++;
			c++;
			int dir = 0;
			while (left >= root + 1) {
				r += move[dir][0] * (root + 1);
				c += move[dir][1] * (root + 1);
				left -= (root + 1);
				dir++;
			}
			if (left) {
				r += move[dir][0] * (left);
				c += move[dir][1] * (left);
			}
		}
		cout << "Line = " << r << ", column = " << c << "." << endl;

	}
	return 0;
}

No comments:

Post a Comment