您的位置:首页 > 其它

USACO Section 1.4 The Clocks

2012-02-08 18:42 399 查看
还是感觉枚举好,搜索啥的越想越麻烦。

因为表是圆的,每个表转四下会回到原来的位置,所以,对于每个表,最多转3次,或者不转,也就是说,对于9个表,每个表有四种可能,那么,一共有4^9种可能,依次枚举,第一个符合条件的即是最小的

/* ID:linyvxi1
PROB:clocks
LANG:C++
*/
#include <stdio.h>
#include<string>
#include<algorithm>
using namespace std;

int ans[10], num[10], tmp[10];

int
main ()
{
freopen ("clocks.in", "r", stdin);
freopen ("clocks.out", "w", stdout);
int i;
for (i = 1; i <= 9; i++)
{
scanf ("%d", &ans[i]);
ans[i] /= 3;
}
for (num[1] = 0; num[1] <= 3; num[1]++)    //每一种操作口令的执行次数都不会超过3次
for (num[2] = 0; num[2] <= 3; num[2]++)
for (num[3] = 0; num[3] <= 3; num[3]++)
for (num[4] = 0; num[4] <= 3; num[4]++)
for (num[5] = 0; num[5] <= 3; num[5]++)
for (num[6] = 0; num[6] <= 3; num[6]++)
for (num[7] = 0; num[7] <= 3; num[7]++)
for (num[8] = 0; num[8] <= 3; num[8]++)
for (num[9] = 0; num[9] <= 3; num[9]++)
{
tmp[1] = (ans[1] + num[1] + num[2] + num[4]) % 4;
tmp[2] =
(ans[2] + num[1] + num[2] + num[3] + num[5]) % 4;
tmp[3] = (ans[3] + num[2] + num[3] + num[6]) % 4;
tmp[4] =
(ans[4] + num[1] + num[4] + num[5] + num[7]) % 4;
tmp[5] =
(ans[5] + num[1] + num[3] + num[5] + num[7] +
num[9]) % 4;
tmp[6] =
(ans[6] + num[3] + num[5] + num[6] + num[9]) % 4;
tmp[7] = (ans[7] + num[4] + num[7] + num[8]) % 4;
tmp[8] =
(ans[8] + num[5] + num[7] + num[8] + num[9]) % 4;
tmp[9] = (ans[9] + num[6] + num[8] + num[9]) % 4;
if (tmp[1] + tmp[2] + tmp[3] + tmp[4] + tmp[5] +
tmp[6] + tmp[7] + tmp[8] + tmp[9] == 0)
{
bool s = false;
for (i = 1; i <= 9; i++)
{
if (num[i])
{
int j;
for (j = 1; j <= num[i]; j++)
{
if (!s)
s = true;
else
putchar (' ');
printf ("%d", i);
}
}
}

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