Open the Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3679 Accepted Submission(s):
1611
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.
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.
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
Author
YE, Kai
Source
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 #include<queue> 6 using namespace std; 7 8 int final[4],start[4]; 9 struct node 10 { 11 int a[4]; 12 int time; 13 }; 14 bool dp[10][10][10][10]; 15 queue<node>Q; 16 17 int bfs() 18 { 19 node cur,now; 20 int i,k,a,b,c,d; 21 for(i=0;i<4;i++) cur.a[i]=start[i]; 22 cur.time=0; 23 Q.push(cur); 24 dp[cur.a[0]][cur.a[1]][cur.a[2]][cur.a[3]]=true; 25 while(!Q.empty()) 26 { 27 cur=Q.front(); 28 Q.pop(); 29 for(i=0,k=0;i<4;i++) 30 { 31 if(final[i]==cur.a[i]) 32 { 33 k++; 34 } 35 if(k==4) return cur.time; 36 } 37 for(i=0;i<4;i++)//add 1 38 { 39 now=cur; 40 if(now.a[i]==9) 41 now.a[i]=1; 42 else now.a[i]=now.a[i]+1; 43 a=now.a[0]; 44 b=now.a[1]; 45 c=now.a[2]; 46 d=now.a[3]; 47 if(dp[a][b][c][d]==false) 48 { 49 dp[a][b][c][d]=true; 50 now.time++; 51 Q.push(now); 52 } 53 } 54 for(i=0;i<4;i++)//del 1 55 { 56 now=cur; 57 if(now.a[i]==1) 58 now.a[i]=9; 59 else now.a[i]=now.a[i]-1; 60 a=now.a[0]; 61 b=now.a[1]; 62 c=now.a[2]; 63 d=now.a[3]; 64 if(dp[a][b][c][d]==false) 65 { 66 dp[a][b][c][d]=true; 67 now.time++; 68 Q.push(now); 69 } 70 } 71 for(i=0;i<3;i++)//change 72 { 73 now=cur; 74 k=now.a[i]; 75 now.a[i]=now.a[i+1]; 76 now.a[i+1]=k; 77 78 a=now.a[0]; 79 b=now.a[1]; 80 c=now.a[2]; 81 d=now.a[3]; 82 if(dp[a][b][c][d]==false) 83 { 84 dp[a][b][c][d]=true; 85 now.time++; 86 Q.push(now); 87 } 88 } 89 } 90 return -1; 91 } 92 int main() 93 { 94 int T,cur,i; 95 char a[10],b[10]; 96 scanf("%d",&T); 97 getchar(); 98 while(T--) 99 { 100 scanf("%s",a); 101 scanf("%s",b); 102 for(i=0;i<4;i++) 103 start[i]=a[i]-'0'; 104 for(i=0;i<4;i++) 105 final[i]=b[i]-'0'; 106 memset(dp,false,sizeof(dp)); 107 while(!Q.empty()) 108 { 109 Q.pop(); 110 } 111 cur=bfs(); 112 printf("%d ",cur); 113 } 114 return 0; 115 }
双向广搜
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 #include<queue> 6 using namespace std; 7 8 char a[6],b[6]; 9 bool flag; 10 struct node 11 { 12 char cur[4]; 13 }; 14 queue<node>Q[2]; 15 bool dp[2][10][10][10][10]; 16 17 void bfs(int x) 18 { 19 int size,i; 20 node t,k; 21 size=Q[x].size(); 22 23 while(size--) 24 { 25 k=Q[x].front(); 26 Q[x].pop(); 27 28 if(dp[x^1][k.cur[0]-'0'][k.cur[1]-'0'][k.cur[2]-'0'][k.cur[3]-'0']==true) 29 { 30 flag=true; 31 return; 32 } 33 for(i=0;i<=2;i++) 34 { 35 t=k; 36 swap(t.cur[i],t.cur[i+1]); 37 if(dp[x][t.cur[0]-'0'][t.cur[1]-'0'][t.cur[2]-'0'][t.cur[3]-'0']==false) 38 { 39 dp[x][t.cur[0]-'0'][t.cur[1]-'0'][t.cur[2]-'0'][t.cur[3]-'0']=true; 40 Q[x].push(t); 41 } 42 } 43 for(i=0;i<=3;i++) 44 { 45 t=k; 46 if(t.cur[i]=='9') 47 t.cur[i]='1'; 48 else t.cur[i]++; 49 if(dp[x][t.cur[0]-'0'][t.cur[1]-'0'][t.cur[2]-'0'][t.cur[3]-'0']==false) 50 { 51 dp[x][t.cur[0]-'0'][t.cur[1]-'0'][t.cur[2]-'0'][t.cur[3]-'0']=true; 52 Q[x].push(t); 53 } 54 t=k; 55 if(t.cur[i]=='1') 56 t.cur[i]='9'; 57 else t.cur[i]--; 58 if(dp[x][t.cur[0]-'0'][t.cur[1]-'0'][t.cur[2]-'0'][t.cur[3]-'0']==false) 59 { 60 dp[x][t.cur[0]-'0'][t.cur[1]-'0'][t.cur[2]-'0'][t.cur[3]-'0']=true; 61 Q[x].push(t); 62 } 63 } 64 } 65 } 66 void dbfs() 67 { 68 int cnt=0; 69 node t; 70 t.cur[0]=a[0]; 71 t.cur[1]=a[1]; 72 t.cur[2]=a[2]; 73 t.cur[3]=a[3]; 74 dp[0][a[0]-'0'][a[1]-'0'][a[2]-'0'][a[3]-'0']=true; 75 Q[0].push(t); 76 77 t.cur[0]=b[0]; 78 t.cur[1]=b[1]; 79 t.cur[2]=b[2]; 80 t.cur[3]=b[3]; 81 dp[1][b[0]-'0'][b[1]-'0'][b[2]-'0'][b[3]-'0']=true; 82 Q[1].push(t); 83 while(true) 84 { 85 cnt++; 86 if(Q[0].size()<Q[1].size()) 87 bfs(0); 88 else bfs(1); 89 if(flag==true) break; 90 } 91 printf("%d ",cnt-1); 92 } 93 94 int main() 95 { 96 int T; 97 scanf("%d",&T); 98 while(T--) 99 { 100 scanf("%s%s",a,b); 101 if(strcmp(a,b)==0) 102 { 103 printf("0 "); 104 continue; 105 } 106 while(!Q[0].empty()) 107 { 108 Q[0].pop(); 109 } 110 while(!Q[1].empty()) 111 { 112 Q[1].pop(); 113 } 114 flag=false; 115 memset(dp,false,sizeof(dp)); 116 dbfs(); 117 } 118 return 0; 119 }