hdu1195

来源:互联网 发布:食品制造业数据 编辑:程序博客网 时间:2024/06/08 19:39

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
2
1234
2144

1111
9999

Sample Output
2
4

题意:有一个紧急开启密码锁的任务。密码由四位数字组成;每个数字从1到9;每次,可以对每一个数字进行加1或者减1;当从1加到9时,由9再加1会变为1;当从9减到1时,由1再减1会变为9;也可以交换两个相邻的数字,最后一个跟第一个不能交换,每次操作作为一个step。你的任务就是用最少的步骤解锁!
思路:
1 用mark[10][10][10][10]记录每个数的状态;
2 bfs执行3个步骤,并查看是否标记,没标记放入数组;
代码:

#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;struct node{   char c[10];   int t;};char c1[10],c2[10];int mark[10][10][10][10];int bfs(){    mark[c1[0]-'0'][c1[1]-'0'][c1[2]-'0'][c1[3]-'0']=1;    node q,p;    queue<node>que;    strcpy(q.c,c1);    q.t=0;    que.push(q);    while(que.size())    {        q=que.front();        que.pop();        if(strcmp(c2,q.c)==0)        {            return q.t;        }        for(int i=0;i<=3;i++)        {            strcpy(p.c,q.c);            if(q.c[i]=='9')               p.c[i]='1';            else               p.c[i]=q.c[i]+1;            if(!mark[p.c[0]-'0'][p.c[1]-'0'][p.c[2]-'0'][p.c[3]-'0'])            {                mark[p.c[0]-'0'][p.c[1]-'0'][p.c[2]-'0'][p.c[3]-'0']=1;                p.t=q.t+1;                que.push(p);            }            strcpy(p.c,q.c);            if(p.c[i]=='1')               p.c[i]='9';            else               p.c[i]=q.c[i]-1;            if(!mark[p.c[0]-'0'][p.c[1]-'0'][p.c[2]-'0'][p.c[3]-'0'])            {                mark[p.c[0]-'0'][p.c[1]-'0'][p.c[2]-'0'][p.c[3]-'0']=1;                p.t=q.t+1;                que.push(p);            }            strcpy(p.c,q.c);            if(i<3)            {                p.c[i]=q.c[i+1];                p.c[i+1]=q.c[i];                if(!mark[p.c[0]-'0'][p.c[1]-'0'][p.c[2]-'0'][p.c[3]-'0'])                {                    mark[p.c[0]-'0'][p.c[1]-'0'][p.c[2]-'0'][p.c[3]-'0']=1;                    p.t=q.t+1;                    que.push(p);                }            }        }    }    return 0;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%s",c1);        scanf("%s",c2);        if(strcmp(c1,c2)==0)        {            printf("0\n");            continue;        }        memset(mark,0,sizeof(mark));        printf("%d\n",bfs());    }}
0 0