如果没有解决方案怎么知道?算法任务

时间:2015-10-01 20:09:54

标签: algorithm search math game-theory

简而言之,给出了这样一个问题:

我们加载了玩家数量,

每个玩家赚钱,

我们加载一个由L i W

组成的字符串

例如:

4 - >玩家' S

2,3,2,1

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

我们加载周期

例如:

WLL - > W == win =现金+ 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;

1 个答案:

答案 0 :(得分:0)

假设你有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;
}
相关问题