hdu 1195 Open the Lock (BFS)
2013-12-14 12:29
453 查看
Open the Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3388 Accepted Submission(s): 1499
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, print the minimal steps in one line.
[align=left]Sample Input[/align]
2
1234
2144
1111
9999
[align=left]Sample Output[/align]
2
4
[align=left]Author[/align]
YE, Kai
[align=left]Source[/align]
Zhejiang University Local Contest 2005
[align=left]Recommend[/align]
Ignatius.L | We have carefully selected several similar problems for you: 1175 1072 1026 1180 1044
//15MS 340K 1607 B C++ /* 题意: 给出两个四位数,有两种操作,相邻交换或每位+/-1; 求最少步数使第一串变为第二串 bfs: 细心点就可以过,先考虑每一次最多可以有多少步可以走,因为只有四位数, 而且有两种操作,一共有七步,相邻交换有三步,每位操作有四步,知道这个然后就常规的 bfs求解。 */ #include<iostream> #include<queue> using namespace std; struct node{ int a,cnt; }; int a,b; int vis[10005]; int bfs() { memset(vis,0,sizeof(vis)); queue<node>Q; node t={a,0}; vis[a]=1; Q.push(t); while(!Q.empty()){ t=Q.front(); Q.pop(); if(t.a==b) return t.cnt; node tt=t; tt.cnt++; tt.a=t.a%100+(t.a/1000)*100+(t.a%1000)/100*1000; if(!vis[tt.a]){ Q.push(tt);vis[tt.a]=1; //printf("1*%d\n",tt.a); } tt.a=t.a/1000*1000+(t.a%1000)/100*10+(t.a%100)/10*100+t.a%10; if(!vis[tt.a]){ Q.push(tt);vis[tt.a]=1; //printf("2*%d\n",tt.a); } tt.a=t.a/100*100+(t.a%10)*10+(t.a%100)/10; if(!vis[tt.a]){ Q.push(tt);vis[tt.a]=1; //printf("3*%d\n",tt.a); } for(int i=1;i<1001;i*=10){ int temp=(t.a%(i*10))/i; if(temp==9) tt.a=t.a-8*i; else tt.a=t.a+i; if(!vis[tt.a]){ Q.push(tt);vis[tt.a]=1; //printf("4*%d\n",tt.a); } if(temp==1) tt.a=t.a+8*i; else tt.a=t.a-i; if(!vis[tt.a]){ Q.push(tt);vis[tt.a]=1; //printf("5*%d\n",tt.a); } } if(t.a<1000) return 0; } return 0; } int main(void) { int t; scanf("%d",&t); while(t--) { scanf("%d%d",&a,&b); printf("%d\n",bfs()); } return 0; }
相关文章推荐
- 交换地方HDU 1195 Open the Lock 简单搜索-bfs
- 双向BFS-->hdu 1195 Open the Lock
- hdu 1195 Open the Lock(bfs)
- HDU-1195-Open the Lock(BFS)
- HDU 1195 Open the Lock (BFS 剪枝)
- hdu-1195--Open the Lock(BFS)
- 【非图BFS+STL】电脑修好了—Open the Lock_HDU 1195
- HDU 1195 Open the Lock BFS
- HDU 1195 Open the Lock (不一样的BFS)
- hdu 1195 Open the Lock(双向bfs)
- Hdu 1195 Open the Lock [Bfs]
- HDU 1195 Open the Lock(双向BFS)
- hdu 1195 Open the Lock(bfs)
- HDU 1195 Problem:OPEN THE LOCK (bfs)
- hdu1195 Open the Lock 【俩方法:1.bfs 2.dfs】
- hdu 1195 Open the Lock(基础bfs)
- hdu 1195 Open the Lock (bfs)
- hdu 1195 Open the Lock (BFS)
- hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)
- hdu 1195 Open the Lock bfs