ACM--steps--dyx--4.2.5--Open the lock(多向广搜)

来源:互联网 发布:淘宝手机端怎么做推广 编辑:程序博客网 时间:2024/04/30 19:20

Open the Lock

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 136 Accepted Submission(s): 80
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

#include<iostream>#include<cstring>#include<queue>using namespace std;struct node{    int num[4],step;}fir,fin;//表示开始的锁位置,和最末的锁位置.int vis[11][11][11][11];int s_num,e_num;bool wyx(node now){    //vis数组初始化为0,1表示已经被访问过了.    if(vis[now.num[0]][now.num[1]][now.num[2]][now.num[3]])    return true;    return false;}int bfs(){    queue<node> que;    node now,next;    now=fir;    now.step=0;    vis[now.num[0]][now.num[1]][now.num[2]][now.num[3]]=1;//表示状态已经被访问过.    que.push(now);    while(!que.empty())    {        now=que.front();        que.pop();        if(now.num[0]==fin.num[0]&&now.num[1]==fin.num[1]&&now.num[2]==fin.num[2]&&now.num[3]==fin.num[3])        return now.step;        //分为3个模块分别进行广搜        //第一个方向。数字+;        for(int i=0;i<4;i++)        {            next=now;            next.num[i]++;            //注意,在进行加法的广搜时,加到10的时候,意即到1;            if(next.num[i]==10)            next.num[i]=1;            if(wyx(next))            continue;            vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;            next.step++;            que.push(next);        }        //第二个方向,数字-;        for(int i=0;i<4;i++)        {            next=now;            next.num[i]--;            if(next.num[i]==0)            next.num[i]=9;            if(wyx(next))            continue;            vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;            next.step++;            que.push(next);        }        //第三个方向,交换        for(int i=0;i<3;i++)        {            next=now;            //锁的相邻两个位置可以实现交换.            next.num[i]=now.num[i+1];            next.num[i+1]=now.num[i];            if(wyx(next))            continue;            vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;            next.step++;            que.push(next);        }    }    return 0;}int main(){    char s1[10],s2[10];    int T;    cin>>T;    while(T--)    {        cin>>s1>>s2;        for(int i=0;i<4;i++)        {            fir.num[i]=s1[i]-'0';            fin.num[i]=s2[i]-'0';        }        memset(vis,0,sizeof(vis));        cout<<bfs()<<endl;    }    return 0;}


 
0 0
原创粉丝点击