BearPlaysDiv2
problem Statement
Limak is a little bear who loves to play. Today He is playing by moving some stones between three piles of stones. Initially, the piles contain A, B, and C stones, respectively. Limak ' s goal is to produce three equal piles.
Limak would try reaching his goal to performing a sequence of zero or more operations. In each operation he'll start by choosing and unequal piles. Let's label their sizes x and y in such a by that x < Y. He'll then double the size of the smaller chosen pile by moving some stones between the other chosen piles. Formally, the new sizes of the chosen piles would be x+x and y-x.
You were given the ints A, B, and C. Return "Possible" (quotes for clarity) if there was a sequence of operations that would Make all three piles equal. Otherwise, return "impossible".
Definition
Class:bearplaysdiv2
Method:equalpiles
parameters:int, int, int
Returns:string
Method signature:string equalpiles (int A, int B, int C) (Be sure your method was public)
Limits
Time limit (s): 2.000
Memory Limit (MB) 256
Stack Limit (MB): 256
Constraints
A, B and C would be between 1 and inclusive.
Examples
0)
10
15
35
Returns: "Possible"
One valid sequence of operations looks as follows:
The initial pile sizes is ten, and 35.
For the first operation Limak would choose the piles with and stones. After doubling the size of the smaller pile the new sizes of these the piles would be is 20 and.
After the first operation the pile sizes is ten, and 20.
For the second operation Limak would choose the piles with ten and stones. After doubling the size of the smaller pile the new sizes of these and 20.
After the second operation each pile have stones, which means that Limak have reached his goal.
1)
1
1
2
Returns: "Impossible"
No matter what Limak does, there'll always be the one piles with a single stone each and one pile with 2 stones.
2)
4
6
8
Returns: "Impossible"
3)
18
18
18
Returns: "Possible"
Sometimes Limak can reach his goal without making any operations.
4)
225
500
475
Returns: "Possible"
Test instructions
Each operation can make two numbers, large into y-x, small to x+x, (assuming y>x) and then ask if you can make three numbers like
Exercises
Direct BFS Search is good, vis array optimization just fine
Code:
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <queue>using namespaceStd;classBearplaysdiv2{public: int vis[600][600]; structNode {int a[3]; }; String equalpiles (int A, int B, int C) {memset (vis,0,sizeof (VIS)); node kiss; kiss.a[0]=a,kiss.a[1]=b,kiss.a [2]= C; int sum=kiss.a[0]+kiss.a[1]+kiss.a[2 ]; if (sum%3!=0 ) return "impossible" ; sort (KISS.A, Kiss.a+3 ); Vis[kiss.a[0]][kiss.a[1]]=1 ; queue<node> Q; Q.push (Kiss); while (! Q.empty ()) {node now= q.front (); if (NOW.A[0]==SUM/3&&NOW.A[1]==SUM/3 ) return "possible" ; Q.pop (); for (int i=0;i<3;i++ ) {A (int j=i+1;j<3;j++ ) {if (now.a[i]!= now.a[j]) {node next= now; next.a[j]=next.a[j]- next.a[i]; next.a[i]=next.a[i]+ next.a[i]; sort (next.a,next.a+3 ); if (!vis[ Next.a[0]][next.a[1 ]] {vis[next.a[0]][next.a[1]]=1 ; Q.push (next); }}}}} return "impossible" ;}};
TC SRM 664 div2 B BearPlaysDiv2 BFS