poj 1166 The Clocks 暴力搜索

The Clocks
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15336 Accepted: 6219

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

IOI 1994


解题报告:

这个题要能看出,每种选择最多只能用3次,因为用4次等于没有用。

还要看出,选择的先后顺序可以是任意的。

因为没看出这个,先进行了一次BFS结果超时了。

后来暴力回溯过了。对每个选择枚举它的次数。


#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk    make_pair
#define _f     first
#define _s     second
using namespace std;
//const int INF=    ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;

//const int maxm=    ;

const int N=9;

int a[N+5];
int tmp[12];
int ans[12];
int mini;

int num[12]={0,4,3,4,3,5,3,4,3,4};
int ob[10][7]={{0},{0,1,2,4,5},{0,1,2,3},{0,2,3,5,6},{0,1,4,7},{0,2,4,5,6,8},{0,3,6,9},{0,4,5,7,8},{0,7,8,9},{0,5,6,8,9}};
void update(int cnt)
{
   for(int i=1;i<=N;i++)
   {
       if(a[i])  return;
   }
   if(cnt<mini)
   {
       memcpy(ans,tmp,sizeof tmp);
       mini=cnt;
   }
}
void dfs(int step,int cnt)
{
    if(step==N+1)  {update(cnt);return;}
  int x=num[step];
  for(int i=0;i<=3;i++)
  {
       for(int j=1;j<=x;j++)
     {
        int obj=ob[step][j];
        a[obj]=(a[obj]+i)%4;
     }
      tmp[step]=i;
      dfs(step+1,cnt+i);
       for(int j=1;j<=x;j++)
     {
        int obj=ob[step][j];
        a[obj]=(a[obj]-i+4)%4;
     }

  }



}
int main()
{
    for(int i=1;i<=N;i++)
       scanf("%d",&a[i]);
       mini=INF;
    dfs(1,0);

    bool ok=0;
    for(int i=1;i<=N;i++)
    {
        int x=ans[i];
        for(int j=1;j<=x;j++)
        {
            if(ok) printf(" %d",i);
            else printf("%d",i),ok=1;
        }
    }
    putchar('\n');



    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值