hdu 1195 Open the Lock(BFS)

Problem Description
Now an emergent task for you is to open a password lock. The password is consisted of four digits. Each digit is numbered from 1 to 9. 
Each time, you can add or minus 1 to any digit. When add 1 to '9', the digit will change to be '1' and when minus 1 to '1', the digit will change to be '9'. You can also exchange the digit with its neighbor. Each action will take one step.

Now your task is to use minimal steps to open the lock.

Note: The leftmost digit is not the neighbor of the rightmost digit.
 
Input
The input file begins with an integer T, indicating the number of test cases. 

Each test case begins with a four digit N, indicating the initial state of the password lock. Then followed a line with anotther four dight M, indicating the password which can open the lock. There is one blank line after each test case.
 
Output
For each test case, print the minimal steps in one line.
 
Sample Input
2 1234 2144 1111 9999
 
Sample Output
2 4
 
简单BFS:
C++:
  1 #include<iostream>
  2 #include<queue>
  3 using namespace std;
  4 #define COPY(x,y) memcpy(x,y,sizeof(y))
  5 
  6 struct Nod
  7 {
  8     int step;
  9     char str[5];
 10 }d,p;
 11 
 12 int mark[10][10][10][10];
 13 
 14 void bfs(char *str,char *key)
 15 {
 16     queue<Nod> q;
 17     d.step=0;
 18 //    memcpy(d.str,str,sizeof(str));
 19     COPY(d.str,str);
 20     q.push(d);
 21     memset(mark,0,sizeof(mark));
 22     while(!q.empty())
 23     {
 24         p=q.front();
 25         q.pop();
 26         if(strcmp(p.str,key)==0)
 27             break;
 28     //    cout<<p.str<<endl;
 29         d.step=p.step+1;
 30         int i;
 31         for(i=0;i<4;i++)
 32         {
 33             COPY(d.str,p.str);
 34             if(d.str[i]-1=='0')
 35             {
 36                 d.str[i]='9';
 37                 if(!mark[d.str[0]-'0'][d.str[1]-'0'][d.str[2]-'0'][d.str[3]-'0'])
 38                 {
 39                     q.push(d);
 40                     mark[d.str[0]-'0'][d.str[1]-'0'][d.str[2]-'0'][d.str[3]-'0']=1;
 41                 }
 42             }
 43             else
 44             {
 45                 d.str[i]=d.str[i]-1;
 46                 if(!mark[d.str[0]-'0'][d.str[1]-'0'][d.str[2]-'0'][d.str[3]-'0'])
 47                 {
 48                     q.push(d);
 49                     mark[d.str[0]-'0'][d.str[1]-'0'][d.str[2]-'0'][d.str[3]-'0']=1;
 50                 }
 51             }
 52 
 53             COPY(d.str,p.str);
 54             if(d.str[i]=='9')
 55             {
 56                 d.str[i]='1';
 57                 if(!mark[d.str[0]-'0'][d.str[1]-'0'][d.str[2]-'0'][d.str[3]-'0'])
 58                 {
 59                     q.push(d);
 60                     mark[d.str[0]-'0'][d.str[1]-'0'][d.str[2]-'0'][d.str[3]-'0']=1;
 61                 }
 62             }
 63             else
 64             {
 65                 d.str[i]=d.str[i]+1;
 66                 if(!mark[d.str[0]-'0'][d.str[1]-'0'][d.str[2]-'0'][d.str[3]-'0'])
 67                 {
 68                     q.push(d);
 69                     mark[d.str[0]-'0'][d.str[1]-'0'][d.str[2]-'0'][d.str[3]-'0']=1;
 70                 }
 71             }
 72         }
 73 
 74         COPY(d.str,p.str);
 75         d.str[0]=p.str[1];
 76         d.str[1]=p.str[0];
 77         if(!mark[d.str[0]-'0'][d.str[1]-'0'][d.str[2]-'0'][d.str[3]-'0'])
 78         {
 79             q.push(d);
 80             mark[d.str[0]-'0'][d.str[1]-'0'][d.str[2]-'0'][d.str[3]-'0']=1;
 81         }
 82 
 83         COPY(d.str,p.str);
 84         d.str[1]=p.str[2];
 85         d.str[2]=p.str[1];
 86         if(!mark[d.str[0]-'0'][d.str[1]-'0'][d.str[2]-'0'][d.str[3]-'0'])
 87         {
 88             q.push(d);
 89             mark[d.str[0]-'0'][d.str[1]-'0'][d.str[2]-'0'][d.str[3]-'0']=1;
 90         }
 91 
 92         COPY(d.str,p.str);
 93         d.str[2]=p.str[3];
 94         d.str[3]=p.str[2];
 95         if(!mark[d.str[0]-'0'][d.str[1]-'0'][d.str[2]-'0'][d.str[3]-'0'])
 96         {
 97             q.push(d);
 98             mark[d.str[0]-'0'][d.str[1]-'0'][d.str[2]-'0'][d.str[3]-'0']=1;
 99         }
100     }
101 }
102 
103 int main()
104 {
105     int t;
106     while(cin>>t)
107     {
108         while(t--)
109         {
110             char str[5],key[5];
111             cin>>str>>key;
112             bfs(str,key);
113             cout<<p.step<<endl;
114         }
115     }
116     return 0;
117 }
原文地址:https://www.cnblogs.com/crazyapple/p/2932684.html