• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            pku2133 Cow Imposters BFS

            題意:
            FJ給每個牛一個號碼牌,牛不喜歡這個號碼牌,就私自建立了1個機器用2個號碼牌來生成一個新號碼牌(兩個輸入口可以放一個號碼牌并且不消耗號碼牌),問最少使用的步數達到新號碼(或者達到最優號碼,N多關鍵字排序規則,一看就知道搜索)。


            解法&代碼
            BFS,直接看代碼吧。。
             1 Source Code
             2 
             3 Problem: 2133        User: yzhw
             4 Memory: 660K        Time: 0MS
             5 Language: C++        Result: Accepted
             6 Source Code
             7 # include <cstdio>
             8 # include <cstring>
             9 # include <stack>
            10 # include <cstdlib>
            11 using namespace std;
            12 int used[1<<16];
            13 int q[1<<16],s=0,e=-1;
            14 int toInt(char *str)
            15 {
            16     int res=0;
            17     for(int i=0;str[i]!='\0';i++)
            18       res=res*2+str[i]-48;
            19     return res;
            20 }
            21 
            22 void print(int top,int b)
            23 {
            24          stack<int> ans;
            25           for(int i=0;i<b;i++)
            26           {
            27             ans.push(top%2);
            28             top/=2;
            29           }
            30           while(!ans.empty())
            31           {
            32             printf("%d",ans.top());
            33             ans.pop();
            34           }
            35           printf("\n");
            36 }
            37 int diff(int target,int code,int b)
            38 {
            39     int ans=0;
            40     for(int i=0;i<b;i++)
            41     {
            42        ans+=(target%2!=code%2);
            43        target/=2;
            44        code/=2;
            45     }
            46     return ans;
            47 }
            48 int main()
            49 {
            50    // freopen("ans.txt","w",stdout);
            51    // int pre[1<<16][2];
            52     memset(used,0,sizeof(used));
            53     int b,ee;
            54     scanf("%d%d",&b,&ee);
            55     char str[20];
            56     scanf("%s",str);
            57     int target=toInt(str);
            58     for(int i=0;i<ee;i++)
            59     {
            60        scanf("%s",str);
            61        q[++e]=toInt(str);
            62     }
            63     while(s<=e)
            64     {
            65        int top=q[s++];
            66        //print(top,b);
            67       // printf("%d %d %d %d\n",top,pre[top][0],pre[top][1],used[top]);
            68        //system("pause");
            69        if(top==target&&used[top])
            70        {
            71           printf("%d\n",used[top]);
            72           print(top,b);
            73           //system("pause");
            74           return 0;
            75        }
            76        for(int i=0;i<ee;i++)
            77          if(!used[top^q[i]])
            78            used[top^q[i]]=used[top]+1,q[++e]=(top^q[i]);
            79     }
            80     int bestcode,bestdiff=0xfffffff,beststep;
            81     for(int i=0;i<(1<<b);i++)
            82       if(used[i]&&(diff(i,target,b)<bestdiff||diff(i,target,b)==bestdiff&&used[i]<beststep||diff(i,target,b)==bestdiff&&used[i]==beststep&&i<bestcode))
            83          bestcode=i,bestdiff=diff(i,target,b),beststep=used[i];
            84     printf("%d\n",beststep);
            85     print(bestcode,b);
            86     //system("pause");
            87     return 0;
            88     
            89 }

            posted on 2011-03-13 02:27 yzhw 閱讀(265) 評論(0)  編輯 收藏 引用 所屬分類: search

            <2011年1月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345

            導航

            統計

            公告

            統計系統

            留言簿(1)

            隨筆分類(227)

            文章分類(2)

            OJ

            最新隨筆

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            伊人色综合久久天天网| 久久免费的精品国产V∧| 久久久久久极精品久久久| 精品久久久久久国产91| 婷婷久久综合九色综合98| 99久久综合狠狠综合久久| 久久精品综合网| 国产成人精品久久一区二区三区| 久久男人中文字幕资源站| 久久精品成人欧美大片| 香蕉久久夜色精品国产小说| 久久久SS麻豆欧美国产日韩| 成人妇女免费播放久久久| 无码人妻久久一区二区三区蜜桃| 老司机国内精品久久久久| 伊人久久国产免费观看视频| 久久久久久亚洲精品无码| 久久亚洲AV成人无码电影| 无码乱码观看精品久久| 99久久精品国产毛片| av国内精品久久久久影院| 久久精品国产免费观看三人同眠| 亚洲精品成人网久久久久久| 91亚洲国产成人久久精品| 99精品国产在热久久| 精品熟女少妇av免费久久| 久久久久久亚洲Av无码精品专口| 99精品国产99久久久久久97 | 久久青青草原精品国产| 日本精品久久久久影院日本| 国产精品成人久久久久三级午夜电影| 99久久久精品| 久久无码av三级| 久久精品国产福利国产秒| 国产精品久久久久乳精品爆| 成人资源影音先锋久久资源网| 国产aⅴ激情无码久久| 久久中文字幕人妻丝袜| 久久成人国产精品| 潮喷大喷水系列无码久久精品| 亚洲午夜久久久精品影院|