The Clocks
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15357   Accepted: 6230

Description

|-------|    |-------|    |-------|

|       |    |       |    |   |   |

|---O   |    |---O   |    |   O   |

|       |    |       |    |       |

|-------|    |-------|    |-------|

    A            B            C    

|-------|    |-------|    |-------|

|       |    |       |    |       |

|   O   |    |   O   |    |   O   |

|   |   |    |   |   |    |   |   |

|-------|    |-------|    |-------|

    D            E            F    

|-------|    |-------|    |-------|

|       |    |       |    |       |

|   O   |    |   O---|    |   O   |

|   |   |    |       |    |   |   |

|-------|    |-------|    |-------|

    G            H            I    

(Figure 1)

There are nine clocks in a 3*3 array (figure 1). The goal is to return all the dials to 12 o'clock with as few moves as possible. There are nine different allowed ways to turn the dials on the clocks. Each such way is called a move. Select for each move a number
1 to 9. That number will turn the dials 90' (degrees) clockwise on those clocks which are affected according to figure 2 below.

Move   Affected clocks

 1         ABDE

 2         ABC

 3         BCEF

 4         ADG

 5         BDEFH

 6         CFI

 7         DEGH

 8         GHI

 9         EFHI    

   (Figure 2)

Input

Your program is to read from standard input. Nine numbers give the start positions of the dials. 0=12 o'clock, 1=3 o'clock, 2=6 o'clock, 3=9 o'clock.

Output

Your program is to write to standard output. Output a shortest sorted sequence of moves (numbers), which returns all the dials to 12 o'clock. You are convinced that the answer is unique.

Sample Input

3 3 0
2 2 2
2 1 2

Sample Output

4 5 8 9

最近在做高斯消元的专题,再一次感觉智商不够用,各种不爽。

这个题目就是不爽之一。。。题意是有9个表,每个表用0 1 2 3 表示这个指针的状态,然后有9种操作,每个操作包含的字母其含义代表把对应位置上的表指针顺时针拨动90度。问最终要通过什么方式让9个表都是0状态。

一开始没有想到是高斯消元。然后想了想,发现这个题目每个操作最多就3次,4次就和没有操作是一样的了。然后还有一点是与操作的顺序没有关系。于是就写了一个九层循环的暴力。。。

代码:

#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std; int val[15];
int num[15]; int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); int i, sum;
int i1, i2, i3, i4, i5, i6, i7, i8, i9;
int j1, j2, j3, j4, j5, j6, j7, j8, j9; for (i = 1; i <= 9; i++)
scanf("%d", &val[i]); for (i1 = 0; i1 < 4; i1++)
{
for (i2 = 0; i2 < 4; i2++)
{
for (i3 = 0; i3 < 4; i3++)
{
for (i4 = 0; i4 < 4; i4++)
{
for (i5 = 0; i5 < 4; i5++)
{
for (i6 = 0; i6 < 4; i6++)
{
for (i7 = 0; i7 < 4; i7++)
{
for (i8 = 0; i8 < 4; i8++)
{
for (i9 = 0; i9 < 4; i9++)
{
num[1] = (val[1] + i1 + i2 + i4) % 4;
num[2] = (val[2] + i1 + i2 + i3 + i5) % 4;
num[3] = (val[3] + i2 + i3 + i6) % 4;
num[4] = (val[4] + i1 + i4 + i5 + i7) % 4;
num[5] = (val[5] + i1 + i3 + i5 + i7 + i9) % 4;
num[6] = (val[6] + i3 + i5 + i6 + i9) % 4;
num[7] = (val[7] + i4 + i7 + i8) % 4;
num[8] = (val[8] + i5 + i7 + i8 + i9) % 4;
num[9] = (val[9] + i6 + i8 + i9) % 4; sum = 0;
for (i = 1; i <= 9; i++)
sum += num[i];
if (sum == 0)
{
for (j1 = 1; j1 <= i1; j1++)printf("1 ");
for (j2 = 1; j2 <= i2; j2++)printf("2 ");
for (j3 = 1; j3 <= i3; j3++)printf("3 ");
for (j4 = 1; j4 <= i4; j4++)printf("4 ");
for (j5 = 1; j5 <= i5; j5++)printf("5 ");
for (j6 = 1; j6 <= i6; j6++)printf("6 ");
for (j7 = 1; j7 <= i7; j7++)printf("7 ");
for (j8 = 1; j8 <= i8; j8++)printf("8 ");
for (j9 = 1; j9 <= i9; j9++)printf("9 ");
return 0;
}
}
}
}
}
}
}
}
}
}
//system("pause");
return 0;
}

后来发现里面的就是高斯消元列一个mod4的方程组,唉,踏踏实实下来好好提高自己吧,别眼高手低了。

高斯消元解法,4不是质数,所以高斯消元里面要改动一些,不能再每列种寻找最大值了,找到不为0的直接跳出。

代码:

#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std; int x[15];
int num[15];
int val[15][25];
bool free_x[305];//标记是否是不确定的变元 inline int gcd(int a, int b)
{
int t;
while (b != 0)
{
t = b;
b = a%b;
a = t;
}
return a;
} inline int lcm(int a, int b)
{
return a / gcd(a, b)*b;//先除后乘防溢出
} int Gauss(int equ, int var)
{
int i, j, k;
int max_r;//当前这列绝对值最大的行
int col;//当前处理的列
int ta, tb;
int LCM;
int temp;
int free_x_num;
int free_index; for (int i = 0; i <= var; i++)
{
x[i] = 0;
free_x[i] = true;
}
//转换为阶梯阵
col = 0;//当前处理的列
for (k = 0; k < equ&&col < var; k++, col++)
{
//枚举当前处理的行
max_r = k;
for (max_r = k; max_r < equ; max_r++)//改动在这里!!!!!
{
if (abs(val[max_r][col]))
{
break;
}
}
if (max_r != k)
{//与第k行交换
for (j = k; j < var + 1; j++)
swap(val[k][j], val[max_r][j]);
}
if (val[k][col] == 0)
{
k--;
continue;
}
for (i = k + 1; i < equ; i++)
{//枚举要删去的行
if (val[i][col] != 0)
{
ta = abs(val[k][col]);
tb = abs(val[i][col]);
if (val[i][col] * val[k][col] < 0)
tb = -tb; for (j = col; j < var + 1; j++)
{
val[i][j] = (((val[i][j] * ta) % 4 - (val[k][j] * tb) % 4) % 4 + 4) % 4;
}
}
}
}
for (i = var - 1; i >= 0; i--)
{
temp = val[i][var];
for (j = i + 1; j < var; j++)
{
if (val[i][j] != 0)
{
temp = temp - (val[i][j] * x[j]) % 4;
temp = (temp % 4 + 4) % 4;
}
}
for (x[i] = 0; x[i] < 4; x[i]++)
{
if ((x[i] * val[i][i] + 4) % 4 == temp)
{
break;
}
}
}
return 0;
}
int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); int i, sum;
int j1, j2, j3, j4, j5, j6, j7, j8, j9; for (i = 1; i <= 9; i++)
scanf("%d", &num[i]); memset(val, 0, sizeof(val));
for (i = 0; i < 9; i++)
{
val[i][9] = (4 - num[i + 1]) % 4;
if (i == 0)
{
val[i][0] = 1;
val[i][1] = 1;
val[i][3] = 1;
}
else if (i == 1)
{
val[i][0] = 1;
val[i][1] = 1;
val[i][2] = 1;
val[i][4] = 1;
}
else if (i == 2)
{
val[i][1] = 1;
val[i][2] = 1;
val[i][5] = 1;
}
else if (i == 3)
{
val[i][0] = 1;
val[i][3] = 1;
val[i][4] = 1;
val[i][6] = 1;
}
else if (i == 4)
{
val[i][0] = 1;
val[i][2] = 1;
val[i][4] = 1;
val[i][6] = 1;
val[i][8] = 1;
}
else if (i == 5)
{
val[i][2] = 1;
val[i][4] = 1;
val[i][5] = 1;
val[i][8] = 1;
}
else if (i == 6)
{
val[i][3] = 1;
val[i][6] = 1;
val[i][7] = 1;
}
else if (i == 7)
{
val[i][4] = 1;
val[i][6] = 1;
val[i][7] = 1;
val[i][8] = 1;
}
else if (i == 8)
{
val[i][5] = 1;
val[i][7] = 1;
val[i][8] = 1;
}
} Gauss(9, 9); for (j1 = 1; j1 <= x[0]; j1++)printf("1 ");
for (j2 = 1; j2 <= x[1]; j2++)printf("2 ");
for (j3 = 1; j3 <= x[2]; j3++)printf("3 ");
for (j4 = 1; j4 <= x[3]; j4++)printf("4 ");
for (j5 = 1; j5 <= x[4]; j5++)printf("5 ");
for (j6 = 1; j6 <= x[5]; j6++)printf("6 ");
for (j7 = 1; j7 <= x[6]; j7++)printf("7 ");
for (j8 = 1; j8 <= x[7]; j8++)printf("8 ");
for (j9 = 1; j9 <= x[8]; j9++)printf("9 "); //system("pause");
return 0;
}

POJ 1166:The Clocks的更多相关文章

  1. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  2. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  3. Poj 1166 The Clocks(bfs)

    题目链接:http://poj.org/problem?id=1166 思路分析:题目要求求出一个最短的操作序列来使所有的clock为0,所以使用bfs: <1>被搜索结点的父子关系的组织 ...

  4. POJ 1166 The Clocks (暴搜)

    发现对这样的模拟题根本没啥思路了,本来准备用bfs的.可是结果超时了,这是參考别的人代码写的: #include <stdio.h> #include <iostream> # ...

  5. POJ 1166 The Clocks (爆搜 || 高斯消元)

    题目链接 题意: 输入提供9个钟表的位置(钟表的位置只能是0点.3点.6点.9点,分别用0.1.2.3)表示.而题目又提供了9的步骤表示可以用来调正钟的位置,例如1 ABDE表示此步可以在第一.二.四 ...

  6. POJ 1166 The Clocks

    高斯消元法第四个冠军,这个称号是非常令人兴奋~~ 题目大意: 给出9个钟表的状态.给出九种操作,问最少要操作几次能把全部的钟表调回12点. 解题思路: 对于9个钟表分别列方程,然后高斯消元就可以.因为 ...

  7. POJ 1166 The Clocks 高斯消元 + exgcd(纯属瞎搞)

    依据题意可构造出方程组.方程组的每一个方程格式均为:C1*x1 + C2*x2 + ...... + C9*x9 = sum + 4*ki; 高斯消元构造上三角矩阵,以最后一个一行为例: C*x9 = ...

  8. POJ 1166 The Clocks [BFS] [位运算]

    1.题意:有一组3*3的只有时针的挂钟阵列,每个时钟只有0,3,6,9三种状态:对时针阵列有9种操作,每种操作只对特点的几个时钟拨一次针,即将时针顺时针波动90度,现在试求从初试状态到阵列全部指向0的 ...

  9. POJ 1459:Power Network(最大流)

    http://poj.org/problem?id=1459 题意:有np个发电站,nc个消费者,m条边,边有容量限制,发电站有产能上限,消费者有需求上限问最大流量. 思路:S和发电站相连,边权是产能 ...

随机推荐

  1. C++中使用sstream进行类型转换(数字字符串转数字、数字转数字字符串)

    1.sstream知识 sstream即字符串流.在使用字符串流sstream时,需要先引入相应的头文件 "#include <sstream>" 基本操作 // 引入 ...

  2. spring security 方法权限使用

    前面我们讲过了使用<security:intercept-url>配置url的权限访问,下面我们讲解一下基于方法的权限使用默认情况下, Spring Security 并不启用方法级的安全 ...

  3. Plastic Sprayers Manufacturer - The Basic Components Of A Spray Bottle

    From cleaning to personal beauty, many people use spray bottles every day, but few people know how t ...

  4. RS232与RS485

    1.RS232实物图与引脚图? 2.RS485实物图与引脚图?

  5. JavaSE复习~运算符与表达式

    运算符 运算符:进行特定操作的符号 表达式:用运算符进行操作的式子 算术运算符 首先是加减乘除:+.-.*./还有取余:% 整数进行算术操作得到的还是整数,例如整数使用 / 得到的是整数(商的整数部分 ...

  6. 重新梳理IT知识之java-03循环

    引用变量时要给变量赋值,如果循环进不去就会报错. 一.循环结构的四要素 1.初始化条件 2.循环条件 ---> 是Boolean类型 3.循环体 4.迭代条件 说明:通常情况下,循环结束都是因为 ...

  7. 深入学习二叉树(07)B树

    问题背景 在大型的数据库存储中,实现索引查找,如果采用二叉查找树的查找的话,由于节点的存储数据是有限的,这样如果数据库很大的话,就会导致树的深度过大从而造成磁盘 IO 操作过于频繁,就会造成效率低下 ...

  8. 如何解决Serv-U管理密码忘记

    如何解决Serv-U管理密码忘记 2016-06-17 15:46:48 2581次 解决方法: 点击“FTP服务器”,停止FTP服务器.进入Serv-U安装目录,默认C:Program FilesS ...

  9. Linux kali国内源

    命令行:leafpad /etc/apt/sources.list 将原来的内容注释掉,添加以下代码 #中科大 deb http://mirrors.ustc.edu.cn/kali kali-rol ...

  10. nodejs的forEach不支持break打断