• <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個機(jī)器用2個號碼牌來生成一個新號碼牌(兩個輸入口可以放一個號碼牌并且不消耗號碼牌),問最少使用的步數(shù)達(dá)到新號碼(或者達(dá)到最優(yōu)號碼,N多關(guān)鍵字排序規(guī)則,一看就知道搜索)。


            解法&代碼
            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 閱讀(254) 評論(0)  編輯 收藏 引用 所屬分類: search

            <2010年10月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            導(dǎo)航

            統(tǒng)計

            公告

            統(tǒng)計系統(tǒng)

            留言簿(1)

            隨筆分類(227)

            文章分類(2)

            OJ

            最新隨筆

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            久久精品毛片免费观看| 久久综合久久美利坚合众国| 一级做a爰片久久毛片毛片| 久久精品国产一区| 国产精品一区二区久久精品| 久久久久久久久久久久中文字幕| 久久久久亚洲AV成人网人人网站 | 人妻无码中文久久久久专区| 欧美久久久久久精选9999| 久久99精品国产麻豆婷婷| 久久精品国产色蜜蜜麻豆| 久久久久亚洲精品天堂久久久久久| 久久精品国产欧美日韩| 久久亚洲高清综合| 亚洲精品无码久久一线| 99国产精品久久| 久久久这里有精品中文字幕| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久99精品久久久久久动态图 | 国产精品熟女福利久久AV | 天天综合久久一二三区| 久久久久亚洲精品日久生情| 久久天天躁狠狠躁夜夜网站 | 久久久久97国产精华液好用吗| 日日狠狠久久偷偷色综合0| 久久久无码精品亚洲日韩蜜臀浪潮| 久久久久亚洲av无码专区导航 | 久久久久久av无码免费看大片| 老司机午夜网站国内精品久久久久久久久 | 99久久国产综合精品五月天喷水 | 亚洲狠狠久久综合一区77777| 精品无码久久久久久久久久 | 色欲av伊人久久大香线蕉影院| 久久香蕉国产线看观看99| 久久这里有精品| 99久久精品国产一区二区| 亚洲午夜久久久久妓女影院 | 久久精品国产99国产精品澳门 | 午夜不卡888久久| 亚洲国产精品久久久天堂| 久久中文字幕视频、最近更新 |