forked from Diusrex/UVA-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10161 Ant on a Chessboard.cpp
59 lines (49 loc) · 1.41 KB
/
10161 Ant on a Chessboard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <algorithm>
#include <iostream>
using namespace std;
const int NumDiagonalNeeded = 44722 + 3;
int diagonalValues[NumDiagonalNeeded];
void MoveAlong(int &primary, int &secondary, int current, int wanted)
{
int left = wanted - current;
if (primary - 1 >= left)
primary -= left;
else
{
primary = left - primary + 1;
++secondary;
}
}
int main()
{
for (int i = 0; i < NumDiagonalNeeded; ++i)
diagonalValues[i] = i * i - i + 1;
int N;
while (cin >> N, N)
{
if (N == 2)
{
// Behaves differently
cout << "1 2\n";
continue;
}
int *diagonal = lower_bound(diagonalValues + 1, diagonalValues + NumDiagonalNeeded, N);
int diagonalPosition = diagonal - diagonalValues, diagonalVal = *diagonal;
// May need to do some shifting
if (diagonalVal != N)
{
--diagonalPosition;
diagonalVal = diagonalValues[diagonalPosition];
}
int row = diagonalPosition, column = diagonalPosition;
if (diagonalVal != N)
{
// Changes the row first
if (diagonalPosition & 1)
MoveAlong(row, column, diagonalVal, N);
else
MoveAlong(column, row, diagonalVal, N);
}
cout << row << ' ' << column << '\n';
}
}