poj 1166 拨钟问题
2016-01-25 15:22
316 查看
Description
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.
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
Sample Output
思路:还是和熄灯的那个差不多,只不过这个麻烦了一点。用两个数组表示钟的状态和操作,枚举操作1,2,3,剩下的操作中只有4可以改变A,5可以改变B,6可以改变C,所以操作4,5,6可以推出来。操作7由D的状态推出,8由G推出,9从EFHI中任一个都可以,我用的是I,然后再验证剩下的三个钟是否合法即可。
代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,success=0,first=1,clock[10],move[10]={0};
for(i=1;i<10;i++)
scanf("%d",&clock[i]);
while(!success){
success=1;
move[4]=(4-((clock[1]+move[1]+move[2])%4))%4;
move[5]=(4-((clock[2]+move[1]+move[2]+move[3])%4))%4;
move[6]=(4-((clock[3]+move[2]+move[3])%4))%4;
move[7]=(4-(clock[4]+move[1]+move[4]+move[5])%4)%4;
move[8]=(4-(clock[7]+move[4]+move[7])%4)%4;
move[9]=(4-(clock[9]+move[6]+move[8])%4)%4;
if((clock[5]+move[1]+move[3]+move[5]+move[7]+move[9])%4!=0)
success=0;
if((clock[6]+move[3]+move[5]+move[6]+move[9])%4!=0)
success=0;
if((clock[8]+move[5]+move[7]+move[8]+move[9])%4!=0)
success=0;
if(success)
break;
i=1; //利用三进制模拟操作123的状态
move[1]++;
while(move[i]>3&&i<4){
move[i]=0;
i++;
move[i]++;
}
}
for(i=1;i<10;i++){
while(move[i]-->0){
if(first){
first=0;
printf("%d",i);
}
else
printf(" %d",i);
}
}
printf("\n");
return 0;
}
|-------| |-------| |-------| | | | | | | | |---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
思路:还是和熄灯的那个差不多,只不过这个麻烦了一点。用两个数组表示钟的状态和操作,枚举操作1,2,3,剩下的操作中只有4可以改变A,5可以改变B,6可以改变C,所以操作4,5,6可以推出来。操作7由D的状态推出,8由G推出,9从EFHI中任一个都可以,我用的是I,然后再验证剩下的三个钟是否合法即可。
代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,success=0,first=1,clock[10],move[10]={0};
for(i=1;i<10;i++)
scanf("%d",&clock[i]);
while(!success){
success=1;
move[4]=(4-((clock[1]+move[1]+move[2])%4))%4;
move[5]=(4-((clock[2]+move[1]+move[2]+move[3])%4))%4;
move[6]=(4-((clock[3]+move[2]+move[3])%4))%4;
move[7]=(4-(clock[4]+move[1]+move[4]+move[5])%4)%4;
move[8]=(4-(clock[7]+move[4]+move[7])%4)%4;
move[9]=(4-(clock[9]+move[6]+move[8])%4)%4;
if((clock[5]+move[1]+move[3]+move[5]+move[7]+move[9])%4!=0)
success=0;
if((clock[6]+move[3]+move[5]+move[6]+move[9])%4!=0)
success=0;
if((clock[8]+move[5]+move[7]+move[8]+move[9])%4!=0)
success=0;
if(success)
break;
i=1; //利用三进制模拟操作123的状态
move[1]++;
while(move[i]>3&&i<4){
move[i]=0;
i++;
move[i]++;
}
}
for(i=1;i<10;i++){
while(move[i]-->0){
if(first){
first=0;
printf("%d",i);
}
else
printf(" %d",i);
}
}
printf("\n");
return 0;
}
相关文章推荐
- 枚举的用法详细总结
- PHP中Enum(枚举)用法实例详解
- c#入门之枚举和结构体使用详解(控制台接收字符串以相反的方向输出)
- 理解C#中的枚举(简明易懂)
- C#枚举中的位运算权限分配浅谈
- C++基础入门教程(四):枚举和指针
- FileShare枚举的使用小结(文件读写锁)
- Java枚举类用法实例
- 枚举窗口句柄后关闭所有窗口示例
- javascript模拟枚举的简单实例
- 深入剖析JavaScript中的枚举功能
- 枚举和宏的区别详细解析
- 深入理解C#中的枚举
- C#实现获取枚举中元素个数的方法
- 结合C++11的新特性来解析C++中的枚举与联合
- java中枚举的详细使用介绍
- java枚举的使用示例
- Java枚举详解及使用实例(涵盖了所有典型用法)
- Java的枚举类型使用方法详解
- 深入解析Swift编程中枚举类型的相关使用