HDU 1195

31 篇文章 0 订阅
24 篇文章 0 订阅

       一道广搜的题。

      看到别人说这道题用双向广搜来做,本想来学学双向广搜,可以我用单纯的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 10000
using 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
  
  
2 1234 2144 1111 9999
 

Sample Output
  
  
2 4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值