USACO 1.4 clocks

1 /*
2 ID: jiafeim1
3 PROG: clocks
4 LANG: C++
5 */
6 #include <iostream>
7 #include <fstream>
8 #include <algorithm>
9 #include <queue>
10
11 using namespace std;
12
13
14 int ward[9][9]={
15 {1,1,0,1,1,0,0,0,0},
16 {1,1,1,0,0,0,0,0,0},
17 {0,1,1,0,1,1,0,0,0},
18 {1,0,0,1,0,0,1,0,0},
19 {0,1,0,1,1,1,0,1,0},
20 {0,0,1,0,0,1,0,0,1},
21 {0,0,0,1,1,0,1,1,0},
22 {0,0,0,0,0,0,1,1,1},
23 {0,0,0,0,1,1,0,1,1}
24 };
25
26 int rank[9]={1,4,16,64,256,1024,4096,16384,65536};
27 bool haveDo[262144]={false};
28 struct work
29 {
30 long clock_state;
31 long step_state;
32 work(long clock,long step)
33 {
34 clock_state = clock;
35 step_state = step;
36 }
37 work(){}
38 };
39 std::queue<work> wq;
40 int main()
41 {
42 ofstream fout ("clocks.out");
43 ifstream fin ("clocks.in");
44 long start_state = 0;
45 long temp;
46 for(int i = 0 ; i!=9;++i)
47 {
48 fin>>temp;
49 temp/=3;
50 temp%=4;
51 start_state = start_state*4+temp;
52 }
53 wq.push(work(start_state,0));
54 haveDo[0]=true;
55 work now;
56 long res = 0;
57 while(!wq.empty())
58 {
59 now = wq.front();
60 wq.pop();
61 if(now.clock_state == 0)
62 {
63 res = now.step_state;
64 break;
65 }
66 if(now.step_state == 262143) continue;
67 for(int i = 0 ;i!=9;++i)
68 {
69 long temp_step = now.step_state;
70 temp_step = (temp_step/rank[i])%4;
71 if(temp_step == 3) continue;
72 temp_step = now.step_state+rank[i];
73
74 if(haveDo[temp_step]) continue;
75 else
76 {
77 haveDo[temp_step] = true;
78 long temp_clock = now.clock_state;
79 for(int j =0;j!=9;++j)
80 {
81 if(ward[i][j]==0) continue;
82 long js = (temp_clock/rank[8-j])%4;
83 if(js == 3)
84 {
85 temp_clock -=3 * rank[8-j];
86 }
87 else
88 {
89
90 temp_clock += rank[8-j];
91 }
92
93 }
94 wq.push(work(temp_clock,temp_step));
95 }
96
97 }
98 }
99
100 bool space = false;
101 for(int i=0;i!=9&&res!=0;++i)
102 {
103 long l = res%4;
104 res/=4;
105 for(int j =0;j!=l;++j)
106 {
107 if(space) fout<<" ";
108 fout<<i+1;
109 space = true;
110 }
111 }
112 fout<<endl;
113 fin.close();
114 fout.close();
115 return 0;
116 }
posted @ 2011-05-02 00:15  幻魇  阅读(146)  评论(0编辑  收藏  举报