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. 【Unity3D】Unity自带组件—完成第一人称人物控制

    1.导入unity自带的Character Controllers包 2.可以看到First Person Controller组件的构成 Mouse Look() : 随鼠标的移动而使所属物体发生旋 ...

  2. Android设计模式之命令模式、策略模式、模板方法模式

    命令模式是其它很多行为型模式的基础模式.策略模式是命令模式的一个特例,而策略模式又和模板方法模式都是算法替换的实现,只不过替换的方式不同.下面来谈谈这三个模式. 命令模式 将一个请求封装为一个对象,从 ...

  3. 一步一步ITextSharp 低级操作函数使用

    首先说一下PDF文档的结构: 分为四层,第一层和第四层由低级操作来进行操作,第二层.第三层由高级对象操作 第一层操作只能使用PdfWriter.DirectContent操作,第四层使用DirectC ...

  4. android moveTaskToback 应用退到后台,类似最小化

    方法:public boolean moveTaskToBack(boolean nonRoot) activity里有这个方法,参数说明如下: nonRoot=false→ 仅当activity为t ...

  5. HDU 5878 I Count Two Three

    I Count Two Three Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. CSAPP(1):数字的计算机表示——课后题

    2.65 int even_ones(unsigned x) 要求:return 1 when x contains an even number of 1s; 0 otherwise. 假设int ...

  7. HDU 5311

    把anniversary分成三个区间,分别枚举每个区间在给定模板中的长度.每次枚举完一个区间,记录下区间长度和起始坐标,下次从剩下长度开始枚举,避免重复. #include<iostream&g ...

  8. SQL中以count及sum为条件的查询

    在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7   例1:查询出现 ...

  9. 基础排序算法,java实现(快速,冒泡,选择,堆排序,插入)

    1.冒泡排序: (1)比较相邻的元素.如果第一个比第二个大,就交换他们两个. (2)外面再套个循环就行. 算法复杂度:O(N2)   不罗嗦,上代码: //冒泡排序(两两交换,外加一个外循环) pub ...

  10. 从零教你如何获取hadoop2.4源码并使用eclipse关联hadoop2.4源码

    从零教你如何获取hadoop2.4源码并使用eclipse关联hadoop2.4源码http://www.aboutyun.com/thread-8211-1-1.html(出处: about云开发) ...