Total passenger garlic lock (BFS)

 

 

https://www.jisuanke.com/course/1797/121114

 

Description

Now an urgent task is to open a lock. Password of four digits, each digit are numbered from 1-9. Each may be incremented or decremented by 1 for any number. When 9 plus 1, numeral 1 becomes, 1 minus 1 when the number changes 9. You can also exchange numbers with their neighbors, every action denoted by step. Now your task is to use the smallest steps to open the lock.
Note: The leftmost digit is not the rightmost digit neighbors.

Input

The first four digits input row, that the initial state of the lock.

The second line input four digits represent unlock password.

Output

Output an integer, represents the minimum step.

Sample input

1234
2144

Sample Output

2 

 

This question is beginning to do the unexpected with the search.

However, this topic is still very easy to use bfs to complete.

 

This question mark for our method and the usual markers are not the same, we had reached in our state flag.

We can be accomplished is through the four-dimensional array of marks, referred to a group password of four digits is a state that: when the password is ABCD, the corresponding flag should be referred to asvis[a][b][c][d]=1。

Every time we have three possible operations, a first one to one plus one of the four digits, four digits for the second bit in a minus one, four digits for the third exchange.

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 using namespace std;
16 
17 struct node
18 {
19     int num[4];
20     int step;
21 }first,last;
22 int vis[11][11][11][11];
23 
24 void BFS()
25 {
26     queue<node> qe;
27     node t;
28     t=first;
29     qe.push(t);
30     vis[t.num[0]][t.num[1]][t.num[2]][t.num[3]]=1;
31     while(!qe.empty())
32     {
33         t=qe.front();
34         qe.pop();
35         if(t.num[0]==last.num[0]&&t.num[1]==last.num[1]&&t.num[2]==last.num[2]&&t.num[3]==last.num[3])
36         {    //BFS出口 
37             printf("%d\n",t.step);
38             return ;
39         }
40         for(int i=0;i<4;i++)//+1 
41         {
42             node next=t;
43             next.num[i]++;
44             if(next.num[i]==10) next.num[i]=1;
45             if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]])
46             {
47                 vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;
48                 next.step++;
49                 qe.push(next);
50             }
51         }
52         for(int i=0;i<4;i++)//-1
53         {
54             node next=t;
55             next.num[i]--;
56             if(next.num[i]==0) next.num[i]=9;
57             if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]])
58             {
59                 vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;
60                 next.step++;
61                 qe.push(next);
62             }
63         }
64         for(int i=0;i<3;i++)//交换 
65         {
66             node next=t;
67             swap(next.num[i],next.num[i+1]);
68             if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]])
69             {
70                 vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;
71                 next.step++;
72                 qe.push(next);
73             }
74         }    
75     }
76 }
77 
78 int main()
79 {
80     #ifdef DEBUG
81     freopen("sample.txt","r",stdin);
82     #endif
83     
84     char str1[5];
85     char str2[5];
86     scanf("%s %s",str1,str2);
87     for(int i=0;i<4;i++)
88     {
89         first.num[i]=str1[i]-'0';
90         last.num[i]=str2[i]-'0';
91     }
92     BFS();
93     
94     return 0;
95 }

 

 

 

 

-

Guess you like

Origin www.cnblogs.com/jiamian/p/12181691.html