您的位置:首页 > 其它

hdu 1430 魔板

2015-05-17 23:04 435 查看
康拓展开?双向Bfs?

no,no,no,简单单向bfs+map判重就好,不过预处理的思想是不变的。

不过我看了许多博客,也没人解释为何12345678可以达到其他所有的状态

#include<iostream>
#include<map>
#include<queue>
using namespace std;
string st,ed;
map<string,string>mapp;
map<string,string>::iterator it;
struct stu
{
string x;
string l;
};
string a(string x)//a变换
{
string ans="";
for(int i=x.size()-1;i>=0;i--)
{
ans+=x[i];
}
return ans;
}
string b(string x)//b变换
{
string ans="";
ans+=x[3];ans+=x[0];ans+=x[1];ans+=x[2];ans+=x[5];ans+=x[6];ans+=x[7];ans+=x[4];
return ans;
}
string c(string x)//c变换
{
string ans="";
ans+=x[0];ans+=x[6];ans+=x[1];ans+=x[3];ans+=x[4];ans+=x[2];ans+=x[5];ans+=x[7];
return ans;
}
void bfs()//预处理所有的变换情况
{
stu x,y;
x.x="12345678";
x.l="";
queue<stu>root;
root.push(x);
while(root.size())
{
x=root.front();
root.pop();
it=mapp.find(x.x);
if(it!=mapp.end()) continue;
mapp[x.x]=x.l;
y.x=a(x.x);y.l=x.l+"A";
root.push(y);
y.x=b(x.x);y.l=x.l+"B";
root.push(y);
y.x=c(x.x);y.l=x.l+"C";
root.push(y);
}
}
int main()
{
cin.sync_with_stdio(false);
bfs();
while(cin>>st>>ed)
{
string ans="";
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
if(ed[i]==st[j]) ans+=(j+'0'+1);
}
}
cout<<mapp[ans]<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: