Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Game Problems #111

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions C++ Problem/Games/Chocolate_Game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
using namespace std;

// Problem: Chocolate Game
// Description: Two players take turns breaking a chocolate bar of size n x m.
// The player who cannot make a move loses. The first player wins if both dimensions are odd.
// Time Complexity: O(1), as we only check the dimensions.
// Space Complexity: O(1), no additional space used.

string chocolateGame(int n, int m) {
return (n % 2 == 1 && m % 2 == 1) ? "First" : "Second"; // First player wins if both dimensions are odd
}

int main() {
int n, m; // Dimensions of the chocolate
cin >> n >> m;
cout << chocolateGame(n, m) << endl; // Output winner
return 0;
}
37 changes: 37 additions & 0 deletions C++ Problem/Games/Coin_Game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
#include <vector>
using namespace std;

// Problem: Coin Game
// Description: Two players take coins from either end of a line of coins,
// trying to maximize their total value. Each player has perfect knowledge of the game.
// Time Complexity: O(n^2), due to filling the DP table.
// Space Complexity: O(n^2), for the DP table.

int coinGame(vector<int>& coins) {
int n = coins.size();
vector<vector<int>> dp(n, vector<int>(n, 0));

for (int i = 0; i < n; i++) {
dp[i][i] = coins[i]; // If only one coin, take it
}

for (int length = 2; length <= n; length++) {
for (int i = 0; i <= n - length; i++) {
int j = i + length - 1;
dp[i][j] = max(coins[i] - dp[i + 1][j], coins[j] - dp[i][j - 1]);
}
}
return dp[0][n - 1];
}

int main() {
int n; // Number of coins
cin >> n;
vector<int> coins(n);
for (int i = 0; i < n; i++) {
cin >> coins[i]; // Input coin values
}
cout << coinGame(coins) << endl; // Output maximum value
return 0;
}
19 changes: 19 additions & 0 deletions C++ Problem/Games/Game_Of_Stones.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
using namespace std;

// Problem: Game of Stones
// Description: Two players take turns removing 1, 2, or 3 stones from a pile.
// The player who takes the last stone wins. The first player wins if the number of stones is not a multiple of 4.
// Time Complexity: O(1), as we perform a single modulus operation.
// Space Complexity: O(1), no additional space used.

bool canWin(int n) {
return n % 4 != 0; // Player can win if the number of stones is not a multiple of 4
}

int main() {
int n; // Number of stones
cin >> n;
cout << (canWin(n) ? "First" : "Second") << endl; // Output winner
return 0;
}
40 changes: 40 additions & 0 deletions C++ Problem/Games/Grundy_Numbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
#include <vector>
using namespace std;

// Problem: Grundy Numbers
// Description: In a game where players can take stones from a pile,
// we need to find the Grundy number for each position to determine
// winning strategies. The Grundy number for a position is calculated
// using the minimum excludant (mex) of the Grundy numbers of all
// positions reachable from it.
// Time Complexity: O(n * k), where n is the number of piles and k is the maximum number of stones a player can take.
// Space Complexity: O(n), for storing Grundy numbers.

int findGrundy(int n, const vector<int>& moves) {
vector<int> grundy(n + 1, 0); // Grundy numbers array
for (int i = 1; i <= n; i++) {
vector<bool> reachable(n + 1, false); // Track reachable Grundy numbers
for (int move : moves) {
if (i - move >= 0) {
reachable[grundy[i - move]] = true; // Mark reachable
}
}
// Find minimum excluded value (mex)
for (int j = 0; j <= n; j++) {
if (!reachable[j]) {
grundy[i] = j; // Assign the mex value
break;
}
}
}
return grundy[n];
}

int main() {
int n; // Number of stones
cin >> n;
vector<int> moves = {1, 2, 3}; // Possible moves
cout << findGrundy(n, moves) << endl; // Output the Grundy number
return 0;
}
31 changes: 31 additions & 0 deletions C++ Problem/Games/K_Game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
#include <vector>
using namespace std;

// Problem: K Game
// Description: In a game where two players alternate taking stones from a
// pile, with each player allowed to take 1 to K stones per turn. The player
// unable to make a move loses. Determine if the first player can win given the
// total number of stones and K.
// Time Complexity: O(n*K), where n is the total number of stones and K is the max stones that can be taken.
// Space Complexity: O(n), for storing the winning states.

bool canFirstPlayerWin(int n, int k) {
vector<bool> dp(n + 1, false); // Winning positions
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= k; j++) {
if (i - j >= 0 && !dp[i - j]) {
dp[i] = true; // If I can force the opponent to lose
break;
}
}
}
return dp[n]; // Return the result for n stones
}

int main() {
int n, k; // Total number of stones and max stones that can be taken
cin >> n >> k;
cout << (canFirstPlayerWin(n, k) ? "First" : "Second") << endl; // Output winner
return 0;
}
28 changes: 28 additions & 0 deletions C++ Problem/Games/Nim_Game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>
#include <vector>
using namespace std;

// Problem: Nim Game
// Description: Two players take turns removing objects from distinct piles.
// The player who takes the last object wins. The winner can be determined using the nim-sum (XOR of pile sizes).
// Time Complexity: O(n), where n is the number of piles.
// Space Complexity: O(1), no additional space used.

string nimGame(vector<int>& piles) {
int nimSum = 0;
for (int pile : piles) {
nimSum ^= pile; // Calculate the nim-sum
}
return (nimSum != 0) ? "First" : "Second"; // First player wins if nim-sum is non-zero
}

int main() {
int n; // Number of piles
cin >> n;
vector<int> piles(n);
for (int i = 0; i < n; i++) {
cin >> piles[i]; // Input pile sizes
}
cout << nimGame(piles) << endl; // Output winner
return 0;
}
Empty file.
76 changes: 76 additions & 0 deletions C++ Problem/Graphs/Bellman_ford.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#define ll long long
#define pb push_back
#define vl vector<long long>
#define mll map<long long, long long>
#define pll pair<long long,long long>
#define mod 1000000007
#define fr(c,a,b) for(ll c=a;c<b;c++)
#include<bits/stdc++.h>
using namespace std;

// Bellman Ford Algo is used to find the single source shortest paths in
// both directed and undirected

// But it has some advantages over Dijkstra algo that we can have negative
// edges and can also detect negative cycles as it only takes n-1
// passes to find the shortest path if the values still changes after that
// then negative cycles exist

vector<vector<ll>> edges;
vector<ll> dist;

void BF_Algo(ll source, ll n) {
fr(c, 0, n) {
dist.pb(INT_MAX);
}
dist[source] = 0;
ll cost = 0;

fr(c,0,n-1){
for (auto it : edges) {
ll u = it[1];
ll v = it[2];
ll w = it[0];
dist[v]=min(dist[v],dist[u]+w);
}
}

fr(c,0,n){
if(dist[c]!=INT_MAX){
cout<<dist[c]<<" ";
}else{
cout<<-1<<endl;
}
}

}

void solve() {
ll n, m;
cin >> n >> m;
fr(c, 0, m) {
ll w, u, v;
cin >> w >> u >> v;
edges.pb({w,u,v});
}
BF_Algo(0, n);
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
solve();
}
return 0;
}

/*
Negative weights are found in various applications of graphs.
For example, instead of paying the cost for a path, we may get some advantage if we follow the path.

Bellman-Ford works better (better than Dijkstra's) for distributed systems. Unlike Dijksra's where we need
to find the minimum value of all vertices, in Bellman-Ford, edges are considered one by one.
*/
99 changes: 99 additions & 0 deletions C++ Problem/Graphs/Bipartite_graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#define ll long long
#define pb push_back
#define mod 1000000007
#define fr(c,a,b) for(ll c=a;c<b;c++)
#include<bits/stdc++.h>
using namespace std;

class Graph
{
ll V;
list<ll> *adj;
void Colour_check(ll v,bool visited[], ll color);
public:
Graph(ll V);
void addEdge_U(ll v, ll w);
void addEdge_D(ll v, ll w);
bool isBipartite();
};

Graph::Graph(ll V)
{
this->V = V;
adj = new list<ll>[V];
}

void Graph::addEdge_U(ll v, ll w)
{
adj[v].pb(w);
adj[w].pb(v);
}
void Graph::addEdge_D(ll v, ll w)
{
adj[v].pb(w);
}

vector<ll> color;
bool bipart;
void Graph::Colour_check(ll v,bool visited[], ll col)
{
if(color[v]!=-1 && color[v]!=col){
bipart = false;
return;
}color[v]=col;
if(visited[v]){
return;
}
visited[v] = true;
list<ll>::iterator i;
for(i = adj[v].begin(); i != adj[v].end(); ++i)
{
Colour_check(*i, visited, (col^1));
}

}

bool Graph::isBipartite()
{
bipart = true;
bool *visited = new bool[V];
color = vector<ll> (V,-1);
fr(c,1,V){
visited[c]=false;
}
fr(c,1,V){
if(!visited[c]){
Colour_check(c, visited, 0);
}
}
return bipart;
}

void solve()
{
Graph g(9);
g.addEdge_U(1,2);
g.addEdge_U(2,3);
g.addEdge_U(3,4);
g.addEdge_U(4,5);
g.addEdge_U(5,6);
g.addEdge_U(6,7);
g.addEdge_U(7,8);
g.addEdge_U(8,1);
if(g.isBipartite()){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}

}

int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t=1;
while(t--)
{
solve();
}
}
Loading