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";
        }
    };
  • 相关阅读:
    2017年5月15号课堂笔记
    2017年5月12号课堂笔记
    2017年5月8号课堂笔记
    2017年5月5号课堂笔记
    2017年4月26号课堂笔记
    不忘初心,坚持走下去
    2017年4月24号课堂笔记
    2017年4月21号课堂笔记
    2017年4月19号课堂笔记
    autoit UIA获取Listview的信息
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4695007.html
Copyright © 2011-2022 走看看