forked from sanketpatil02/Code-Overflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoin Tower Game.cpp
More file actions
70 lines (60 loc) · 1.51 KB
/
Coin Tower Game.cpp
File metadata and controls
70 lines (60 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
Problem Statement:
Coin Tower Game
Whis and Beerus are playing a new game today. They form a tower of N coins and make a move in alternate turns. Beerus plays first. In one step, the player can remove either 1, X, or Y coins from the tower. The person to make the last move wins the game. Can you find out who wins the game?
Input format :
The first and the only line of input contains three integer values separated by a single space. They denote the value of N, X and Y, respectively.
Output format :
Prints the name of the winner, either 'Whis' or 'Beerus' (Without the quotes).
Constraints :
1 <= N <= 10 ^ 6
2 <= X <= Y<= 50
Time Limit: 1 sec
Sample Input 1 :
4 2 3
Sample Output 1 :
Whis
Sample Input 2 :
10 2 4
Sample Output 2 :
Beerus
Code:
#include<bits/stdc++.h>
using namespace std;
bool helper(int x, int y, int n)
{
int *dp=new int[n + 1];
dp[0]=0;
dp[1]=1;
dp[x]=1;
dp[y]=1;
for (int i = 2; i <= n; i++)
{
if(i!=1 && i!=x && i!=y)
{
dp[i]=dp[i-1]^1;
if (i - x >= 1)
{
dp[i] =max(dp[i],dp[i - x]^1);
}
if (i - y >= 1)
{
dp[i] =max(dp[i],dp[i - y]^1);
}
}
}
for(int i=1;i<n+1;i++)
{
cout<<i<<" "<<dp[i]<<endl;
}
return dp[n];
}
string findWinner(int n, int x, int y)
{
string a="Whis";
string b="Beerus";
int k=helper(x,y,n);
if(k==0)
return a;
else
return b;
}