HDU1195:Open the Lock(BFS)

来源:互联网 发布:java中ssh是什么 编辑:程序博客网 时间:2024/04/27 20:55
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
 


 

普通的BFS,将已经访问过的状态标记下即可

 

#include <stdio.h>#include <algorithm>#include <string.h>#include <queue>using namespace std;struct node{    int num[4],step;} first,last;int vis[11][11][11][11];void bfs(){    int i;    node a,next;    queue<node> q;    a = first;    a.step = 0;    q.push(a);    vis[a.num[0]][a.num[1]][a.num[2]][a.num[3]] = 1;    while(!q.empty())    {        a = q.front();        q.pop();        if(a.num[0] == last.num[0] && a.num[1] == last.num[1] && a.num[2] == last.num[2] && a.num[3] == last.num[3])        {            printf("%d\n",a.step);            return ;        }        for(i = 0; i<4; i++) //+1        {            next = a;            next.num[i]++;            if(next.num[i]==10)                next.num[i] = 1;            if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]])            {                vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]] = 1;                next.step++;                q.push(next);            }        }        for(i = 0; i<4; i++) //-1        {            next = a;            next.num[i]--;            if(next.num[i]==0)                next.num[i] = 9;            if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]])            {                vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]] = 1;                next.step++;                q.push(next);            }        }        for(i = 0; i<3; i++) //交换        {            next = a;            next.num[i] = a.num[i+1];            next.num[i+1] = a.num[i];            if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]])            {                vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]] = 1;                next.step++;                q.push(next);            }        }    }}int main(){    int i,j,t;    char s1[10],s2[10];    scanf("%d",&t);    while(t--)    {        scanf("%s%s",s1,s2);        for(i = 0; i<4; i++)        {            first.num[i] = s1[i]-'0';            last.num[i] = s2[i]-'0';        }        memset(vis,0,sizeof(vis));        bfs();    }    return 0;}


 

0 0
原创粉丝点击