POJ 1166 The clocks

本文介绍了一个经典的九宫格时钟谜题,并通过位运算结合BFS算法求解。该问题要求将一个3x3排列的九个时钟全部调整到12点方向,提供了九种旋转方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

The Clocks
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8928 Accepted: 3479

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

Source

 

/*

这题用位运算+BFS过的,效率不是很高,但是毕竟过了。网上利用数论的mod和矩阵运算有更快的方法,这个留着以后有时间再研究了

思路比较简单,关键是位运算,由于一共有9个clock,每个clock最多4种状态因此每个clock可以用2bit来表示,9个clock一共需要

18bit来表示,0-1位表示第一个clock的状态,2-3位表示第二个clock的状态,一次类推,这样一共需要2 ^ 18 - 1 = 262143种状态

*/

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值