zoukankan      html  css  js  c++  java
  • P1213 时钟

    题目描述

    考虑将如此安排在一个 3 x 3 行列中的九个时钟:

    目标要找一个最小的移动顺序将所有的指针指向12点。下面原表格列出了9种不同的旋转指针的方法,每一种方法都叫一次移动。选择1到9号移动方法,将会使在表格中对应的时钟的指针顺时针旋转90度。

    移动方法 受影响的时钟

    1 ABDE

    2 ABC

    3 BCEF

    4 ADG

    5 BDEFH

    6 CFI

    7 DEGH

    8 GHI

    9 EFHI

    Example

    [但这可能不是正确的方法,请看下面]

    输入输出格式

    输入格式:

    第1-3行: 三个空格分开的数字,每个数字表示一个时钟的初始时间,3,6,9,12。数字的含意和上面第一个例子一样。

    输出格式:

    单独的一行包括一个用空格分开的将所有指针指向12:00的最短移动顺序的列表。

    如果有多种方案,输出那种使其连接起来数字最小的方案。(举例来说5 2 4 6 < 9 3 1 1)。

    输入输出样例

    输入样例#1: 复制
    9 9 12
    6 6 6
    6 3 6 
    
    输出样例#1: 复制
    4 5 8 9
    

    说明

    题目翻译来自NOCOW。

    USACO Training Section 1.4

    // luogu-judger-enable-o2
    //九维数组暴力判啊
    //hash写的太渣
    
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<map>
    #include<queue>
    #include<algorithm>
    using namespace std;
    
    const int N=500000000;
    const int mod=10000007;
    
    string move[15]={"0","0134","012","1245","036","13457","258","3467","678","4578"};
    int len[15]={0,4,3,4,3,5,3,4,3,4};
    struct CLO
    {
        int id;
        string s;
        int pre;
        string way;
        int cnt;
    }clo[500005];
    int num_clo;
    string ans[100005];
    int len_ans,sum;
    
    bool operator < (string a,string b)
    {
        return a+b<b+a;
    }
    
    bool h[N];
    bool hash(string a)
    {
        int tmp=0;
        for(int i=0;i<9;++i)
        {
            tmp=tmp*10+a[i]-'0';
        }
        if(h[tmp])
            return 1;
        h[tmp]=1;
        return 0;
    }
    
    void bfs()
    {
        queue<CLO> que;
        que.push(clo[0]);
        hash(clo[0].s);
        CLO now,tmp;
        len_ans=N;
        while(!que.empty())
        {
            now=que.front(),que.pop();
    //        cout<<now.s<<endl;
            if(now.cnt>=len_ans)
                break;
            for(int i=1;i<=9;++i)
            {
    //            cout<<"i: "<<i<<endl;
                tmp.s=now.s;
                for(int j=0;j<len[i];++j)
                {
                    ++tmp.s[move[i][j]-'0'];
                    if(tmp.s[move[i][j]-'0']=='5')
                        tmp.s[move[i][j]-'0']='1';
                }
    //            cout<<"tmp.s: "<<tmp.s<<endl;
                if(tmp.s=="444444444")
                {
                    ans[++sum]=now.way;
                    ans[sum]+=i+'0';
                    len_ans=min(len_ans,now.cnt+1);
                    continue;
                }
                if(hash(tmp.s))
                    continue;
                tmp.cnt=now.cnt+1;
                tmp.id=++num_clo;
                tmp.way=now.way;
                tmp.way+=i+'0';
                tmp.pre=now.id;
                clo[num_clo]=tmp;
                que.push(tmp);
    //            cout<<"num_clo: "<<num_clo<<endl;
            }
        }
    }
    
    int main()
    {
    //    freopen("testdata.in","r",stdin);
        for(int i=1,a;i<=9;++i)
        {
            scanf("%d",&a);
            clo[0].s+=a/3+'0';
        }
        bfs();
        sort(ans+1,ans+sum+1);
    //    for(int i=1;i<=sum;++i)
    //        cout<<ans[i]<<endl;
    //    cout<<sum;
        for(int i=0;i<ans[1].length();++i)
            printf("%c ",ans[1][i]);
        return 0;
    }

  • 相关阅读:
    2016"百度之星"
    codeforces 55 div2 C.Title 模拟
    codeforces 98 div2 C.History 水题
    codeforces 97 div2 C.Replacement 水题
    codeforces 200 div2 C. Rational Resistance 思路题
    bzoj 2226 LCMSum 欧拉函数
    hdu 1163 九余数定理
    51nod 1225 余数的和 数学
    bzoj 2818 gcd 线性欧拉函数
    Codeforces Round #332 (Div. 2)D. Spongebob and Squares 数学
  • 原文地址:https://www.cnblogs.com/lovewhy/p/8665987.html
Copyright © 2011-2022 走看看