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 will try reaching his goal by performing a sequence of zero or more operations. In each operation he will start by choosing two unequal piles. Let's label their sizes X and Y in such a way that X < Y. He will then double the size of the smaller chosen pile by moving some stones between the two chosen piles. Formally, the new sizes of the two chosen piles will be X+X and Y-X.

You are given the ints A, B, and C. Return "possible" (quotes for clarity) if there is a sequence of operations that will 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 is public)
Limits  
Time limit (s):2.000
Memory limit (MB)256
Stack limit (MB):256
Constraints
A, B and C will be between 1 and 500, inclusive.
Examples
0)
10
15
35
Returns: "possible"
One valid sequence of operations looks as follows:
The initial pile sizes are 10, 15, and 35.
For the first operation Limak will choose the piles with 15 and 35 stones. After doubling the size of the smaller pile the new sizes of these two piles will be 30 and 20.
After the first operation the pile sizes are 10, 30, and 20.
For the second operation Limak will choose the piles with 10 and 30 stones. After doubling the size of the smaller pile the new sizes of these two piles will be 20 and 20.
After the second operation each pile has 20 stones, which means that Limak has reached his goal.
1)
1
1
2
Returns: "impossible"
No matter what Limak does, there will always be two 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"

题意:

每次操作可以使得两个数中,大的变成y-x,小的变成x+x,(假设y>x)然后问你能不能使得三个数一样

题解:

直接bfs搜就好了,vis数组优化一下就好

代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std; class BearPlaysDiv2{
public:
int vis[600][600];
struct node
{
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++)
{
for(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的更多相关文章

  1. TC SRM 664 div2 A BearCheats 暴力

     BearCheats Problem Statement    Limak is an old brown bear. Because of his bad eyesight he sometime ...

  2. topcpder SRM 664 div2 A,B,C BearCheats , BearPlays equalPiles , BearSorts (映射)

    A题,熊孩子测视力,水题,题意就是判断一下两个数对应位不相同的数字有多少个. #include<bits/stdc++.h> using namespace std; class Bear ...

  3. TC SRM 663 div2 B AABB 逆推

    AABB Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description One day, Jamie noticed that many En ...

  4. TC SRM 663 div2 A ChessFloor 暴力

    ChessFloor Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description Samantha is renovating a squa ...

  5. TC SRM 665 DIV2 A LuckyXor 暴力

    LuckyXorTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description A lucky number is a positive int ...

  6. TC SRM 593 DIV2 1000

    很棒的DP,不过没想出,看题解了..思维很重要. #include <iostream> #include <cstdio> #include <cstring> ...

  7. TC SRM 591 DIV2 1000

    很不错的一题,非常巧妙的用DP顺序解决这个问题... 可以发现,只和A里面最小的有关系... #include <cstdio> #include <cstring> #inc ...

  8. tc srm 636 div2 500

    100的数据直接暴力就行,想多了... ac的代码: #include <iostream> #include <cstdio> #include <cstring> ...

  9. TC SRM 665 DIV2 B LuckyCycle 暴力

    LuckyCycleTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.ac ...

随机推荐

  1. Hibernate管理Session和批量操作

    Hibernate管理Session Hibernate自身提供了三种管理Session对象的方法 Session对象的生命周期与本地线程绑定 Session对象的生命周期与JTA事务绑定 Hiber ...

  2. [Papers]MHD, $\pi$, Lorentz space [Suzuki, DCDSA, 2011]

    $$\bex \sen{\pi}_{L^{s,\infty}(0,T;L^{q,\infty}(\bbR^3))} +\sen{{\bf b}}_{L^{\gamma,\infty}(0,T;L^{\ ...

  3. Redis常用命令手册:服务器相关命令

    Redis提供了丰富的命令(command)对数据库和各种数据类型进行操作,这些command可以在Linux终端使用.在编程时,比如各类语言包,这些命令都有对应的方法.下面将Redis提供的命令做一 ...

  4. Selenium IDE 测试

    Selenium IDE 测试 调试是为了发现和修复测试脚本,任何脚本开发的共同步骤是错误的处理.为了使这一过程更加稳固,我们可以使用Selenium IDE的一个插件叫“Power Debugger ...

  5. JDBC中DAO事务函数模版

    DAO事物函数模版1: public void OrderFinsByPage(){ Connection conn = null; PreparedStatement pstmt = null; R ...

  6. Javascript手记-执行环境和作用域

    执行环境是javascript一个重要的概念,执行环境定义了变量有权访问其他数据决定了他们各自的行为,每个执行环境 都有一个与之关联的变量,环境中定义的所有变量和函数都保存在这个对象中,虽然我们编写的 ...

  7. windows下安装和配置Weka

    Weka是一款免费的,非商业化的,基于java环境下的开源的机器学习以及数据挖掘软件.Weka里含有各种数据挖掘工具:数据预处理,分类与回归,聚类,关联规则和可视化工具. 一.安装weka 我们首先需 ...

  8. Delphi开发ocx插件的调试

    Delphi开发ocx苦于调试,网上看了下大概配置: IE调用ocx调试配置,在当前ocx工程  run-->parameters-->host application 里面配置IE的程序 ...

  9. Shell中的变量

    一.什么是变量 变量在 bash 环境中是非常重要的,简单的说,就是让某一个特定字符串来代表不固定的内容.举例: 那就是:『 y = ax + b 』这东西,在等号左边的(y)就是变量,在等号右边的( ...

  10. C++问题-无法打开包括文件:“GLES2/gl2.h”

    资料来源:http://blog.csdn.net/weizehua/article/details/12623719http://tieba.baidu.com/p/2747715029 问题经过: ...