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. <一>SQL优化1-4

    第一条:去除在谓词列上编写的任何标量函数        --->在select 显示列上使用标量函数是可以的.但在where语句后的过滤条件部分对列使用函数,需要考虑.因为执行sql的引擎会因为 ...

  2. c# 利用反射动态给实体类对象赋值

    转:http://blog.sina.com.cn/s/blog_659a572b0100xp5s.html 例子如下 using System; using System.Collections.G ...

  3. 编译boost (windows msvc14)

    我的环境 OS: WIN10 (x64) IDE: VS2015 (VC14) http://www.boost.org/ 1. 下载 下载boost包, boost_1_62_0.7z 使用ASIO ...

  4. hdu 2825(ac自动机+状态压缩dp)

    题意:容易理解... 分析:在做这道题之前我做了hdu 4057,都是同一种类型的题,因为题中给的模式串的个数最多只能为10个,所以我们就很容易想到用状态压缩来做,但是开始的时候我的代码超时了dp时我 ...

  5. Visual Studio 2010中创建ASP.Net Web Service

    转自:http://blog.csdn.net/xinyaping/article/details/7331375 很多人在论坛里说,在Visual Studio 2010中不能创建“ASP.Net ...

  6. codedorces 260 div2 A题

    水题,扫描一遍看是否出现价格低质量高的情况. #include<cstdio> #include<string> #include<vector> #include ...

  7. C/C++:类模板

    类模板就是为类声明一种模板,使得类中的某些数据成员,或某些成员函数的参数,又或者是某些成员函数的返回值可以取任意的数据类型,包括基本数据类型和自定义数据类型. 类模板的声明形式如下: template ...

  8. jQuery Mobile入门教程

    简介:jQuery Mobile框架可以轻松的帮助我们实现非常好看的.可跨设备的Web应用程序.我们将后续的介绍中向大家介绍大量的代码及实例. jQuery一直以来都是非常流行的富客户端及Web应用程 ...

  9. UML 学习

    推荐书籍:<面向对象分析与设计(第3版)>.<UML精粹:标准对象建模语言简明指南(第3版)> 推荐一: http://amateras.sourceforge.jp/cgi- ...

  10. B树在数据库索引中的应用剖析

    引言 关于数据库索引,google一个oracle index,mysql index总 有大量的结果,其中很多的使用方法推荐,**索引之n条经典建议云云.笔者认为,较之借鉴,在搞清楚了自己的需求的基 ...