无名选手


私信TA

用户名:qwer123456

访问量:10056

签 名:

qwer123456

等  级
排  名 836
经  验 3499
参赛次数 10
文章发表 25
年  龄 0
在职情况 学生
学  校 某知名大专
专  业

  自我简介:

解题思路:就是个典型广搜就可以了

注意事项:这狗比垃圾水题一道,题没读完就开始解题,

               然后有个条件没看到一直不对

参考代码:

#include<iostream>
#include<queue>
#include<map>
#include<string>
using namespace std;
map<string,int>vis;
struct node{
 string s;
 int we,step;
};
string ss;
queue<node>q;
int dir[6]={1,-1,2,-2,3,-3};
int main()
{
 node st;
 cin>>ss;
 cin>>st.s;
 for(int i=0;i<ss.size();i++)
 {
  if(st.s[i]=='*')
    st.we=i;
 }
 st.step=0;
 if(st.s==ss)
 {
 
 cout<<st.step;
    return 0;
 }
 node h,t;
 vis[t.s]=1;
 q.push(st);
    while(!q.empty())
 {
  h=q.front();
  q.pop();
  for(int i=0;i<6;i++)
  {
      t.we=h.we+dir[i];
  if(t.we>=0&&t.we<ss.length())
  {
  
   t.s=h.s;
   t.step=h.step+1;
   swap(t.s[t.we],t.s[h.we]);
   if(!vis[t.s])
   {
    
   vis[t.s]=1;
   if(t.s==ss)
   {
    cout<<t.step;
    return 0;
   }
   q.push(t);
      }
      }
    }
  }
 return 0;
}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区