zoukankan      html  css  js  c++  java
  • HDOJ 3220 Alice’s Cube

    反向BFS+二进制记录
    Alice’s Cube

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1393    Accepted Submission(s): 407


    Problem Description
    HDOJ 3220 Alice’s Cube - qhn999 - 码代码的猿猿

    Alice has received a hypercube toy as her birthday present. This hypercube has 16 vertices, numbered from 1 to 16, as illustrated below. On every vertex, there is a light bulb that can be turned on or off. Initially, eight of the light bulbs are turned on and the other eight are turned off. You are allowed to switch the states of two adjacent light bulbs with different states (“on” to “off”, and “off” to “on”; specifically, swap their states) in one operation.

    Given the initial state of the lights, your task is to calculate the minimum number of steps needed to achieve the target state, in which the light bulbs on the sub cube (1,2,3,4)-(5,6,7,8) are turned off, and the rest of them are turned on.
     

    Input
    There are multiple test cases. The first line of the input contains an integer T, meaning the number of the test cases. There are about 13000 test cases in total.
    For each test case there are 16 numbers in a single line, the i-th number is 1 meaning the light of the i-th vertex on the picture is on, and otherwise it’s off.
     

    Output
    For every test cases output a number with case number meaning the minimum steps needed to achieve the goal. If the number is larger than 3, you should output “more”.
     

    Sample Input
    30 0 0 0 0 0 0 0 1 1 1 1 1 1 1 10 1 0 0 0 0 0 0 1 0 1 1 1 1 1 10 0 0 0 0 0 1 0 1 0 1 1 1 1 1 1
     

    Sample Output
    Case #1: 0Case #2: 1Case #3: more
     

    Source
     

    Recommend
    zhuweicong
     


    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <queue>

    using namespace std;

    int vis[66000];

    char st[16]={'0','0','0','0','0','0','0','0','1','1','1','1','1','1','1','1'};

    int a[32]={1,3,2,1,10,2, 4, 1,3, 9, 9, 11,  6,14,6, 8, 5,13,5, 7, 13,15,5,7,  2,4,12,10,3,1,11,9};
    int b[32]={2,4,4,3,12,10,12,9,11,11,10,12,  8,16,14,16,7,15,13,15,14,16,6,8,  6,8,16,14,7,5,15,13};

    int bi(char *str)
    {
        int sum=0;
        for(int i=0;i<16;i++)
        {
            if(str=='1')
            {
                sum+=pow(2.,(double)(15-i));
            }
        }
        return sum;
    }

    void bfs(int k)
    {
        queue<int> q;
        q.push(k);
        while(!q.empty())
        {
            int st=q.front();
            q.pop();

            if(vis[st]>=3)  continue;

            for(int i=0;i<32;i++)
            {
                int j1=st&(1<<(a-1));
                int j2=st&(1<<(b-1));

                if(j1==j2)  continue;

                int temp=st;

                temp=temp^(1<<(a-1));
                temp=temp^(1<<(b-1));

                if(vis[temp]==-1)
                {
                    vis[temp]=vis[st]+1;
                    if(vis[temp]<3)  q.push(temp);
                }
            }
        }
    }

    int main()
    {
        memset(vis,-1,sizeof(vis));
        int k=bi(st);
        vis[k]=0;
        bfs(k);
    int n;
    cin>>n;
    for(int u=0;u<n;u++)
    {
        char str[16];
        for(int i=0;i<16;i++)
            cin>>str;
        int l=bi(str);

        int tot=vis[l];
        if(tot!=-1)
        {
            cout<<"Case #"<<u+1<<": "<<tot<<endl;
        }
        else
            cout<<"Case #"<<u+1<<": more"<<endl;
    }
        return 0;
    }


  • 相关阅读:
    方式方法和思维技巧集合
    【NOIP/CSP2019】D2T1 Emiya 家今天的饭
    【NOIP/CSP2019】D1T2 括号树
    【网络流24】餐巾
    【NOIP2016】愤怒的小鸟
    结论和典例集合
    2020牛客寒假集训营第一场题解
    新生训练赛001题解
    The 2014 ACM-ICPC Asia Regional Contest Xi'an Site题解
    SDNU ACM-ICPC 2019 Competition For the End of Term(12-15)山师停训赛题解
  • 原文地址:https://www.cnblogs.com/CKboss/p/3351094.html
Copyright © 2011-2022 走看看