蓝桥杯青蛙跳杯子+同类型题8数码(bfs最短步数 + 字符串处理)

在这里插入图片描述
链接:

http://lx.lanqiao.cn/problem.page?gpid=T2835

这道题和8数码是同类型题。
八数码链接

都是求最短操作次数的,并且都是对字符串进行操作。

8数码ac代码:

#include <iostream>
#include <queue>
#include <unordered_map>
#include <algorithm>
using namespace std;

unordered_map<string,bool> vis;

int direction[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
string start;
string e = "12345678x";
int bfs()
{
    int step = 0;
    queue<string> q;
    q.push(start);
    vis[start] = true;
    while(!q.empty())
    {
        int sz = q.size();
        for(int i = 0; i < sz; i++)
        {
            string s = q.front();
            q.pop();
            int pos;
            if(s == e) return step;
            for(int i = 0; i < 9; i++)
                if(s[i]=='x')
                    pos = i;
            int x = pos/3, y = pos % 3;
            for(int i = 0; i < 4; i++)
            {
                int newx = x + direction[i][0];
                int newy = y + direction[i][1];
                if(newx < 3 && newx >=0 && newy >= 0 && newy < 3)
                {
                    string tmp = s;
                    int newpos = newx*3 + newy;
                    swap(tmp[newpos],tmp[pos]);
                    if(vis[tmp] == false)
                    {
                        q.push(tmp);
                        vis[tmp] = true;
                    }
                }
            }
        }
        step++;
    }
    return -1;
}

int main()
{
    char k;
    while(cin>>k)
    {
        if(k==' ') continue;
        else start+=k;
    }
    int res = bfs();
    if(res == -1) cout<<"-1";
    else cout<<res;
}

注:进行字符串变化的时候,变化了就要还原回去,不然会影响下一次变化。有两种操作可以避免这种情况。第一种是手动还原,第二种是对临时变量进行变换。第二种更好。

青蛙跳杯子也是如此,跳的条件有三个,且都可以向左向右跳,因此有六个条件。每次跳就是交换两个字符的位置。记得要创建临时变量来进行变换,而不是对原字符串进行变换。

ac代码:

#include <iostream>
#include <queue>
#include <string>
#include <map>
using namespace std;

string s, obj;
queue<string> q;
map<string, bool> vis;

int bfs()
{
	q.push(s);
	vis[s] = true;
	int step = 0;
	while(q.size())
	{
		int sz = q.size();
		for(int i = 0; i < sz; i++)
		{
			string t = q.front();
			q.pop();
			if(t == obj) return step;
			int u = t.find('*');
			//cout << u << " ";
			for(int j = 0; j < 6; j++)
			{
				string tmp = t;
				//和相连的左边交换
				if(j == 0 && u - 1 >= 0) swap(tmp[u], tmp[u - 1]);
				else if(j == 1 && u + 1 < t.size()) swap(tmp[u], tmp[u + 1]);
				else if(j == 2 && u - 2 >= 0) swap(tmp[u], tmp[u - 2]);
				else if(j == 3 && u + 2 < t.size()) swap(tmp[u], tmp[u + 2]);
				else if(j == 4 && u + 3 < t.size()) swap(tmp[u], tmp[u + 3]);
				else if(j == 5 && u - 3 >= 0) swap(tmp[u], tmp[u - 3]);
				if(vis[tmp] == true) continue;
				//cout << tmp << endl;
				q.push(tmp);
				vis[tmp] = true;
				
			}
		}
		step++;
	}
	return -1;
}

int main()
{
	cin >> s;
	cin >> obj;
	cout << bfs();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值