11. 青蛙跳杯子

题目链接:https://www.lanqiao.cn/problems/102/learning/?page=1&first_category_id=1&second_category_id=3

题意:

一排杯子,青蛙可以跨过0或1或2个青蛙,跳进空杯子中。求从初始局面转化到题目给定局面所需要的最小步数

思路:

用bfs模拟,队列里塞的是字符串的状态,用map来查询当前字符串是否出现过,若没出现过,塞进去,并且更新 此字符串答案 为 由上一个字符串转化来的答案+1

#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define pb push_back
#define endl "\n"
#pragma GCC optimize(3)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const ll llmax=LLONG_MAX;
const int maxn=1e5+5;
const int mod=1e9+7;
string a,b;
map<string,int>mp;
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0);
	cin>>a>>b;

	queue<string>q;
	q.push(a);
	mp[a]=0;
	while(!q.empty()){
		string x=q.front();q.pop();
		if(x==b){
			cout<<mp[x];
			break;
		}
		int pos=x.find('*');
		
		rep(i,0,x.size()-1){
			if(x[i]=='*'||abs(i-pos)>3)continue;
			
			swap(x[i],x[pos]);
			if(mp[x]==0){
				string k=x;
				swap(k[i],k[pos]);
				mp[x]=mp[k]+1;
				q.push(x);
			}
			
			swap(x[i],x[pos]);
		}
		
	}
	return 0;
}


posted @ 2025-02-21 17:41  Marinaco  阅读(15)  评论(0)    收藏  举报
//雪花飘落效果