poj 1166 简单搜索

The Clocks
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12508 Accepted: 4949

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


思路:

       非常简单的一道题啊。。。为什么我做了这么久咧。。。。

       暴搜就可以过的,直接九层for循环,再加判断就行啦。本人还在想着怎么用BFS做,结果BFS出来还TLE。

       看来以后的思路要拓宽些了。

       因为无论是1~9哪个操作,只要知道进行了哪些操作,进行了几次,不需要管其中的顺序,就可以直接得到最后的结果,又因为每个操作

做四次就回到了原来,所以1~9的每个操作,最多做4次。这样一来,就是9重for循环,每重做4次。。。

 

代码实现:

 

View Code
 1 #include<iostream>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 int a[2000000][10];
 6 int doing[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, 
 7 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};
 8 int main(){
 9     int now[10];
10     int num[10];
11     while(cin>>now[0]>>now[1]>>now[2]>>now[3]>>now[4]>>now[5]>>now[6]>>now[7]>>now[8]){
12        memset(num,0,sizeof(num));
13        int times=0;
14        for(int i1=0;i1<4;i1++){
15                num[0]=i1;
16                for(int i2=0;i2<4;i2++){
17                        num[1]=i2;
18                        for(int i3=0;i3<4;i3++){
19                                 num[2]=i3;
20                                for(int i4=0;i4<4;i4++){
21                                         num[3]=i4;
22                                        for(int i5=0;i5<4;i5++){
23                                                 num[4]=i5;
24                                                for(int i6=0;i6<4;i6++){
25                                                         num[5]=i6;
26                                                        for(int i7=0;i7<4;i7++){
27                                                                 num[6]=i7;
28                                                                for(int i8=0;i8<4;i8++){
29                                                                         num[7]=i8;
30                                                                        for(int i9=0;i9<4;i9++){
31                                                                              num[8]=i9;
32                                                                             int flag=0;
33                                                                             for(int i10=0;i10<9;i10++){
34                                                                                     int temp=now[i10];
35                                                                                     for(int j=0;j<9;j++){
36                                                                                          now[i10]+=(doing[j][i10]*num[j]);
37                                                                
38                                                                                          }
39                                                                                       
40                                                                                     if((now[i10]%4)!=0){
41                                                                                         flag=1;
42                                                                                         }
43                                                                                     now[i10]=temp;
44                                                                                         }
45                                                                             if(flag==0){
46                                                                                  for(int k=0;k<9;k++){
47                                                                                          a[times][k]=num[k];
48                                                                                          }
49                                                                                 
50                                                                                  times++;
51                                                                                  }
52                                                                             }
53                                                                                  }
54                                                                                  }
55                                                                                  }
56                                                                                  }
57                                                                                  }
58                                                                                  }
59                                                                                  }
60                                                                            }
61        int min=10000000;
62        int temp2=0;
63        for(int g=0;g<times;g++){
64                int sum=0;
65                for(int i=0;i<9;i++){
66                sum+=a[g][i];
67                }
68                if(min>sum){
69                     min=sum;
70                     temp2=g;
71                     }
72                     }
73        int time1=0;
74        for(int j=0;j<9;j++){
75                int k;
76                for(k=0;k<a[temp2][j];k++){
77                        if(time1!=0)
78                               cout<<" ";
79                         cout<<j+1;
80                         time1++;
81                        }            
82                        }
83        cout<<endl;
84        }
85     return 0;
86 }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值