TC SRM 664 div2 B BearPlaysDiv2 BFS

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.