HDU 1195 Open the Lock (不一样的BFS)

来源:互联网 发布:手机淘宝店铺管理软件 编辑:程序博客网 时间:2024/06/05 02:30

Open the Lock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6045    Accepted Submission(s): 2697


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
21234214411119999
 

Sample Output
24
 

Author
YE, Kai
 

Source
Zhejiang University Local Contest 2005
 

Recommend
Ignatius.L


原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1195


知道是BFS 的题目,但想不到怎么去搜,据说比素数环还要简单,经过别人的点拨和网上的代码,瞬间发现,想通了真的比素数环还要简单

AC代码:

#include <iostream>#include <queue>#include <cstring>using namespace std;struct node{    int a[4],step;};char str[2][5];int f[4];bool vis[10][10][10][10];int BFS(){    node now,next;    for(int i=0;i<4;i++)    {        now.a[i]=str[0][i]-'0';        f[i]=str[1][i]-'0';    }    memset(vis,false,sizeof(vis));    now.step=0;    queue<node>q;    q.push(now);    bool lock;    while(!q.empty())    {        now=q.front();        q.pop();        lock=true;        for(int i=0;i<4;i++)        {            if(now.a[i]!=f[i])            {                lock=false;                break;            }        }        if(lock)            return now.step;        for(int i=0;i<4;i++)//加一        {            next=now;            if(now.a[i]==9)                next.a[i]=1;            else                next.a[i]=now.a[i]+1;            if(!vis[next.a[0]][next.a[1]][next.a[2]][next.a[3]])            {                vis[next.a[0]][next.a[1]][next.a[2]][next.a[3]]=true;                next.step=now.step+1;                q.push(next);            }        }        for(int i=0;i<4;i++)//减一        {            next=now;            if(now.a[i]==1)                next.a[i]=9;            else                next.a[i]=now.a[i]-1;            if(!vis[next.a[0]][next.a[1]][next.a[2]][next.a[3]])            {                vis[next.a[0]][next.a[1]][next.a[2]][next.a[3]]=true;                next.step=now.step+1;                q.push(next);            }        }        for(int i=0;i<3;i++)//交换        {            next=now;            next.a[i]=now.a[i+1];            next.a[i+1]=now.a[i];            if(!vis[next.a[0]][next.a[1]][next.a[2]][next.a[3]])            {                vis[next.a[0]][next.a[1]][next.a[2]][next.a[3]]=true;                next.step=now.step+1;                q.push(next);            }        }    }    return -1;}int main(){    int t;    cin>>t;    while(t--)    {        cin>>str[0]>>str[1];        cout<<BFS()<<endl;    }    return 0;}




0 0