魔板(bfs)

在这里插入图片描述
在这里插入图片描述

思路:

吧数字转化为子符串然后处理他的3个状态,用个map记录一下有没有访问过这个状态,并在node里记录路径

#pragma GCC optimize(2)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<iomanip>
#include<cstring>
#include<time.h>
#include<bits/stdc++.h>
 
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
const int mod=1e9+7;
const int N=2e6+10;
const int M=1e5+10;
const int inf=0x3f3f3f3f;
const int maxx=2e5+7;
const double eps=1e-6;
int gcd(int a,int b)
{
    
    
    return b==0?a:gcd(b,a%b);
}
 
ll lcm(ll a,ll b)
{
    
    
    return a*(b/gcd(a,b));
}
 
template <class T>
void read(T &x)
{
    
    
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-')
            op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op)
        x = -x;
}
template <class T>
void write(T x)
{
    
    
    if(x < 0)
        x = -x, putchar('-');
    if(x >= 10)
         write(x / 10);
    putchar('0' + x % 10);
}
ll qsm(int a,int b,int p)
{
    
    
    ll res=1%p;
    while(b)
    {
    
    
        if(b&1)
            res=res*a%p;
        a=1ll*a*a%p;
        b>>=1;
    }
    return res;
}
int n, m,k;
int dir[4][2]={
    
    {
    
    -1,0},{
    
    0,1},{
    
    1,0},{
    
    0,-1}};
struct node
{
    
    
    string chuan;
    int step;
    string apl;
};

string ed;
string st="12345678";
//int vis[1005][1005];
int sx,sy,ex,ey;
unordered_map<string ,int> mp;
queue<node> q;
string get1(string x)
{
    
    
    string y=x;
    for(int i=0;i<4;i++)
      x[i]=y[8-i-1];
    for(int i=4;i<8;i++)
      x[i]=y[8-i-1];
      return x;
}
string get2(string x)
{
    
    
    string y=x;
    x[0]=y[3];for(int i=1;i<4;i++) x[i]=y[i-1];
    x[7]=y[4];for(int i=4;i<7;i++) x[i]=y[i+1];
    return x;

}
string get3(string x)
{
    
    
    string y=x;
    x[1]=y[6];x[2]=y[1];x[5]=y[2];x[6]=y[5];
    return x;
}
void bfs()
{
    
    
    mp[st]=1;
    q.push({
    
    st,0,""});
    while(!q.empty())
    {
    
    
        auto now=q.front();q.pop();
        if(now.chuan==ed){
    
    
            cout<<now.step<<endl;
            cout<<now.apl<<endl;break;
        }
        string s1=get1(now.chuan);
        if(!mp[s1]){
    
    
            q.push({
    
    s1,now.step+1,now.apl+"A"});
            mp[s1]=1;
        }
        string s2=get2(now.chuan);
        if(!mp[s2]){
    
    
            q.push({
    
    s2,now.step+1,now.apl+"B"});
            mp[s2]=1;
        }
        string s3=get3(now.chuan);
        if(!mp[s3]){
    
    
            q.push({
    
    s3,now.step+1,now.apl+"C"});
            mp[s3]=1;
        }

    }

}

int main()
{
    
    

   for(int i=0;i<8;i++)
   {
    
    
       int x;
      scanf("%d",&x);
      ed+=(char)(x+'0');

   }

   if(st==ed) cout<<0<<endl;
   else
   bfs();
   
  

    return 0;

}


猜你喜欢

转载自blog.csdn.net/qq_43619680/article/details/109477749