UVa 10161 - Ant on a Chessboard

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

中文網址

Solution

取對角線的點當基準 (1,1)、(2,2)、…。
然後判斷其秒數在那一個對角線上點的前面,而看圖可得知對角線座標:

  • 為偶數的,ex.13,它就是 x 向左方 12,11,10 接著 9,8
  • 為奇數的,ex.7,它就是 y 向下 6,5 接著 4

以此去加減座標。

Code

UVa 10161
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
#include<cstdio>

int main()
{
int n;
while (scanf("%d", &n) && n)
{
int i, k;
//1,3,7,13,21,...
for (i = 0, k = 1; k <= n; k += 2 * i)
{
if (k == n)
{
printf("%d %d\n", i+1, i+1);
break;
}

i++;
}

int x, y, diff;
if (k != n)
{
x = y = i + 1;
diff = k - n;
if (i & 1)//3,13,...
{
if (diff <= i)
x -= diff;
else
{
diff -= i;
x = diff;
y--;
}
}
else//7,21,...
{
if (diff <= i)
y -= diff;
else
{
diff -= i;
y = diff;
x--;
}
}

printf("%d %d\n", x, y);
}
}

return 0;
}