pc110902 Playing with Wheels (BFS)

Playing with Wheels

 
 

Consider the following mathematical machine. Digits ranging from 0 to 9 are printed consecutively (clockwise) on the periphery of each wheel. The topmost digits of the wheels form a four-digit integer. For example, in the following figure the wheels form the integer 8,056. Each wheel has two buttons associated with it. Pressing the button marked with a left arrow rotates the wheel one digit in the clockwise direction and pressing the one marked with the right arrowrotates it by one digit in the opposite direction.

We start with an initial configuration of the wheels, with the topmost digits forming the integer S1S2S3S4. You will be given a set of n forbidden configurations Fi1Fi2Fi3Fi4 ( 1$ \le$i$ \le$n) and a target configuration T1T2T3T4. Your job is to write a program to calculate the minimum number of button presses required to transform the initial configuration to the target configuration without passing through a forbidden one.

Input

The first line of the input contains an integer Ngiving the number of test cases. A blank line then follows.

The first line of each test case contains the initial configuration of the wheels, specified by four digits. Two consecutive digits are separated by a space. The next line contains the target configuration. The third line contains an integer n giving the number of forbidden configurations. Each of the following nlines contains a forbidden configuration. There is a blank line between two consecutive input sets.

Output

For each test case in the input print a line containing the minimum number of button presses required. If the target configuration is not reachable print ``-1''.

Sample Input

2

8 0 5 6
6 5 0 8
5
8 0 5 7
8 0 4 7
5 5 0 8
7 5 0 8
6 4 0 8

0 0 0 0
5 3 1 7
8
0 0 0 1
0 0 0 9
0 0 1 0
0 0 9 0
0 1 0 0
0 9 0 0
1 0 0 0
9 0 0 0

Sample Output

14
-1

分析:BFS总思路是:如果现在已经变化了k次,则把所以改变k+1次的状态入队。
有两个比较好的思路是:对于每个状态我都使用一个整数来存储,使用完后就把这个整数标记,所以很快就可以判断下个状态是否已经出现过。
View Code
 1 #include<iostream>
 2 #include<cstring>
 3 #include<queue>
 4 #define N 10010
 5 using namespace std;
 6 
 7 bool visited[N],flag[N];
 8 int e;
 9 struct SS{int num,count;};
10 
11 int fun(int a,int b,int c,int d)
12 {
13     return a*1000+b*100+c*10+d;
14 }
15 
16 int BFS(SS s)
17 {
18     queue<SS>Q;
19     while(!Q.empty()) Q.pop();
20     Q.push(s);
21     int a,b,c,d;
22     SS t,tt;
23     while(!Q.empty())
24     {
25         t=Q.front();Q.pop();
26         if(t.num==e) return t.count;
27         a=t.num/1000;b=t.num/100%10;c=t.num/10%10;d=t.num%10;
28         tt.count=t.count+1;
29 
30         tt.num=fun(a,b,c,(d+1)%10);
31         if(!visited[tt.num]&&!flag[tt.num]) Q.push(tt);
32         visited[tt.num]=true;
33         tt.num=fun(a,b,c,(10+d-1)%10);
34         if(!visited[tt.num]&&!flag[tt.num]) Q.push(tt);
35         visited[tt.num]=true;
36 
37         tt.num=fun(a,b,(c+1)%10,d);
38         if(!visited[tt.num]&&!flag[tt.num]) Q.push(tt);
39         visited[tt.num]=true;
40         tt.num=fun(a,b,(10+c-1)%10,d);
41         if(!visited[tt.num]&&!flag[tt.num]) Q.push(tt);
42         visited[tt.num]=true;
43 
44         tt.num=fun(a,(b+1)%10,c,d);
45         if(!visited[tt.num]&&!flag[tt.num]) Q.push(tt);
46         visited[tt.num]=true;
47         tt.num=fun(a,(10+b-1)%10,c,d);
48         if(!visited[tt.num]&&!flag[tt.num]) Q.push(tt);
49         visited[tt.num]=true;
50 
51         tt.num=fun((a+1)%10,b,c,d);
52         if(!visited[tt.num]&&!flag[tt.num]) Q.push(tt);
53         visited[tt.num]=true;
54         tt.num=fun((10+a-1)%10,b,c,d);
55         if(!visited[tt.num]&&!flag[tt.num]) Q.push(tt);
56         visited[tt.num]=true;
57     }
58     return -1;
59 }
60 
61 int main()
62 {
63     int test,a,b,c,d,n,i;
64     cin>>test;
65     SS s;
66     while(test--)
67     {
68         cin>>a>>b>>c>>d;
69         s.num=fun(a,b,c,d);
70         s.count=0;
71         cin>>a>>b>>c>>d;
72         e=fun(a,b,c,d);
73         cin>>n;
74         memset(visited,false,sizeof(visited));
75         memset(flag,false,sizeof(flag));
76         for(i=0;i<n;i++)
77         {
78             cin>>a>>b>>c>>d;
79             flag[fun(a,b,c,d)]=true;
80         }
81         cout<<BFS(s)<<endl;
82     }
83     return 0;
84 }

 

posted @ 2012-04-25 14:15  mtry  阅读(346)  评论(0编辑  收藏  举报