【问题标题】:How can I tell when there are no solutions? Algorithm task [closed]我怎么知道什么时候没有解决方案?算法任务[关闭]
【发布时间】:2015-12-30 00:09:11
【问题描述】:

简而言之,鉴于这样一个问题:

我们加载玩家的数量,

给每个玩家钱,

我们加载一个由 L i W 组成的字符串

例如:

4 -> 玩家的

2、3、2、1

2 是第一个玩家的钱,3 是第二个等等。

我们加载循环

例如:

WLL -> W == 赢 = 现金 + 1,L == 输 = 现金 -1;

如果其中一位玩家的钱用完了,则中断游戏,并给出所有玩家的游戏数量。

所以:

循环不断重复,所以,我们有 WLLWLL ... WLL

2、3、2、1

[WLL - 第一个周期] [WLL - 下一个周期]

所以,我们有:

3,2,1,2

下一个:

2,1,2,1

最后:

1,2,1,0

我们计算游戏的数量 - 12

玩家永远不会输的情况也是如此,然后你写-1

所以,

我的问题是:我如何编写一个程序来计算它的效率,如果游戏永远不会结束写-1?

我有类似的东西:

enter code here

#include<vector>
#include<iostream>
using namespace std;


int main()
{
int n, m, ile_gier;
bool nieskonczonosc = true;
cin >> n;
int tab[n];
for(int i = 0; i < n; i++)
{
    cin >> tab[i];
}
cin >> m;
char znak[m];
cin >> znak;
int przesuniecie = n%m;
for(int i = 0; i < n; i++)
{
    if(znak[i+przesuniecie] == 'W') nieskonczonosc = true;

}
if(nieskonczonosc == true) cout << "-1" << endl;

【问题讨论】:

    标签: algorithm search math game-theory


    【解决方案1】:

    假设你有 n 个玩家和一个长度为 k 的 cicle,每个玩家的钱都大于 n。 因此,下一行将移动 n mod k。现在您可以计算 k 行之后的货币变化(偏移肯定为 0)现在您可以计算游戏的状态和游戏即将结束的行数(仅当 n 行之后的所有变化都是正数时)游戏没有结束)之后你可以直接计算它。

    PS 您可以通过采用 n/gCd(n,k) 行来改进这一点,因为移位也将为 0。并且您可以计算所有玩家在 n 轮中的最大金钱减少量。因此玩家的钱可以在你必须评估一切之前降到这个值.....

    编辑:这是一个示例代码...它并不完美,但您应该能够理解我的意思.....

    #include<string>
    #include<iostream>
    #include<algorithm>
    
    int bruteforce(int nPlayer, int* money, const std::string,int,bool doesEnd);
    void calculateWin(int nPlayer, const std::string Cycle, int* changes);
    int fastforward(int nPlayer, int* money,int* changes);
    
    int main()
    {
        const int nPlayer = 4;
        int money[nPlayer] = { 5,5,5,5 };
        std::string Cycle = "WLL";
    
        int changes[nPlayer];
    
        calculateWin(nPlayer, Cycle, changes);
    
        bool doesEnd = false;
        for (int i = 0; i < nPlayer;i++)
            if (changes[i] < 0)
            {
                doesEnd = true;
                break;
            }
    
    
    
        if (doesEnd)
        {
            int fast = 0;
             fast = fastforward(nPlayer, money, changes);
            std::cout << "Games: " << fast*Cycle.length()*nPlayer+ bruteforce(nPlayer, money, Cycle, 0, 1) << std::endl;
        }
        else
        {
            int games = bruteforce(nPlayer, money, Cycle, 0, 0);
            if (games == -1)
                std::cout << "The Game will not end" << std::endl;
            else
                std::cout << "Games: " << games<<std::endl;
        }
    
        system("pause");
    
    }
    
    int bruteforce(int nPlayer, int* money, const std::string Cycle, int offset,bool doesend)
    {
        int nGames = 0;
        int player = 0;
        while (true)
        {
            player %= nPlayer;
            offset %= Cycle.length();
            for (int i = 0; i < nPlayer;i++)
                if (money[i] <= 0) return nGames;
            money[player] += Cycle[offset] == 'W' ? 1 : -1;
            player++;
            offset++;
            nGames++;
            if (!doesend&&nGames == nPlayer*Cycle.length())return -1;
    
    
        }
    }
    
    
    void calculateWin(int nPlayer, const std::string Cycle, int* changes) // calculates the changes after nPlayer*Cycle.length() Games
    {
        int shift = nPlayer % Cycle.length();
        for (int i = 0; i < nPlayer;i++)
        {
            changes[i] = 0;
            for (int j = 0;j < Cycle.length();j++)
                changes[i]+= Cycle[(j*shift+i)%Cycle.length()] == 'W'?1:-1;
    
        }
    }
    
    int fastforward(const int nPlayer, int* money, int* changes)
    {
    
        int res = 2147483647; // max int 
    
        for (int i = 0; i < nPlayer;i++)
        {
            if(changes[i]<0)
                res = std::min( (money[i] - nPlayer) / -changes[i],res);
        }
        if (res < 0)
            return 0;
    
    
        for (int i = 0; i < nPlayer; i++)
            money[i] += res * changes[i]; 
    
        return res;
    }
    

    【讨论】:

    • 你可以把它写成步骤信吗?
    • 我的代码不起作用 - 我不知道为什么
    • 你能用c++写这个吗?
    • 如何计算?
    • 看看我的代码,下一步是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 2017-02-10
    • 2022-10-15
    • 2010-09-10
    • 1970-01-01
    • 2021-12-08
    • 2016-08-07
    相关资源
    最近更新 更多