HDU1195 BFS或者DP

原创 2016年06月01日 00:10:08

Open the Lock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5936    Accepted Submission(s): 2651


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减1,和左边交换或者和右边交换。可以用搜索的来查询每种操作。
要求代价最小可以近似的理解为寻找最短路,那么很自然的选择用BFS来做。
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
int s1,s2,s3,s4;
int e1,e2,e3,e4;
int steps;
bool vis[10000];
struct Q
{
    int x[4];
    int steps;
};
void bfs()
{
    queue<Q>q;
    Q temp,now;
    int i,j;
    int x[4];
    int bit[4]={1000,100,10,1};

    while(!q.empty())q.pop();
    temp.x[0]=s1; temp.x[1]=s2;
    temp.x[2]=s3; temp.x[3]=s4;
    temp.steps=0;
    q.push(temp);
    steps=0xfffffff;
    memset(vis,0,sizeof(vis));
    vis[s1*1000+s2*100+s3*10+s4]=1;
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.x[0]==e1&&now.x[1]==e2&&now.x[2]==e3&&now.x[3]==e4)
        {

            if(steps>now.steps)
            {
                steps=now.steps;
            }
        }
        for(i=0;i<4;i++)
        {
            for(j=0;j<4;j++)
            {
                x[0]=now.x[0];x[1]=now.x[1];
                x[2]=now.x[2];x[3]=now.x[3];
                temp=now;
                if(j==0)
                {
                    x[i]++;
                    if(x[i]>9)x[i]=1;
                }
                else
                if(j==1)
                {
                    x[i]--;
                    if(x[i]<1)x[i]=9;
                }
                else
                if(j==2)
                {
                    if(i<=0)continue;
                    int t=x[i];
                    x[i]=x[i-1];
                    x[i-1]=t;
                }
                else
                {
                    if(i>=3)continue;
                    int t=x[i];
                    x[i]=x[i+1];
                    x[i+1]=t;
                }
                //printf("%d %d %d %d\n",x[0],x[1],x[2],x[3]);
                if(vis[x[0]*bit[0]+x[1]*bit[1]+x[2]*bit[2]+x[3]*bit[3]])continue;
                vis[x[0]*bit[0]+x[1]*bit[1]+x[2]*bit[2]+x[3]*bit[3]]=1;
                temp.x[0]=x[0];temp.x[1]=x[1];temp.x[2]=x[2];temp.x[3]=x[3];

                temp.steps++;
                q.push(temp);
            }
        }
    }
    return;
}

int main()
{
    int t;
    char str[5];

    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str);
        s1=str[0]-'0'; s2=str[1]-'0';
        s3=str[2]-'0'; s4=str[3]-'0';
        scanf("%s",str);
        e1=str[0]-'0'; e2=str[1]-'0';
        e3=str[2]-'0'; e4=str[3]-'0';
        bfs();
        printf("%d\n",steps);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu1195Open the Lock(双向bfs+循环队列)

->题目请戳这里 题目大意:给2个长度为4的数字串,求从第一个串变到第二个串最少要几步。 每一步变换情况有: 1:每个数字+1,9+1 = 1; 2:每个数字-1,1-1 = 9; 3:某个...

hdu1195 Open the Lock(bfs水题)

题目:Open the LockTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

hdu1195 Open the Lock (BFS)

题意:给出两个4位的数字,都由1~9组成,要求经过一系列的操作将第一个数字变为第二个数字。操作有两种, 第一种:选择一位加上或减去1,9+1=1,1-1=9。 第二种:交换相邻的两位。第一位和第四...

HDU1195 ZOJ2416 Open the Lock【BFS】

Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

HDU1195(BFS)

Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To...

HDU-1195-Open the Lock(BFS)

题目链接题意:两个四位数字进行转化,每一次可以对其中一位数+1或者-1(1-1=9 9+1=1),或者交换相邻的两位。思路,因为最多不超过10000种状态,所以直接bfs一下,有11种状态#inclu...

【HDU 1195】Open the Lock(BFS)

DescriptionNow an emergent task for you is to open a password lock. The password is consisted of fou...

hdu 1195 Open the Lock(BFS)

问题描述: Problem Description Now an emergent task for you is to open a password lock. The password is...

HDU 1195 Open the Lock BFS

#include #include using namespace std; int visit[11][11][11][11]; struct node { int num[4]; int...
  • vsooda
  • vsooda
  • 2012年09月07日 15:40
  • 349

hdu1195 Open the Lock BFS 广搜

题目链接:here 题意: 给你四个数字,每次可以将其中任何一个数字 +1 或者 -1 ; 1的时候-1 等于9  ;9的时候+1 等于1;  问最少需要变换几次,才可以变到目标序列。。 分析:...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDU1195 BFS或者DP
举报原因:
原因补充:

(最多只允许输入30个字)