HDU--1195--bfs--Open the Lock

时间:2024-04-06 20:06:01
/*
    Name: HDU--1195--Open the Lock
    Author: shen_渊
    Date: 17/04/17 08:54
    Description: bfs,用四维数组标记状态,每位数字可以做三种操作 +1,-1,交换
                可以不用优先队列,题意决定,优先队列更费时间
*/

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
struct node{
    int steps;
    string s;
    node():steps(){
    };
    bool operator <(const node &a)const{
        return steps>a.steps;
    }
};
int bfs();
][][][];
string str1,str2;
int main()
{
    ios::sync_with_stdio(false);
//    freopen("in.txt","r",stdin);
    int n;cin>>n;
    while(n--){
        memset(vis,,sizeof(vis));
        cin>>str1>>str2;
        if(str1 == str2)cout<<"0\n";
        else cout<<bfs()<<endl;
    }
    ;
}
int bfs(){
    priority_queue<node> q;
    node p;
    p.s = str1;
    q.push(p);
    vis[p.s[]-]-]-]-;
    while(!q.empty()){
        node a,temp;
        temp = q.top();q.pop();
        if(temp.s == str2)return temp.steps;
        ; i<; ++i) {
            //+1
            a.s = temp.s;
            ';
            ;
            ]-]-]-]-']){
                a.steps = temp.steps + ;
                q.push(a);
                vis[a.s[]-]-]-]-;
            }
            //-1
            a.s = temp.s;
            ';
            ;
            ]-]-]-]-']){
                a.steps = temp.steps + ;
                q.push(a);
                vis[a.s[]-]-]-]-;
            }
            //交换
            ){
                a.s = temp.s;
                a.s[i] = temp.s[i+];
                a.s[i+] = temp.s[i];
                ]-]-]-]-']){
                a.steps = temp.steps + ;
                q.push(a);
                vis[a.s[]-]-]-]-;
                }
            }
        }
    }
    ;
}