博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TC SRM 664 div2 B BearPlaysDiv2 bfs
阅读量:6120 次
发布时间:2019-06-21

本文共 3033 字,大约阅读时间需要 10 分钟。

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
#include
#include
#include
#include
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
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"; } };

转载地址:http://lalka.baihongyu.com/

你可能感兴趣的文章
【Git入门之四】操作项目
查看>>
老男孩教育每日一题-第107天-简述你对***的理解,常见的有哪几种?
查看>>
Python学习--time
查看>>
在OSCHINA上的第一篇博文,以后好好学习吧
查看>>
高利率时代的结局,任重道远,前途叵测
查看>>
Debian 6.05安装后乱码
查看>>
欢迎大家观看本人录制的51CTO精彩视频课程!
查看>>
IntelliJ IDEA中设置忽略@param注释中的参数与方法中的参数列表不一致的检查
查看>>
关于软件开发的一些感悟
查看>>
uva 10806
查看>>
纯CSS3绘制的黑色图标按钮组合
查看>>
Linux中环境变量文件及配置
查看>>
从0开始学Flutter
查看>>
mysql操作入门基础之对数据库和表的增删改查
查看>>
IIS负载均衡
查看>>
分布式事务,EventBus 解决方案:CAP【中文文档】
查看>>
Linux下的CPU性能瓶颈分析案例
查看>>
spring mvc入门
查看>>
2012在数据库技术会议上的讲话PPT打包
查看>>
【Android】 TextView设置个别字体样式
查看>>