Skip to content

Commit

Permalink
Adding problems related to Algorithm Games
Browse files Browse the repository at this point in the history
  • Loading branch information
Danish authored and Danish committed Oct 8, 2014
1 parent 2157aef commit feebb79
Show file tree
Hide file tree
Showing 9 changed files with 486 additions and 0 deletions.
72 changes: 72 additions & 0 deletions Algorithm_Games/GameOfSegments.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <sstream>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <bitset>
#include <algorithm>
using namespace std;

template <class T> void checkmax(T &t, T x) { if (x > t) t = x; }
template <class T> void checkmin(T &t, T x) { if (x < t) t = x; }
template <class T> void _checkmax(T &t, T x) { if (t == -1 || x > t) t = x; }
template <class T> void _checkmin(T &t, T x) { if (t == -1 || x < t) t = x; }
typedef long long ll;
class GameOfSegments {
public:
int mem[1010];

int grundy(int n) {
if(n==0 || n == 1) return 0;
if(mem[n]!=-1) return mem[n];

set<int> reachable;
for(int i=0;i<=n-2;i++) {
int a = grundy(i);
int b = grundy(n-i-2);
reachable.insert(a^b);
}
int answer = 0;
while(reachable.count(answer)) answer++;

return mem[n] = answer;
}

int winner(int N) {
memset(mem,-1,sizeof(mem));
if(grundy(N)) {
return 1;
}
return 2;
}

// BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { int Arg0 = 3; int Arg1 = 1; verify_case(0, Arg1, winner(Arg0)); }
void test_case_1() { int Arg0 = 4; int Arg1 = 1; verify_case(1, Arg1, winner(Arg0)); }
void test_case_2() { int Arg0 = 15; int Arg1 = 2; verify_case(2, Arg1, winner(Arg0)); }
void test_case_3() { int Arg0 = 191; int Arg1 = 2; verify_case(3, Arg1, winner(Arg0)); }

// END CUT HERE

};

// BEGIN CUT HERE
int main() {
GameOfSegments ___test;
___test.run_test(-1);
}
74 changes: 74 additions & 0 deletions Algorithm_Games/MATGAME.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <string.h>

using namespace std;

int n,m;
vector<vector<int> > matrix;
int mem[51][51];

int solve(int row, int index) {

if(index == m-1) {
return matrix[row][index];
}

if(mem[row][index]!=-1) return mem[row][index];

int temp = solve(row, index+1);

if(matrix[row][index] <= temp) {

return mem[row][index] = matrix[row][index] - 1;
} else {
return mem[row][index] = matrix[row][index];
}
}

int compute() {
int answer =0;
memset(mem,-1,sizeof(mem));
for(int i=0;i<n;i++) {

int temp = solve(i, 0);
answer ^= temp;

}
return answer;
}
int main() {
// your code goes here
ios_base::sync_with_stdio(false);
int test;
cin>>test;
while(test--) {

cin>>n>>m;
matrix.clear();
for(int i=0;i<n;i++) {
vector<int> t;

for(int j=0;j<m;j++) {
int temp;
cin>>temp;

t.push_back(temp);
}
matrix.push_back(t);
}

int val = compute();
if(val == 0) {
cout<<"SECOND"<<endl;
} else {
cout<<"FIRST"<<endl;
}
}
return 0;
}



47 changes: 47 additions & 0 deletions Algorithm_Games/MMMGAME.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm> // binsearch,max(a,b),min(a,b)
#include <math.h>
#include <queue>

#include <vector>
#include <set>
#include <list>
#include <map>
#include <string.h> // memset
#include <cstdlib> // abs(int),labs(int),llabs(int),min,max
#include <limits.h> // int_max,int_min,long_long_max,long_long_min
using namespace std;

int main()
{
int test;
cin>>test;
while(test--) {
int n;
cin>>n;

int ans = 0;
bool allOne = true;
for(int i=0;i<n;i++) {
int temp;
cin>>temp;
if(temp!=1) allOne = false;
ans = ans ^ temp;
}
string s1,s2;
s1 = "John";
s2 = "Brother";
if(allOne) {
if(n%2==0) {
cout<<s1<<endl;
} else cout<<s2<<endl;
} else {
if (ans == 0) {
cout<<s2<<endl;
} else cout<<s1<<endl;
}
}
return 0;
}
89 changes: 89 additions & 0 deletions Algorithm_Games/TRIOMINO.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm> // binsearch,max(a,b),min(a,b)
#include <math.h>
#include <queue>

#include <vector>
#include <set>
#include <list>
#include <map>
#include <string.h> // memset
#include <cstdlib> // abs(int),labs(int),llabs(int),min,max
#include <limits.h> // int_max,int_min,long_long_max,long_long_min
using namespace std;

int mem[810][2][2][2][2];
int grundy(int n,int leftLower, int leftUpper, int rightLower, int rightUpper) {

// base cases:
//cout << n << " " << leftLower << " " << leftUpper << " " << rightLower << " " << rightUpper << endl;
if(n==1 && !leftLower && !leftUpper && !rightUpper && !rightLower) {
return 0;
}
if(n==0) {
return 0;
}

if (mem[n][leftLower][leftUpper][rightLower][rightUpper]!=-1) {
//cout<<"Already stored"<<endl;
return mem[n][leftLower][leftUpper][rightLower][rightUpper];
}

set<int> reachable;
if(leftUpper || leftLower) {
reachable.insert(grundy(n-1,0,0,rightLower,rightUpper));
}
if(rightUpper || rightLower) {
reachable.insert(grundy(n-1,leftLower,leftUpper,0,0));
}
if (n >= 2) {
reachable.insert(grundy(n-2,0,1,rightLower,rightUpper));
reachable.insert(grundy(n-2,1,0,rightLower,rightUpper));
reachable.insert(grundy(n-2,leftLower,leftUpper,0,1));
reachable.insert(grundy(n-2,leftLower,leftUpper,1,0));
}


for(int i=2;i<=n-1;i++) {
int a,b,c,d;
// right orientations
a = grundy(i-1,leftLower,leftUpper,0,0);
b = grundy(n-i-1,0,1,rightLower,rightUpper);
c = grundy(n-i-1,1,0,rightLower,rightUpper);
reachable.insert(a^c);
reachable.insert(a^b);

// left orientations
a = grundy(n-i,0,0,rightLower,rightUpper);
b = grundy(i-2,leftLower,leftUpper,0,1);
c = grundy(i-2,leftLower,leftUpper,1,0);
reachable.insert(a^c);
reachable.insert(a^b);

}


// calculating mex of the reachable set
int answer = 0;
while(reachable.count(answer)) {
answer++;
}
return mem[n][leftLower][leftUpper][rightLower][rightUpper] = answer;
}
int main() {
int test;
memset(mem,-1,sizeof(mem));
cin>>test;
while(test--) {
int n;
cin>>n;
if(grundy(n,0,0,0,0)) {
cout<<"X"<<endl;
} else {
cout<<"Y"<<endl;
}
}
return 0;
}
30 changes: 30 additions & 0 deletions DP/COINS_MAP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm> // binsearch,max(a,b),min(a,b)
#include <math.h>
#include <queue>

#include <vector>
#include <set>
#include <list>
#include <map>
#include <string.h> // memset
#include <cstdlib> // abs(int),labs(int),llabs(int),min,max
#include <limits.h> // int_max,int_min,long_long_max,long_long_min
using namespace std;
map<long long,long long> m;

long long solve(long long n) {
if(m.count(n)) {
return m[n];
}
return m[n] = max(n,solve(n/2) + solve(n/3) + solve(n/4));
}
int main() {
long long val;
while(cin>>val) {
cout<<solve(val)<<endl;
}
return 0;
}
22 changes: 22 additions & 0 deletions DP/COINS_NAIVE.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# include<stdio.h>
long long mem[1000000];
long long solve(long long a) {
if(a==0) return 0;
if(a<1000000 && mem[a]!=0) return mem[a];
long long ret=solve(a/2) + solve(a/3) + solve(a/4) ;
ret= ret > a ? ret:a;
if(a<1000000) mem[a]=ret;
return ret;

}
int main()
{
long long test;
while(scanf("%lld",&test)==1) {
printf("%lld\n",solve(test));
}
return 0;
}



27 changes: 27 additions & 0 deletions DP/MCOINS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
#include <algorithm>

using namespace std;

int main(){
int K,L,m;
cin >> K >> L >> m;
bool play[1000001]; // to calculate wins of 'A'
int coin;

for(int i = 1;i <= 1000001;++i){
play[i] = !play[i-1];
if(i >= K && !play[i-K]) play[i] = true;
if(i >= L && !play[i-L]) play[i] = true;
}

for(int i = 0;i < m;++i){
cin >> coin;
if(play[coin]) cout << 'A';
else cout << 'B';
}

cout << "\n";

return 0;
}
Loading

0 comments on commit feebb79

Please sign in to comment.