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. dapper基本操作

    https://www.cnblogs.com/vichin/p/9289969.html

  2. Django框架之ORM对表结构操作

    ORM的优点:(1)简单,不用自己写SQL语句 (2)开发效率高 ORM的缺点:对于不同的人写的代码,执行效率有差别 ORM的对应关系: 类  ---------->  数据表 对象------ ...

  3. UIView的API

    - (instancetype)initWithFrame:(CGRect)frame; 使用指定的框架矩形初始化并返回新分配的视图对象. - (instancetype)initWithCoder: ...

  4. linux克隆多台虚拟机后网络无法识别更无法连接上网的解决方法

    本人近期学习LINUX,想克隆多台CENTOS来练习在LINUX下安装软件,配置环境.因为对系统不是很熟悉,就想保持一个纯净版本,如果系统玩坏了就删除再重新克隆一个继续配置,以节省时间.后来发现,克隆 ...

  5. C++11常用特性介绍——for循环新用法

    一.for循环新用法——基于范围的for循环 for(元素类型 元素对象 : 容器对象) { //遍历 } 1)遍历字符串 std::string str = "hello world&qu ...

  6. FastDFS文件上传和下载流程

    文件上传流程 客户端上传文件后存储服务器将文件 ID 返回给客户端,此文件 ID 用于以后访问该文件的索引信息.文件索引信息包括:组名,虚拟磁盘路径,数据两级目录,文件名.  组名:文件上传后所在的 ...

  7. php虚拟主机配置( 输入网址 对应 ip地址)

    1.启动http_vhost.conf文件 在httpd-conf中,#virtual hosts 去掉前面的井号 # Includeconf/extra/httpd_vhost.conf 2.配置h ...

  8. Centos6.X创建Oracle用户

    第一步:创建数据表空间 第二步:创建临时表空间 第三步:创建用户并指定表空间 第四步:给用户授予权限 1.创建数据表空间 格式: create tablespace 表间名 datafile ‘数据文 ...

  9. toPlainString() 和 toString()(转载)

    函数 toPlainString() 和 toString() 对于 BigDecimal b ; (b=(0.4321)^ 20) String s = b.toPlainString() ; Sy ...

  10. PAT T1024 Currency Exchange Centers

    krustral算法求最少结点数的最小生成树,用优先队列实时排序,优先选择已经被选中的中心~ #include<bits/stdc++.h> using namespace std; ; ...