Open the Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2919 Accepted Submission(s): 1276
Problem DescriptionNow 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.
InputThe 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.
OutputFor each test case, print the minimal steps in one line.
Sample Input
21234214411119999
Sample Output
24
AuthorYE, Kai
SourceZhejiang University Local Contest 2005
RecommendIgnatius.L
解題思路:該題為廣度優先搜尋題,咋的一看可能想不到。但確實是求最短路勁的,不過和一般的迷宮搜尋的方向不一樣。這裡的搜尋樹是根據題意走的,不是上下左右,而是加,減,左換,右換。始終記住廣度優先搜尋的特點,最短路徑,遍曆標記。一定要對搜尋過得狀態進行標記,不然等待的結果將是:逾時。望大家以我為前車之鑒。。。。。。
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;int a[4];int b[4];int f[10][10][10][10];struct node{ int x[4]; int step;};int bfs(){ int i,j; node s,e; queue<node> q; memset(f,0,sizeof(f)); s.x[0]=a[0]; s.x[1]=a[1]; s.x[2]=a[2]; s.x[3]=a[3]; s.step=0; q.push(s); //int n=20; f[a[0]][a[1]][a[2]][a[3]]=1; while(!q.empty()) { s=q.front(); q.pop(); if(s.x[0]==b[0]&&s.x[1]==b[1]&&s.x[2]==b[2]&&s.x[3]==b[3]) return s.step; //printf("%d%d%d%d\n",s.x[0],s.x[1],s.x[2],s.x[3]); for(i=0;i<4;i++) { //左邊交換 if(i!=0) { e.x[0]=s.x[0]; e.x[1]=s.x[1]; e.x[2]=s.x[2]; e.x[3]=s.x[3]; j=e.x[i-1]; e.x[i-1]=e.x[i]; e.x[i]=j; e.step=s.step+1; if(!f[e.x[0]][e.x[1]][e.x[2]][e.x[3]]) { q.push(e); f[e.x[0]][e.x[1]][e.x[2]][e.x[3]]=1; } } if(e.x[0]==b[0]&&e.x[1]==b[1]&&e.x[2]==b[2]&&e.x[3]==b[3]) return e.step; //右邊交換 if(i!=3) { e.x[0]=s.x[0]; e.x[1]=s.x[1]; e.x[2]=s.x[2]; e.x[3]=s.x[3]; j=e.x[i]; e.x[i]=e.x[i+1]; e.x[i+1]=j; e.step=s.step+1; if(!f[e.x[0]][e.x[1]][e.x[2]][e.x[3]]) { q.push(e); f[e.x[0]][e.x[1]][e.x[2]][e.x[3]]=1; } } if(e.x[0]==b[0]&&e.x[1]==b[1]&&e.x[2]==b[2]&&e.x[3]==b[3]) return e.step; //x[i]+1; //printf""(); e.x[0]=s.x[0]; e.x[1]=s.x[1]; e.x[2]=s.x[2]; e.x[3]=s.x[3]; e.x[i]+=1; if(e.x[i]==10) e.x[i]=1; e.step=s.step+1; if(!f[e.x[0]][e.x[1]][e.x[2]][e.x[3]]) { q.push(e); f[e.x[0]][e.x[1]][e.x[2]][e.x[3]]=1; } if(e.x[0]==b[0]&&e.x[1]==b[1]&&e.x[2]==b[2]&&e.x[3]==b[3]) return e.step; //x[i]-1 e.x[0]=s.x[0]; e.x[1]=s.x[1]; e.x[2]=s.x[2]; e.x[3]=s.x[3]; e.x[i]-=1; if(e.x[i]==0) e.x[i]=9; e.step=s.step+1; if(!f[e.x[0]][e.x[1]][e.x[2]][e.x[3]]) { q.push(e); f[e.x[0]][e.x[1]][e.x[2]][e.x[3]]=1; } if(e.x[0]==b[0]&&e.x[1]==b[1]&&e.x[2]==b[2]&&e.x[3]==b[3]) return e.step; } }}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%1d%1d%1d%1d",&a[0],&a[1],&a[2],&a[3]); scanf("%1d%1d%1d%1d",&b[0],&b[1],&b[2],&b[3]); //printf("%d %d %d %d\n",a[0],a[1],a[2],a[3]); // printf("%d %d %d %d\n",b[0],b[1],b[2],b[3]); printf("%d\n",bfs()); } return 0;}