TC SRM 664 div2 B BearPlaysDiv2 bfs
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的更多相关文章
- TC SRM 664 div2 A BearCheats 暴力
BearCheats Problem Statement Limak is an old brown bear. Because of his bad eyesight he sometime ...
- topcpder SRM 664 div2 A,B,C BearCheats , BearPlays equalPiles , BearSorts (映射)
A题,熊孩子测视力,水题,题意就是判断一下两个数对应位不相同的数字有多少个. #include<bits/stdc++.h> using namespace std; class Bear ...
- TC SRM 663 div2 B AABB 逆推
AABB Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description One day, Jamie noticed that many En ...
- TC SRM 663 div2 A ChessFloor 暴力
ChessFloor Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description Samantha is renovating a squa ...
- TC SRM 665 DIV2 A LuckyXor 暴力
LuckyXorTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description A lucky number is a positive int ...
- TC SRM 593 DIV2 1000
很棒的DP,不过没想出,看题解了..思维很重要. #include <iostream> #include <cstdio> #include <cstring> ...
- TC SRM 591 DIV2 1000
很不错的一题,非常巧妙的用DP顺序解决这个问题... 可以发现,只和A里面最小的有关系... #include <cstdio> #include <cstring> #inc ...
- tc srm 636 div2 500
100的数据直接暴力就行,想多了... ac的代码: #include <iostream> #include <cstdio> #include <cstring> ...
- TC SRM 665 DIV2 B LuckyCycle 暴力
LuckyCycleTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.ac ...
随机推荐
- (1)java设计模式之简单工厂模式
一:简单工厂模式的优点 --->在阎宏博士的<JAVA与模式>一书中开头是这样描述简单工厂模式的:简单工厂模式是类的创建模式,又叫做静态工厂方法(Static Fa ...
- ECshop 二次开发模板教程3
<p>商品列表</p> <table width="70%" border="1"> <tr> <td&g ...
- tcprstat的使用方式
两种使用方式:1)本机直接在线采集:2)分析tcpdump采集到的离线pcap文件 1. 本机直接在线采集 参数: -p :指定只采集此TCP port的请求 -t : 采集输出的时间间 ...
- hdu1722 bjfu1258 辗转相除法
这题就是个公式,代码极简单.但我想,真正明白这题原理的人并不多.很多人只是随便网上一搜,找到公式a了就行,其实这样对自己几乎没有提高. 鉴于网上关于这题的解题报告中几乎没有讲解原理的,我就多说几句,也 ...
- Mahout分步式程序开发 聚类Kmeans(转)
Posted: Oct 14, 2013 Tags: clusterHadoopkmeansMahoutR聚类 Comments: 13 Comments Mahout分步式程序开发 聚类Kmeans ...
- C语言char[]和char*比较
先看看一个例子: #include <iostream> using namespace std; main() { char *c1 = "abc"; char c2 ...
- Spark编程实现SQL查询的实例
1.Oracle中的SQL select count(1) from a_V_PWYZL_CUSTACCT_PSMIS t where not exists (select 1 from tb_sho ...
- Orchard源码分析(1):插件式的支持——模块和主题
在Orchard,模块和主题都是可以插拔式的,在源码处理时,用类型(参考:DefaultExtensionTypes)区分,都没太大的本质区别,以下都称做模块. 插件的支持,实现分以下几步: 搜集模块 ...
- pku3668 Game of Lines
http://poj.org/problem?id=3668 水题,STL #include <stdio.h> #include <set> using namespace ...
- 第二百五十一天 how can I 坚持
hadoop,namenote和datanode.namenode如果要是在启动时加载到内存,会不会对内存的要求比较高呢. edits-->fsimage. secondnamenode,那么n ...