Open the Lock
64-bit integer IO format: %I64d Java class name: Main
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
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
Sample Input
2 1234 2144 1111 9999
Sample Output
2 4
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 struct node{ 18 int key[4],step; 19 }; 20 queue<node>q; 21 set<int>st; 22 int temp[4],tar[4]; 23 bool transformIt(int *d){ 24 int num = 0; 25 for(int i = 0; i < 4; i++) 26 num = num*10 + d[i]; 27 if(st.count(num)) return false; 28 st.insert(num); 29 return true; 30 } 31 bool ok(int *d){ 32 for(int i = 0; i < 4; i++) 33 if(d[i] != tar[i]) return false; 34 return true; 35 } 36 int bfs(){ 37 while(!q.empty()) q.pop(); 38 node a,b; 39 memcpy(a.key,temp,sizeof(temp)); 40 st.clear(); 41 a.step = 0; 42 q.push(a); 43 transformIt(a.key); 44 while(!q.empty()){ 45 b = q.front(); 46 q.pop(); 47 if(ok(b.key)) return b.step; 48 for(int i = 0; i < 4; i++){ 49 a.step = b.step+1; 50 memcpy(temp,b.key,sizeof(temp)); 51 if(temp[i] + 1 > 9) temp[i] = 1; 52 else temp[i]++; 53 if(transformIt(temp)){ 54 memcpy(a.key,temp,sizeof(temp)); 55 q.push(a); 56 } 57 memcpy(temp,b.key,sizeof(temp)); 58 if(temp[i]-1 == 0) temp[i] = 9; 59 else temp[i]--; 60 if(transformIt(temp)){ 61 memcpy(a.key,temp,sizeof(temp)); 62 q.push(a); 63 } 64 if(i){ 65 memcpy(temp,b.key,sizeof(temp)); 66 swap(temp[i],temp[i-1]); 67 if(transformIt(temp)){ 68 memcpy(a.key,temp,sizeof(temp)); 69 q.push(a); 70 } 71 } 72 if(i < 3){ 73 memcpy(temp,b.key,sizeof(temp)); 74 swap(temp[i],temp[i+1]); 75 if(transformIt(temp)){ 76 memcpy(a.key,temp,sizeof(temp)); 77 q.push(a); 78 } 79 } 80 } 81 } 82 return -1; 83 } 84 int main(){ 85 int t,i,j; 86 char md[6]; 87 scanf("%d",&t); 88 while(t--){ 89 scanf("%s",md); 90 for(i = 0; i < 4; i++) temp[i] = md[i] - '0'; 91 scanf("%s",md); 92 for(i = 0; i < 4; i++) tar[i] = md[i] - '0'; 93 printf("%d\n",bfs()); 94 } 95 return 0; 96 }
不用set居然快了这么多。。。。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 struct node{ 18 int key[4],step; 19 }; 20 queue<node>q; 21 int temp[4],tar[4]; 22 bool vis[10000]; 23 bool transformIt(int *d){ 24 int num = 0; 25 for(int i = 0; i < 4; i++) 26 num = num*10 + d[i]; 27 if(vis[num]) return false; 28 vis[num] = true; 29 return true; 30 } 31 bool ok(int *d){ 32 for(int i = 0; i < 4; i++) 33 if(d[i] != tar[i]) return false; 34 return true; 35 } 36 int bfs(){ 37 while(!q.empty()) q.pop(); 38 node a,b; 39 memcpy(a.key,temp,sizeof(temp)); 40 memset(vis,false,sizeof(vis)); 41 a.step = 0; 42 q.push(a); 43 transformIt(a.key); 44 while(!q.empty()){ 45 b = q.front(); 46 q.pop(); 47 if(ok(b.key)) return b.step; 48 for(int i = 0; i < 4; i++){ 49 a.step = b.step+1; 50 memcpy(temp,b.key,sizeof(temp)); 51 if(temp[i] + 1 > 9) temp[i] = 1; 52 else temp[i]++; 53 if(transformIt(temp)){ 54 memcpy(a.key,temp,sizeof(temp)); 55 q.push(a); 56 } 57 memcpy(temp,b.key,sizeof(temp)); 58 if(temp[i]-1 == 0) temp[i] = 9; 59 else temp[i]--; 60 if(transformIt(temp)){ 61 memcpy(a.key,temp,sizeof(temp)); 62 q.push(a); 63 } 64 if(i){ 65 memcpy(temp,b.key,sizeof(temp)); 66 swap(temp[i],temp[i-1]); 67 if(transformIt(temp)){ 68 memcpy(a.key,temp,sizeof(temp)); 69 q.push(a); 70 } 71 } 72 if(i < 3){ 73 memcpy(temp,b.key,sizeof(temp)); 74 swap(temp[i],temp[i+1]); 75 if(transformIt(temp)){ 76 memcpy(a.key,temp,sizeof(temp)); 77 q.push(a); 78 } 79 } 80 } 81 } 82 return -1; 83 } 84 int main(){ 85 int t,i,j; 86 char md[6]; 87 scanf("%d",&t); 88 while(t--){ 89 scanf("%s",md); 90 for(i = 0; i < 4; i++) temp[i] = md[i] - '0'; 91 scanf("%s",md); 92 for(i = 0; i < 4; i++) tar[i] = md[i] - '0'; 93 printf("%d\n",bfs()); 94 } 95 return 0; 96 }