现在的位置: 首页 > 综合 > 正文

HDU 1195 Open the Lock BFS

2018年01月20日 ⁄ 综合 ⁄ 共 2339字 ⁄ 字号 评论关闭

Open the Lock

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

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
/*
hdoj 1195 
BFS 对数字有三种操作
+1 -1 相邻的交换 
*/

#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;

struct node
{
    int num[4],step;
};

char a[5],b[5];
int visit[10][10][10][10];//对应4个位置10个数字 
node f,e;

void BFS()
{
    int i,k;
    queue<node> que;
    node start,end;
    
    memset(visit,0,sizeof(visit));
    
    start=f;        
    start.step=0;
    visit[start.num[0]][start.num[1]][start.num[2]][start.num[3]]=1;
    
    que.push(start);
    while(!que.empty())
    {
        start=que.front();
        que.pop();
        
        k=0;
        for(i=0;i<4;i++)
            if(start.num[i]!=e.num[i])
                {
                    k=1;break;
                }
        
        if(k==0)
            {
                printf("%d\n",start.step);
                return;
            }
        
        for(i=0;i<4;i++) //+1
        {
            end=start; 
            if(start.num[i]==9) end.num[i]=1;//每次变换一次就入队 所以要end=start 其他的不变 
                else end.num[i]=start.num[i]+1;
            end.step=start.step+1;
            if(!visit[end.num[0]][end.num[1]][end.num[2]][end.num[3]])
            {
                visit[end.num[0]][end.num[1]][end.num[2]][end.num[3]]=1;
                que.push(end);
            }
        }
        
        for(i=0;i<4;i++) //-1
        {
            end=start; 
            if(start.num[i]==1) end.num[i]=9;
                else end.num[i]=start.num[i]-1;
            end.step=start.step+1;
            if(!visit[end.num[0]][end.num[1]][end.num[2]][end.num[3]])
            {
                visit[end.num[0]][end.num[1]][end.num[2]][end.num[3]]=1;
                que.push(end);
            }
        }
        
        for(i=0;i<3;i++) //相邻位置的交换 
        {
            end=start; 
            end.num[i]=start.num[i+1];
            end.num[i+1]=start.num[i];
            end.step=start.step+1;
            if(!visit[end.num[0]][end.num[1]][end.num[2]][end.num[3]])
            {
                visit[end.num[0]][end.num[1]][end.num[2]][end.num[3]]=1;
                que.push(end);
            }
        }        
    }    
}
int main(){
    
    int n,i;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s%s",a,b);
        for(i=0;i<4;i++)
        {
            f.num[i]=a[i]-'0';
            e.num[i]=b[i]-'0';
        }
        BFS();
    } 
    return 0;
}

抱歉!评论已关闭.