vijos P1016 北京2008的挂钟(poj 1166)
2014-09-02 21:43
211 查看
描述
![](https://www.vijos.org/static/ProblemImg/P1016.gif)
在2008北京奥运会雄伟的主会场的墙上,挂着如上图所示的3*3的九个挂钟(一开始指针即时针指向的位置请根据输入数据调整)。然而此次奥运会给与了大家一个机会,去用最少的移动操作改变上面的挂钟的时间全部为12点正(我们只考虑时针)。然而每一次操作并不是任意的,我们必须按照下面给出的列表对于挂钟进行改变。每一次操作我们给而且必须给指定的操作挂钟进行,每一个挂钟顺时针转动90度。列表如下:
操作 指定的操作挂钟 1 ABDE 2 ABC 3 BCEF 4 ADG 5 BDEFH 6 CFI 7 DEGH 8 GHI 9 EFHI
输入格式
你的程序按照标准的3*3格式读入,一共9个0-3的数。0代表12点,1代表3点,2代表6点,3代表9点。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.
输出格式
你的程序需要写出标准的输出。输出一个最短的能够使所有挂钟指向12点的移动操作序列,中间以空格隔开,最后有空格,加回车。这一条最短操作需要是所有最短操作中最小的,也就是说选择最小的第一个操作数,如果第一个操作数相等,那么选择最小的第二个操作数……以此类推。值得肯定的是,这一条操作序列是唯一的。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.
样例1
样例输入1
3 3 0 2 2 2 2 1 2
样例输出1
4 5 8 9
限制
各个测试点1s题解
恶补搜索之dfs+剪枝。我们可以想到每个操作使用四次就会变为原来的样子,所以每个操作使用次数为0~3次。这可以算作一种剪枝(不用搜重复无用的状态)。
其次我的收获是对于“搜索状态的设计”。怎么说呢。我开始想的是,我从初始状态开始,枚举选用9种中的哪一个操作(若选择次数已经=3就不能选),然后判断该状态是否到达终点,然后进入下一层;当某一层的所有状态都无法到达时,回溯到上一层。可是仔细想想,这样会组成许多重复的状态(因为操作顺序不影响最终效果)。于是搜索蒟蒻的我偷看了一下题解,我们可以将所有操作已用的次数记为数字s[],这样只要依次次枚举每个操作用了多少次,就可以不重复地搜到所有情况。
#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<cmath> #include<algorithm> using namespace std; int a[10],ct,b[10],s[10]; int move[9][9]={{1,1,0,1,1,0,0,0,0}, {1,1,1,0,0,0,0,0,0}, {0,1,1,0,1,1,0,0,0}, {1,0,0,1,0,0,1,0,0}, {0,1,0,1,1,1,0,1,0}, {0,0,1,0,0,1,0,0,1}, {0,0,0,1,1,0,1,1,0}, {0,0,0,0,0,0,1,1,1}, {0,0,0,0,1,1,0,1,1} }; void init() { int i; for(i=0;i<9;i++) scanf("%d",&a[i]); } bool dfs(int w) { int i,j,tag=1; for(i=0;i<9;i++) b[i]=a[i]; for(j=0;j<9;j++) for(i=0;i<9;i++) b[j]=(b[j]+move[i][j]*s[i])%4; for(i=0;i<9;i++) {if(b[i]) {tag=0; break;}} if(tag) {for(i=0;i<9;i++) for(j=0;j<s[i];j++) printf("%d ",i+1); printf("\n"); return true; } if(w>=9) return false; for(i=0;i<4;i++) {s[w]=i; if(dfs(w+1)) return true; } return false; } int main() { init(); dfs(0); return 0; }
相关文章推荐
- 【网络流】poj 3821 Destroying the bus stations -北京2008
- POJ 1166 The Clocks (暴搜)
- poj 1166 拨钟问题
- POJ 1166 The Clocks
- poj_1166_The Clocks(暴力)
- Vijos——T 1016 北京2008的挂钟 || 洛谷—— P1213 时钟
- POJ 1166 The Clocks
- poj 1166 The Clocks 记录路径的广搜
- POJ 1166 枚举或者高斯消元
- poj 1166 The Clocks
- POJ 1166 The Clocks (暴搜)
- poj 1166 The Clocks
- poj 1166 简单搜索
- POJ_1166_The Clocks
- poj 1166 敌兵布阵 线段树
- POJ 1166 The clocks
- poj-1166
- POJ The Clocks 1166
- POJ 1166 The Clocks
- poj_1166 The Clocks(高斯消元)