翻硬币

历届试题 翻硬币
时间限制:1.0s 内存限制:256.0MB

问题描述

小明正在玩一个“翻硬币”的游戏。

桌上放着排成一排的若干硬币。我们用 * 表示正面,用 o 表示反面(是小写字母,不是零)。

比如,可能情形是:**oo***oooo

如果同时翻转左边的两个硬币,则变为:oooo***oooo

现在小明的问题是:如果已知了初始状态和要达到的目标状态,每次只能同时翻转相邻的两个硬币,那么对特定的局面,最少要翻动多少次呢?

我们约定:把翻动相邻的两个硬币叫做一步操作,那么要求:
输入格式

两行等长的字符串,分别表示初始状态和要达到的目标状态。每行的长度< 1000

输出格式

一个整数,表示最小操作步数。

样例输入

**********
o****o****

样例输出

5

样例输入

*o**o***o***
*o***o**o***

样例输出

1

方法一:BFS(时间超限)qwq

#include<bits/stdc++.h>
using namespace std;
int n[1000];
map<string, int> mp;
queue<string> q;
string change(string temp, int i, int j) {
    if (temp[n[i]] == '*') {
        temp[n[i]] = 'o';
        if (temp[n[i] + j] == '*')
            temp[n[i] + j] = 'o';
        else
            temp[n[i] + j] = '*';
    } else {
        temp[n[i]] = '*';
        if (temp[n[i] + j] == '*')
            temp[n[i] + j] = 'o';
        else
            temp[n[i] + j] = '*';
    }
    return temp;
}

int BFS(string str, string goal) {
    int cnt = 0;
    string temp,copy;
    if (str == goal)
        return cnt;
    q.push(str);
    mp[str] = 0;
    while (!q.empty()) {
        str = q.front();
        temp = str;
        int len = str.size();
        int j = 0;
        for (int i = 0; i < len; i++)
            if (temp[i] != goal[i])
                n[j++] = i;
        for (int i = 0; i < j; i++) {
            copy=temp;
            if (n[i] > 0) {
                temp=change(temp, i, -1);
                if (temp == goal)
                    return mp[str] + 1;
                if (mp.find(temp) == mp.end()) {
                    mp[temp] = mp[str] + 1;
                    q.push(temp);
                }
                temp=copy;
            }
            if (n[i] +1 < len) {
                temp=change(temp, i, 1);
                if (temp == goal)
                    return mp[str] + 1;
                if (mp.find(temp) == mp.end()) {
                    mp[temp] = mp[str] + 1;
                    q.push(temp);
                }
                temp=copy;
            }
        }
        q.pop();
    }
    return -1;
}
int main() {
    string str, goal;
    cin >> str >> goal;
    cout << BFS(str, goal) << endl;
    return 0;
}

方法二

#include<bits/stdc++.h>
using namespace std;
int Find_fist(string str, string goal, int len) {
    for (int i = 0; i < len; i++)
        if (str[i] != goal[i])
            return i;
    return -1;
}

int main() {
    string str, goal;
    cin >> str >> goal;
    int len = str.length();
    int cnt = 0, i;
    while ((i = Find_fist(str, goal, len)) != -1) {
        str[i] = goal[i];
        if (str[i + 1] == '*')
            str[i + 1] = 'o';
        else
            str[i + 1] = '*';
        ++cnt;
    }
    cout << cnt << endl;
    return 0;
}

翻硬币二

翻币问题。有N个硬币(N≥6)正面朝上排成一排,每次将5个硬币翻过来放在原位置,直到最后全部硬币翻成反面朝上为止。编程让计算机找出步数最少的翻法并把翻币次数打印出来(用O表示正面,*表示反面)

代码(BFS,没有找到数据,未测试)

#include<bits/stdc++.h>
using namespace std;
map<int, int> mp;
map<int,string>data;
queue<int> q;
int BFS(int cnt) {
    if (cnt%5 == 0)
        return cnt/5;
    int ans = cnt / 5;
    cnt = cnt % 5;
    mp[cnt] = ans;
    q.push(cnt);
    data[cnt]=
    while (q.size()) {
        cnt = q.front();
        q.pop();
        for (int i = 1,j; i <= cnt; i++) {
            j=5 - i + cnt - i;
            if (j%5 == 0)
                return mp[cnt] + 1;
            if (mp.find(j % 5) == mp.end())
            {
                mp[j% 5] = mp[cnt] + 1 + j % 5;
                q.push(j%5);
            }
        }
    }
    return -1;
}

int main() {
    string str, goal;
    cin >> str >> goal;
    int len = str.length();
    int cnt = 0;
    for (int i = 0; i < len; i++)
        if (str[i] != goal[i])
            ++cnt;
    cout << BFS(cnt) << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值