HDU 1195

来源:互联网 发布:史上最奇葩的淘宝差评 编辑:程序博客网 时间:2024/06/05 04:48

       一道广搜的题。

      看到别人说这道题用双向广搜来做,本想来学学双向广搜,可以我用单纯的BFS也过了,下面的代码就是纯BFS的解法。

     通过这题,我觉得我的英语需要好好加强,今天把题目里“ you can add or minus 1 to any digit”的意思理解错了。我错误的以为意思是可以加上或减去大于等于1的任何数,哪知题目只是让你在四个数字中的任意一个加上或减去1。

代码(C++):

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#define MAX 10005#define MAX_N 10000using namespace std;const int dir[2]={-1,1};struct Node{    int id;    int step;};bool vis[MAX];int num1[MAX_N][5],num2[5];int get_num(int num[]){    int i,res=0;    for(i=0;i<4;i++)    {        res=res*10+num[i];    }    return res;}int bfs(int num){    queue<Node> qi;    Node node,tmp;    int c,id,t,i,j;    c=1;    tmp.id=0;    tmp.step=0;    qi.push(tmp);    while(!qi.empty())    {        node=qi.front();        qi.pop();        id=node.id;        for(i=0;i<4;i++)        {            for(j=0;j<2;j++)            {                memcpy(num1[c],num1[id],sizeof(num1[id]));                num1[c][i]=(num1[c][i]+dir[j]-1+9)%9+1;                t=get_num(num1[c]);                if(t==num) return node.step+1;                if(!vis[t])                {                    tmp.id=c++;                    tmp.step=node.step+1;                    vis[t]=true;                    qi.push(tmp);                }            }        }        for(i=0;i<3;i++)        {            if(num1[id][i]!=num1[id][i+1])            {                memcpy(num1[c],num1[id],sizeof(num1[id]));                swap(num1[c][i],num1[c][i+1]);                t=get_num(num1[c]);                if(t==num) return node.step+1;                if(!vis[t])                {                    tmp.id=c++;                    tmp.step=node.step+1;                    vis[t]=true;                    qi.push(tmp);                }            }        }    }    return 0;}int main(){    //freopen("in.txt","r",stdin);    int t,i,res,num;    char s1[5],s2[5];    scanf("%d",&t);    while(t--)    {        getchar();        cin.getline(s1,5);        cin.getline(s2,5);        for(i=0;i<4;i++)        {           num1[0][i]=s1[i]-'0';           num2[i]=s2[i]-'0';        }        memset(vis,false,sizeof(vis));        vis[get_num(num1[0])]=true;        num=get_num(num2);        if(vis[num]) res=0;        else res=bfs(num);        printf("%d\n",res);    }    return 0;}

题目(http://acm.hdu.edu.cn/showproblem.php?pid=1195):

Open the Lock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)


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

0 0
原创粉丝点击