您的位置:首页 > 其它

usaco 4.4 Shuttle Puzzle 找规律

2008-08-25 16:23 211 查看
可以用宽搜尝试,结果n>9时发现严重超时,超空间。观察一下输出结果

n = 9

2: 9 11

3: 12 10 8

4: 7 9 11 13

5: 14 12 10 8 6

6: 5 7 9 11 13 15

7: 16 14 12 10 8 6 4

8: 3 5 7 9 11 13 15 17

9: 18 16 14 12 10 8 6 4 2

10: 1 3 5 7 9 11 13 15 17 19

9: 18 16 14 12 10 8 6 4 2

8: 3 5 7 9 11 13 15 17

7: 16 14 12 10 8 6 4

6: 5 7 9 11 13 15

5: 14 12 10 8 6

4: 7 9 11 13

3: 12 10 8

2: 9 11

1: 10

n = 8

2: 8 10

3: 11 9 7

4: 6 8 10 12

5: 13 11 9 7 5

6: 4 6 8 10 12 14

7: 15 13 11 9 7 5 3

8: 2 4 6 8 10 12 14 16

9: 17 15 13 11 9 7 5 3 1

8: 2 4 6 8 10 12 14 16

7: 15 13 11 9 7 5 3

6: 4 6 8 10 12 14

5: 13 11 9 7 5

4: 6 8 10 12

3: 11 9 7

2: 8 10

1: 9

很有规律,那么输出皆可~~

/*
PROG: shuttle
LANG: C++
ID: heben991
*/
#include <iostream>

#include <algorithm>
using namespace std;

const int M = 1000000;

int a[M];
int main()
{
int i, j, k, n, x, sign;

freopen("shuttle.in","r",stdin);
freopen("shuttle.out","w",stdout);
scanf("%d", &n);
sign = 1;
x = n;
for(i = 2; i <= n+1; ++i)
{
for(j = 1; j <= i; ++j)
{
a[++a[0]] = x;
if(j!=i) x += 2*sign;
else x += sign;
//printf("%d ", a[a[0]]);

}
//puts("");
sign *= -1;
}

x += 2*sign;

for(i = n; i >= 1; --i)
{
for(j = 1; j <= i; ++j)
{
a[++a[0]] = x;
if(j!=i) x += 2*sign;
else x -= sign;
//printf("%d ", a[a[0]]);

}
//puts("");
sign *= -1;
}

j = 0;
for(i = 1; i <= a[0]; ++i)
{
printf("%d",a[i]);
++j;
if(j==20 || i==a[0])
{
j = 0;
putchar('/n');
}
else putchar(' ');
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: