社区讨论

abc D求调(玄关)

灌水区参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@lyazxwxx
此快照首次捕获于
2024/07/07 11:31
2 年前
此快照最后确认于
2024/07/07 11:31
2 年前
查看原帖
rt,思路是优先队列优化广搜,WA 了三个点不知道原因。
CPP
#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;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...