搞了一個晚上,果然好久不寫信息的後遺症。(辣雞中考)
題目鏈接:https://www.luogu.org/problemnew/show/P2739
本質上就是曾經很拿手的深搜,但有一個很巧妙地貪心優化沒有想到吶。
爲了儘快的完成目標,白的只能往左走,黑的只能往右走。(不然就是往回走惹)
算是漲姿勢了。。。
(謹以此代碼,紀念我逝去的晚上與悼念我明天要崩的人機對話)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2000005;
string a;int flag=1;
map<string,int> m;
int n;int N=0;
string goal;
struct node
{
string k;int pos,fa,node,op;
};
node si[maxn];
node ans;int res[maxn];
queue<node> q;
int main()
{
scanf("%d",&n);string s="";
for(int i=1;i<=n;i++) {s=s+'W';goal=goal+'B';}
s=s+' ';goal=goal+' ';
for(int i=n+2;i<=2*n+1;i++) {s=s+'B';goal=goal+'W';}
node t;t.k=s;t.pos=n;m[t.k]=1;t.node=++N;
q.push(t);
while(!q.empty())
{
node tmp=q.front();q.pop();
if(tmp.pos>0&&tmp.k[tmp.pos-1]=='W')//這裏是優化
{
node temp=tmp;
swap(temp.k[temp.pos],temp.k[temp.pos-1]);
temp.op=temp.pos-1;temp.pos-=1;temp.fa=tmp.node;temp.node=++N;si[N]=temp;
if(!m[temp.k]) {q.push(temp);m[temp.k]=1;}
if(temp.k==goal)
{
ans=temp;
break;
}
}
if(tmp.pos<2*n&&tmp.k[tmp.pos+1]=='B')
{
node temp=tmp;
swap(temp.k[temp.pos],temp.k[temp.pos+1]);
temp.op=temp.pos+1;temp.pos+=1;temp.fa=tmp.node;temp.node=++N;si[N]=temp;
if(!m[temp.k]) {q.push(temp);m[temp.k]=1;}
if(temp.k==goal)
{
ans=temp;
break;
}
}
if(tmp.pos>1&&tmp.k[tmp.pos-2]=='W')
{
node temp=tmp;
swap(temp.k[temp.pos],temp.k[temp.pos-2]);
temp.op=temp.pos-2;temp.pos-=2;temp.fa=tmp.node;temp.node=++N;si[N]=temp;
if(!m[temp.k]) {q.push(temp);m[temp.k]=1;}
if(temp.k==goal)
{
ans=temp;
break;
}
}
if(tmp.pos<2*n-1&&tmp.k[tmp.pos+2]=='B')
{
node temp=tmp;
swap(temp.k[temp.pos],temp.k[temp.pos+2]);
temp.op=temp.pos+2;temp.pos+=2;temp.fa=tmp.node;temp.node=++N;si[N]=temp;
if(!m[temp.k]) {q.push(temp);m[temp.k]=1;}
if(temp.k==goal)
{
ans=temp;
break;
}
}
}
int tot=0;
for(int i=ans.node;i!=1;i=si[i].fa)
{
res[++tot]=si[i].op+1;
}
int K=0;
for(int i=tot;i>=1;i--)
{
cout<<res[i]<<" ";K++;
if(K%20==0) cout<<endl;
}
cout<<endl;
return 0;
}