abc D求调(玄关)
  • 板块灌水区
  • 楼主all__
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/7/7 11:29
  • 上次更新2024/7/7 11:31:16
查看原帖
abc D求调(玄关)
938210
all__楼主2024/7/7 11:29

rt,思路是优先队列优化广搜,WA 了三个点不知道原因。

#include<iostream>
#include<queue>
#include<map>
#define int long long
using namespace std;
int n;
string s,t;
map<string,int> mo;
priority_queue<pair<int,pair<string,int> > > pro;
void bfs(){
	pro.push({0,{s,n+1}});
	while(!pro.empty()){
		string awa=pro.top().second.first;
		int bs=-pro.top().first,x=pro.top().second.second;pro.pop();
		if(awa==t){
			cout<<bs<<endl;
			break;
		}
		if(mo[awa]) continue; mo[awa]=1;
		for(int i=1;i<n+2;i++){
			if(i==x||i==x+1||i==x-1) continue;
			swap(awa[i],awa[x]);swap(awa[i+1],awa[x+1]);
			pro.push({-(bs+1),{awa,i}});
			swap(awa[i],awa[x]);swap(awa[i+1],awa[x+1]);
		}
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);int a[2]={0,0},b[2]={0,0};
	cin>>n>>s>>t;s="6"+s+"..",t="6"+t+"..";
	for(int i=1;i<=n;i++)
		a[(s[i]=='B')]++,b[(t[i]=='B')]++;
	if(a[0]!=b[0]||a[1]!=b[1]){cout<<-1<<endl;return 0;}
	bfs();
	return 0;
}
2024/7/7 11:29
加载中...