• <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>
            posts - 33,  comments - 25,  trackbacks - 0
            本題與POJ 1753非常相似,因此直接提交以下代碼:
              1 #include <iostream>
              2 #include <vector>
              3 #include <queue>
              4 using namespace std;
              5 
              6 
              7 const int ROW = 4;
              8 const int SIZE = ROW * ROW;
              9 const int START_ACTION = -2;
             10 const int MAX_STATE = 1 << SIZE; // 2 ^ 16
             11 const int OPEN = 65535;
             12 
             13 int ConvertCharToInt(char c)
             14 {
             15     switch(c)
             16     {
             17     case '+':
             18         return 0;
             19     case '-':
             20         return 1;
             21     }
             22     return -1;
             23 }
             24 
             25 int ChangeState(int state_id, int position)
             26 {
             27     state_id ^= (1 << position);
             28 
             29     int copy_position = position;
             30 
             31     // left-row
             32     int left_end = position / ROW * ROW;
             33     while(--copy_position >= left_end && copy_position >= 0)
             34     {
             35         state_id ^= (1 << copy_position);
             36     }
             37     copy_position = position;
             38 
             39     // right-row
             40     int right_end = (position / ROW + 1* ROW;
             41     while(++copy_position < right_end)
             42     {
             43         state_id ^= (1 << copy_position);
             44     }
             45     copy_position = position;
             46 
             47     // up-col
             48     int up_end = 0;
             49     while((copy_position -= ROW) >= up_end)
             50     {
             51         state_id ^= (1 << copy_position);
             52     }
             53     copy_position = position;
             54 
             55     // down-col
             56     int down_end = SIZE;
             57     while((copy_position += ROW) < SIZE)
             58     {
             59         state_id ^= (1 << copy_position);
             60     }
             61     return state_id;
             62 }
             63 
             64 int _tmain(int argc, _TCHAR* argv[])
             65 {
             66     int action_position[MAX_STATE];
             67     int last_action[MAX_STATE];
             68     int time[MAX_STATE];
             69 
             70     memset(action_position, -1sizeof(action_position));
             71     memset(last_action, -1sizeof(last_action));
             72     memset(time, -1sizeof(time));
             73 
             74     queue<int> search_query;
             75 
             76     char state;
             77     int current_state_id = 0;
             78 
             79     for(int i = 0; i < SIZE; ++i)
             80     {
             81         cin >> state;
             82         current_state_id += ConvertCharToInt(state) << i;
             83     }
             84 
             85     if(current_state_id == OPEN)
             86     {
             87         cout << "0" << endl;
             88         return 0;
             89     }
             90 
             91     action_position[current_state_id] = START_ACTION;
             92     last_action[current_state_id] = START_ACTION;
             93     time[current_state_id] = 0;
             94 
             95     search_query.push(current_state_id);
             96     int next_state_id;
             97 
             98     while(!search_query.empty())
             99     {
            100         current_state_id = search_query.front();
            101         search_query.pop();
            102 
            103         for(int i = 0; i < SIZE; ++i)
            104         {
            105             next_state_id = ChangeState(current_state_id, i);
            106 
            107             if(next_state_id == OPEN)
            108             {
            109                 cout << time[current_state_id] + 1 << endl;
            110                 int result = current_state_id;
            111                 vector<int> position;
            112                 position.push_back(i);
            113                 while(action_position[result] != START_ACTION)
            114                 {
            115                     position.push_back(action_position[result]);
            116                     result = last_action[result];
            117                 }
            118                 while(!position.empty())
            119                 {
            120                     cout << ((position.back() >> 2+ 1<< " " << (position.back() % 4 + 1<< endl;
            121                     position.pop_back();
            122                 }
            123                 return 0;
            124             }
            125 
            126             if(time[next_state_id] == -1 /* not visited */)
            127             {
            128                 action_position[next_state_id] = i;
            129                 last_action[next_state_id] = current_state_id;
            130                 time[next_state_id] = time[current_state_id] + 1;
            131                 search_query.push(next_state_id);
            132             }
            133         }
            134     }
            135     return 0;
            136 }
            結(jié)果TLE,至今不知道為什么,兩題測(cè)試數(shù)據(jù)相差很大么?
            在看了該題的討論版后,得知一個(gè)高效的解法,總結(jié)如下:
            先看一個(gè)簡(jiǎn)單的問題,如何把'+'變成'-'而不改變其他位置上的狀態(tài)?答案是將該位置(i,j)及位置所在的行(i)和列(j)上所有的handle更新一次,結(jié)果該位置被更新了7次,相應(yīng)行(i)和列(j)的handle被更新了6次,剩下的被更新了4次.被更新偶數(shù)次的handle不會(huì)造成最終狀態(tài)的改變.因此得出高效解法,在每次輸入碰到'+'的時(shí)候,自增該位置與相應(yīng)的行和列,當(dāng)輸入結(jié)束后,遍歷數(shù)組,所有為奇數(shù)的位置則是操作的位置,而奇數(shù)位置的個(gè)數(shù)之和則是最終的操作次數(shù).
            PS:該題不會(huì)有"Impossible"的情況.代碼如下:
             1#include <iostream>
             2using namespace std;
             3
             4const int ROW = 4;
             5
             6int _tmain(int argc, _TCHAR* argv[])
             7{
             8    bool HANDLES[ROW][ROW] = {false};
             9    char handle;
            10    for(int i = 0; i < ROW; ++i)
            11    {
            12        for(int j = 0; j < ROW; ++j)
            13        {
            14            cin >> handle;
            15            if(handle == '+')
            16            {
            17                HANDLES[i][j] = !HANDLES[i][j];
            18                for(int k = 0; k < ROW; ++k)
            19                {
            20                    HANDLES[i][k] = !HANDLES[i][k];
            21                    HANDLES[k][j] = !HANDLES[k][j];
            22                }

            23            }

            24        }

            25    }

            26    int result = 0;
            27    for(int i = 0; i < ROW; ++i)
            28    {
            29        for(int j = 0; j < ROW; ++j)
            30        {
            31            if(HANDLES[i][j])
            32            {
            33                ++result;
            34            }

            35        }

            36    }

            37    cout << result << endl;
            38    for(int i = 0; i < ROW; ++i)
            39    {
            40        for(int j = 0; j < ROW; ++j)
            41        {
            42            if(HANDLES[i][j])
            43            {
            44                cout << i + 1 << " " << j + 1 << endl;
            45            }

            46        }

            47    }

            48    return 0;
            49}

            posted on 2009-03-21 11:11 肖羽思 閱讀(3738) 評(píng)論(8)  編輯 收藏 引用 所屬分類: POJ

            FeedBack:
            # re: POJ 2965 解題報(bào)告
            2010-02-15 21:35 | chhaya
            高效解法確實(shí)很NB啊。  回復(fù)  更多評(píng)論
              
            # re: POJ 2965 解題報(bào)告
            2010-04-13 20:20 | Cre
            我們可以聊一聊嗎?不止一次看過你的代碼,感覺有必要向你學(xué)習(xí)一下!  回復(fù)  更多評(píng)論
              
            # re: POJ 2965 解題報(bào)告
            2010-04-14 10:49 | 肖羽思
            @Cre
            歡迎交流哈~~學(xué)習(xí)倒談不上~~歡迎發(fā)郵件至:yusi.xiao@gmail.com  回復(fù)  更多評(píng)論
              
            # re: POJ 2965 解題報(bào)告
            2010-04-14 22:49 | Cre
            @肖羽思
            我們可以通過QQ交流嗎?感覺這種方式更方便一些!當(dāng)然,如果你感覺不方便,也無大礙!  回復(fù)  更多評(píng)論
              
            # re: POJ 2965 解題報(bào)告
            2010-04-15 11:32 | 肖羽思
            @Cre
            好的~~請(qǐng)加:54118794  回復(fù)  更多評(píng)論
              
            # re: POJ 2965 解題報(bào)告
            2010-07-05 15:58 | Tanky Woo
            按你的寫法,似乎輸入一個(gè)+就改一次,也不論后面的是啥,
            個(gè)人感覺是錯(cuò)的。要么也是先全部輸入后再調(diào)整。
            不信你輸入
            ++++
            +---
            +---
            +---
            應(yīng)該輸出1沒錯(cuò)吧?
            而你代碼的結(jié)果是7。
            你的意思就是算的有多少個(gè)+去了。  回復(fù)  更多評(píng)論
              
            # re: POJ 2965 解題報(bào)告
            2010-07-05 16:04 | Tanky Woo
            還有個(gè)地方寫錯(cuò)了,那里分別是7次,4次,和2次,
            而不是7,6,4次
            歡迎去我博客和我探討:
            www.wutianqi.com  回復(fù)  更多評(píng)論
              
            # re: POJ 2965 解題報(bào)告
            2011-04-18 08:41 | 劉灝
            @Tanky Woo
            你說的不對(duì),你根本沒看樓主的算法,結(jié)果必定為1  回復(fù)  更多評(píng)論
              
            <2009年3月>
            22232425262728
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久综合九色综合网站| 97精品伊人久久久大香线蕉| 日本久久久久久中文字幕| 久久亚洲精品无码VA大香大香| 国产精品久久久久久| 日本久久久久亚洲中字幕| 久久精品亚洲AV久久久无码| 人人狠狠综合久久亚洲高清| 97久久精品人人澡人人爽| 91麻精品国产91久久久久 | 狠狠精品干练久久久无码中文字幕 | 性色欲网站人妻丰满中文久久不卡| 久久久精品2019免费观看| 久久精品国产免费观看| 思思久久精品在热线热| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 伊人久久一区二区三区无码| 性做久久久久久免费观看| 亚洲色欲久久久久综合网| 亚洲国产成人精品久久久国产成人一区二区三区综 | 久久久国产精品网站| 久久九九青青国产精品| 久久成人国产精品二三区| 亚洲国产精品久久久久| 国产午夜精品理论片久久| 久久免费观看视频| 精品久久久久久久国产潘金莲| 欧美亚洲国产精品久久高清| 亚洲国产精品无码久久久不卡| 久久久久亚洲AV片无码下载蜜桃| 国产精品久久久久影院嫩草| 99久久精品免费观看国产| 久久久中文字幕日本| 久久久久亚洲精品男人的天堂| 要久久爱在线免费观看| 久久人人妻人人爽人人爽| 四虎国产精品免费久久5151| 亚洲精品成人网久久久久久| 亚洲欧洲日产国码无码久久99| 久久r热这里有精品视频| 久久综合精品国产一区二区三区|