poj 1166(枚举)

总时间限制:
1000ms
内存限制:
65536kB
描述
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)
输入
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.
输出
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.
样例输入
3 3 0
2 2 2
2 1 2
样例输出
4 5 8 9

翻译:
问题描述:有9个时钟,排成一个33的矩阵。现在需要用最少的移动,将9个时钟的指针都拨到12点的位置。共允许有9种不同的移动。如右表所示,每个移动会将若干个时钟的指针沿顺时针方向拨动90度。
输入:从标准输入设备读入9个整数,表示各时钟指针的起始位置。0=12点、1=3点、2=6点、3=9点。
输出:输出一个移动序列,使得9个时钟的指针都指向12点。按照移动的序号由小到大,输出结果.
解题思路:
这题既然选择用枚举来做,首先我们要想好先要枚举什么,观题不难理解枚举9种移动的方法可以很好的
表示出来,另外,每种方法只能用三次应为第四次会回到起始点。
#include<iostream>
#include<string>
using namespace std ;
int a[10] ;//接收输入数据
int b[10] ;//接收选择方法的次数
int c[10] ;//存储结果
int tempsum ,sum=28;
string s  ;
int main()
{
int i ;
for(i=1;i<10;i++)
    cin>>a[i] ;

    for(b[1]=0;b[1]<=3;b[1]++)
    for(b[2]=0;b[2]<=3;b[2]++)
    for(b[3]=0;b[3]<=3;b[3]++)
    for(b[4]=0;b[4]<=3;b[4]++)
    for(b[5]=0;b[5]<=3;b[5]++)
    for(b[6]=0;b[6]<=3;b[6]++)
    for(b[7]=0;b[7]<=3;b[7]++)
    for(b[8]=0;b[8]<=3;b[8]++)
    for(b[9]=0;b[9]<=3;b[9]++)
   {
    c[1] = (b[1] + b[2] + b[4] + a[1])%4 ;
    c[2] = (b[1] + b[2] + b[3] + b[5]+a[2])%4 ;
    c[3] = (b[2] + b[3] + b[6]+a[3])%4 ;
    c[4] = (b[1] + b[4] + b[5] + b[7]+a[4])%4 ;
    c[5] = (b[1] + b[3] + b[5] + b[7] + b[9]+a[5])%4 ;
    c[6] = (b[3] + b[5] + b[6] + b[9]+a[6])%4 ;
    c[7] = (b[4] + b[7] + b[8]+a[7])%4 ;
    c[8] = (b[5] + b[7] + b[8] + b[9]+a[8])%4 ;
    c[9] = (b[6] + b[8] + b[9]+a[9])%4 ;
    if((c[1]+c[2]+c[3]+c[4]+c[5]+c[6]+c[7]+c[8]+c[9])==0)
    {
     tempsum = b[1]+b[2]+b[3]+b[4]+b[5]+b[6]+b[7]+b[8]+b[9] ;
     if(tempsum<sum)
     {
         sum = tempsum ;
         s = "" ;
       //  cout<<sum<<endl  ;
         for(i=1;i<=b[1];i++)
            s = s + "1 " ;
         for(i=1;i<=b[2];i++)
            s = s + "2 " ;
         for(i=1;i<=b[3];i++)
            s = s + "3 " ;
         for(i=1;i<=b[4];i++)
            s = s + "4 " ;
         for(i=1;i<=b[5];i++)
            s = s + "5 " ;
         for(i=1;i<=b[6];i++)
            s = s + "6 " ;
         for(i=1;i<=b[7];i++)
            s = s + "7 " ;
         for(i=1;i<=b[8];i++)
            s = s + "8 " ;
         for(i=1;i<=b[9];i++)
            s = s + "9 " ;

     }
    }
   }

  cout<<s<<endl ;

    return 0 ;
}
 

你可能感兴趣的:(poj)