假設您有n個球員和長度爲k的一個cicle每個玩家有足夠的資金越大,然後n。 因此,下一行將被移位n模k。現在你可以計算k行後的金錢變化(那裏的變化肯定是0),現在你可以計算遊戲的狀態和遊戲即將結束的行數(只有當n行後的所有變化都是正數時遊戲不會結束)之後,你可以直接計算它。
PS可以通過取N/GCD(N,K)行改善這一點,因爲那裏的移位爲0,以及。你可以計算n輪所有玩家的最大減少金錢。因此,玩家的錢可以踏踏實實地這個值,你必須EVAL之前的一切.....
編輯:這是一個示例代碼......它並不完美,但你應該能理解我..... .....
#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;
}
你可以寫在步驟的信? – Minnkok
我的代碼doesen't工作 - 我不知道爲什麼 – Minnkok
你可以寫在C++? – Minnkok