题目链接如下
http://acm.hdu.edu.cn/showproblem.php?pid=1195
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.
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.
Output
For each test case, print the minimal steps in one line.
Sample Input
Sample Output
Author
YE, Kai
Source
Recommend
Ignatius.L
/*
考虑到此题密码变换规则相当复杂,但是数据并不是很大,密码只有4位,结果只要求输出最短的步数,可以考虑穷举法
假设结果是X种变换,我们第一步到第X步考虑每一种变换,每一种变换只有11种可能,变换后的密码又有11种可能,
如此重复,直到正确密码出现,然后直接输出变换的次数。
这里我们用 vis[10000] 这个数组表示每一种密码,
step[10000]数组保存变换到每一种密码所需要的最小步数
a[5],b[5]数组保存每个密码数字的值
q队列更新每次的密码值,且来控制循环
其实也就是bfs只是写法不同而已
*/
#include <iostream>
#include <queue>
using namespace std;
int n,m;
int step[10000];
bool vis[10000];
int main()
{
int N;
cin>>N;
while(N--)
{
cin>>n>>m;
if(n==m) cout<<"0"<<endl;
int i,flag,temp;
int a[5],b[5];
queue<int> q;
q.push(n);
memset(step,0,sizeof(step));
memset(vis,true,sizeof(vis));
while(!q.empty())
{
temp=q.front();
q.pop();
b[4]=a[4]=temp%10;
b[3]=a[3]=temp/10%10;
b[2]=a[2]=temp/100%10;
b[1]=a[1]=temp/1000;
for (i=0;i<11;i++)
{
switch (i)
{
case 0:
{
b[1]=a[1];b[2]=a[2];b[3]=a[3];b[4]=a[4];
if (a[1]==9) b[1]=1;
else b[1]+=1;
}
break;
case 1:
{
b[1]=a[1];b[2]=a[2];b[3]=a[3];b[4]=a[4];
if (a[2]==9) b[2]=1;
else b[2]+=1;
}
break;
case 2:
{
b[1]=a[1];b[2]=a[2];b[3]=a[3];b[4]=a[4];
if (a[3]==9) b[3]=1;
else b[3]+=1;
}
break;
case 3:
{
b[1]=a[1];b[2]=a[2];b[3]=a[3];b[4]=a[4];
if (a[4]==9) b[4]=1;
else b[4]+=1;
}
break;
case 4:
{
b[1]=a[1];b[2]=a[2];b[3]=a[3];b[4]=a[4];
if (a[1]==1) b[1]=9;
else b[1]-=1;
}
break;
case 5:
{
b[1]=a[1];b[2]=a[2];b[3]=a[3];b[4]=a[4];
if (a[2]==1) b[2]=9;
else b[2]-=1;
}
break;
case 6:
{
b[1]=a[1];b[2]=a[2];b[3]=a[3];b[4]=a[4];
if (a[3]==1) b[3]=9;
else b[3]-=1;
}
break;
case 7:
{
b[1]=a[1];b[2]=a[2];b[3]=a[3];b[4]=a[4];
if (a[4]==1) b[4]=9;
else b[4]-=1;
}
break;
case 8:
{
b[1]=a[1];b[2]=a[2];b[3]=a[3];b[4]=a[4];
flag=b[1];
b[1]=a[2];
b[2]=flag;
}
break;
case 9:
{
b[1]=a[1];b[2]=a[2];b[3]=a[3];b[4]=a[4];
flag=b[2];
b[2]=a[3]; b[3]=flag;
}
break;
case 10:
{
b[1]=a[1];b[2]=a[2];b[3]=a[3];b[4]=a[4];
flag=b[3];
b[3]=a[4]; b[4]=flag;
}
break;
}
n=b[1]*1000+b[2]*100+b[3]*10+b[4];
if (vis[n])
{
vis[n]=false;
step[n]=step[temp]+1;
q.push(n);
if (n==m) { cout<<step[n]<<endl; break;}
}
}
}
}
}
分享到:
相关推荐
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
杭电ACMhdu1163
HDU1059的代码
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu1001解题报告
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 1166线段树代码
HDU最全ac代码
自己做的HDU ACM已经AC的题目
hdu动态规划算法集锦
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. Input Input...
hdu-acm源代码(上百题)hdu-acm源代码、hdu-acm源代码hdu-acm源代码
hdu题目分类
Hdu 1237 解题代码