zoukankan      html  css  js  c++  java
  • 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";
        }
    };
  • 相关阅读:
    《Android 4游戏高级编程(第2版)》书评
    push研究——Apache Mina探索初步
    Android UI开发第二十三篇——分享书架UI实现
    cookie学习总结
    Web.xml配置详解
    Java序列化的机制和原理
    Java高级技术(汇总中...)
    [Java]HashMap的两种排序方式
    jdk与jre的区别
    DM,NLP常用算法汇总
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4695007.html
Copyright © 2011-2022 走看看