Tuesday 24 October 2017

Stone Game : Problem 260: Project Euler Solution in C

Problem: https://projecteuler.net/problem=260
A game is played with three piles of stones and two players.
At her turn, a player removes one or more stones from the piles. However, if she takes stones from more than one pile, she must remove the same number of stones from each of the selected piles.
In other words, the player chooses some N>0 and removes:
  • N stones from any single pile; or
  • N stones from each of any two piles (2N total); or
  • N stones from each of the three piles (3N total).
The player taking the last stone(s) wins the game.
winning configuration is one where the first player can force a win.
For example, (0,0,13), (0,11,11) and (5,5,5) are winning configurations because the first player can immediately remove all stones.
losing configuration is one where the second player can force a win, no matter what the first player does.
For example, (0,1,2) and (1,3,3) are losing configurations: any legal move leaves a winning configuration for the second player.
Consider all losing configurations (xi,yi,zi) where xi ≤ yi ≤ zi ≤ 100.
We can verify that Σ(xi+yi+zi) = 173895 for these.
Find Σ(xi+yi+zi) where (xi,yi,zi) ranges over the losing configurations
with xi ≤ yi ≤ zi ≤ 1000.

Solution: copied from euler.stephan-brumme.com/260/


#include <stdio.h>
int maxPileSize = 1000;

int id(int a, int b)
{
                return a*(maxPileSize+1)+b;
}
main()
{
                printf("enter maximum pile size");
                scanf("%d", &maxPileSize);
                int won=0, lost=1,s=(maxPileSize+1)*(maxPileSize+1),i;
                int one[s];
                int two[s];
                int all[s];
                for(i=0;i<s;i++)
                {
                                one[i]=0;
                                two[i]=0;
                                all[i]=0;
                }
               
                int count=0,x,y,z;
                for(x=0;x<=maxPileSize;x++)
                {
                                for(y=x;y<=maxPileSize;y++)
                                {
                                                if(one[id(x,y)] == 1)
                                                                continue;
                                                for(z=y;z<=maxPileSize;z++)
                                                {
                                                                if(one[id(y,z)] == 1 || one[id(x,z)] == 1||one[id(x,y)] == 1)
                                                                                continue;
                                                              if(two[id(y-x,z)] == 1||two[id(z-y,x)] == 1||two[id(z-x,y)] == 1)
                                                                                continue;
                                                                if(all[id(y-x,z-x)] == 1)
                                                                                continue;
                                                                count=count+x+y+z;
                                                                one[id(y,z)] == 1;
                                                                one[id(x,z)] == 1;
                                                                one[id(x,y)] == 1;
                                                                two[id(y-x,z)] == 1;
                                                                two[id(z-y,x)] == 1;
                                                                two[id(z-x,y)] == 1;
                                                                all[id(y-x,z-x)]==1;
                                                }
                                }
                }                             
printf("Number of loosing configurations=%d",count);
}


 Output:


No comments:

Post a Comment