• <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>

            TC-SRM-233-1000pt BFS

            Posted on 2009-11-25 11:39 rikisand 閱讀(279) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Topcoder 、Algorithm
            繼續(xù)是misof 數(shù)字教學(xué)里面的習(xí)題~ 第一篇的最后一道題了~
            Problem Statement

            You are in a maze containing revolving doors. The doors can be turned 90 degrees by pushing against them in either direction. You are to find a route from the start square to the end square that involves revolving as few doors as possible. Given a map of the maze, determine the fewest number of door revolutions necessary to get from the start to the end.

            In the map:

               ' ': empty space
               '#': wall
               'O': center of a revolving door (letter "oh", not zero)
               '-': horizontal door (always adjacent to a 'O')
               '|': vertical door (always adjacent to a 'O')
               'S': start square
               'E': end square
            

            Each revolving door will always be oriented horizontally (with two horizontal segments) or vertically (with two vertical segments):

                |
                O  or  -O-
                |
            

            Doors can be revolved 90 degrees by moving onto a door segment from any of the 4 squares diagonally adjacent to the door center, i.e., the 'X' characters below:

               X|X     X X
                O  or  -O-
               X|X     X X
            

            Here is an example map:

                    ###
                    #E#
                   ## #
                ####  ##
                # S -O-#
                # ###  #
                #      #
                ########
            

            In this example, 2 door revolutions are necessary to get from 'S' to 'E'. The first turn is shown here:

                    ###         ###
                    #E#         #E#
                   ## #        ## #
                ####  ##    #### |##
                # S -O-#    # S  OX#
                # ### X#    # ###| #
                #      #    #      #
                ########    ########
            

            And the second turn is shown here:

                    ###         ###
                    #E#         #E#
                   ## #        ## #
                ####X|##    #### X##
                # S  O #    # S -O-#
                # ###| #    # ###  #
                #      #    #      #
                ########    ########
            

            Your method should return an int, the minimum number of door revolutions necessary to get from the start square to the end square. If there is no way to reach the end square, return -1.

            Definition

            Class:
            RevolvingDoors

            Method:
            turns

            Parameters:
            vector <string>

            Returns:
            int

            Method signature:
            int turns(vector <string> map)

            (be sure your method is public)

            Notes

            -
            Assume that all squares off the edge of the map are walls.

            Constraints

            -
            map will contain between 3 and 50 elements, inclusive.

            -
            Each element of map will contain between 3 and 50 characters, inclusive.

            -
            Each element of map will contain the same number of characters.

            -
            Each character in map will be one of 'S', 'E', 'O', '-', '|', '#', and ' ' (space).

            -
            There will be exactly one 'S' and one 'E' in map.

            -
            There will be between 1 and 10 doors, inclusive, and they will be formatted in map as described in the problem statement.

            -
            No two doors will be close enough for any part of them to occupy the same square.

            -
            It is not allowed for a door to be blocked and unable to turn. There will not be any walls in any of the 4 squares immediately adjacent to the center of a door, nor will a door be on the edge of the map.

            關(guān)鍵是門的狀態(tài)表示,參考了網(wǎng)站上的代碼,挑了一個(gè)比較簡(jiǎn)練的,用的優(yōu)先級(jí)隊(duì)列。寫完調(diào)好發(fā)現(xiàn)TLE 囧~ copy出網(wǎng)站上的再run依然TLE``` ``` 看了下發(fā)現(xiàn)現(xiàn)在的system testing 比原來增加了幾個(gè)測(cè)試用例~~~   于是找出misof大牛的解法,發(fā)現(xiàn)對(duì)狀態(tài)處理一樣的~~~只不過用了memo和deque,省去了優(yōu)先級(jí)隊(duì)列調(diào)整的時(shí)間開銷,改好了就pass了~ 上代碼~~:
            Code Snippet
            using namespace std;
            typedef long long int64;  
            typedef vector<int> VI;
            typedef vector<string> VS;
            #define inf 1000000
            #define REP(i,n) for(int (i)=(0);((i)<(n));++(i))
            template<class T> inline void checkmin(T &a,const T &b) { if (b<a) a=b; }
            template<class T> inline void checkmax(T &a,const T &b) { if (b>a) a=b; }
            int dr[]={-1,0,1,0};
            int dc[]={0,1,0,-1};
            struct state{state(int x,int y,int z,int s):r(x),c(y),doorstate(z),best(s){}int r;int c;int doorstate;int best;};
            int memo[56][56][1<<11];
            class RevolvingDoors
            {
                    public:
                    int turns(vector <string> mp)
                    {
                         int x=mp.size()+2;int y=mp[0].size()+2;
                         int sr,sc,er,ec,cnt=0,doorInit=0;
                         REP(i,x-2)mp[i]='#'+mp[i]+'#';                //trick:modify the pattern to make it easy to loop
                         mp.insert(mp.begin(),string(58,'#'));
                         mp.push_back(string(58,'#'));
                         REP(i,x)REP(j,y)if(mp[i][j]=='S'){mp[i][j]=' ';sr=i;sc=j;}else if(mp[i][j]=='E'){mp[i][j]=' ';er=i;ec=j;}
                         REP(i,x)REP(j,y)if(mp[i][j]=='O'){if(mp[i-1][j]=='|')doorInit|=(1<<cnt);
                            mp[i-1][j]=mp[i+1][j] = 100 + cnt*2 +1;    //use the content in the box to identify the door number,and the door pos
                            mp[i][j-1]=mp[i][j+1] = 100 + cnt*2 ;    //if pos==0 it means this box is on the left or right of the door
                            cnt++; mp[i][j]='#';
                         }
                         REP(i,x)REP(j,y)REP(t,1<<cnt) memo[i][j][t] = inf;    //init the memo
                         deque<state> Q; Q.push_back(state(sr,sc,doorInit,0));
                         while(!Q.empty()){
                            state now=Q.front();Q.pop_front();
                            int r=now.r  , c=now.c  , door=now.doorstate , b=now.best;
                            if( memo[r][c][door] < b)continue;    //no better ,no vist
                            REP(dir,4){                            //try four direction
                                int nr=r+dr[dir],nc=c+dc[dir];
                                if(mp[nr][nc]==' '){
                                    if(memo[nr][nc][door] > b){ memo[nr][nc][door]=b;Q.push_back(state(nr,nc,door,b));}
                                }
                                else if(mp[nr][nc]=='#')continue;
                                else{                            //if we come to a box near to the door-mid
                                    int doornum=(mp[nr][nc]-100)/2;int open=(mp[nr][nc]-100)%2;    
                                    if( ((door>>doornum)&1) != open){    //lucily,the box is empty
                                        if(memo[nr][nc][door] > b){memo[nr][nc][door] = b;Q.push_back(state(nr,nc,door,b));}
                                    }        
                                    else {                                // the box has a door
                                        if(open==0 && dr[dir]==0) continue;    //try to define the relative pos between the direction and the box
                                        if(open==1 && dc[dir]==0) continue;    //also ~ if we cannot push the door we give up
                                        int ndoor=door^(1<<doornum);    //we can go into the box if we push the door ~
                                        if(memo[nr][nc][ndoor] > b+1 ){memo[nr][nc][ndoor] = b+1 ;Q.push_back(state(nr,nc,ndoor,b+1));}
                                    }
                                }
                            }
                         }
                         int ans=inf;
                         REP(i,1<<cnt){ //loop to check the best ans~
                             if(memo[er][ec][i]<ans){ans=memo[er][ec][i];cout<<er<<" "<<ec<<" "<<hex<<i<<endl;}
                         }
                         if(ans == inf) return -1;
                         else return ans;
                    }

            中文copy是亂碼···囧啊~~ 俺的破爛英文注釋啊~~~

            久久99精品国产麻豆宅宅| 一本色综合网久久| 国产精品美女久久久m| 97精品伊人久久久大香线蕉| 亚洲精品综合久久| 亚洲精品国产自在久久| 国产亚洲精久久久久久无码AV| 久久99国产亚洲高清观看首页| 国产欧美久久久精品| 久久久久国产精品| 国产精品欧美久久久久无广告 | 免费无码国产欧美久久18| 久久久久亚洲精品天堂久久久久久 | 国内精品久久久久久中文字幕| 久久九色综合九色99伊人| 久久性精品| 成人久久免费网站| 精品国产VA久久久久久久冰 | 久久精品成人免费观看97| 国内精品久久久久久久久电影网| 久久99精品久久久久久齐齐| 久久人妻AV中文字幕| 久久久久久九九99精品| 亚洲国产精品久久久久婷婷软件 | 久久97精品久久久久久久不卡| 国产Av激情久久无码天堂| 久久久久四虎国产精品| 久久天天躁狠狠躁夜夜av浪潮| 中文字幕精品久久| 精品乱码久久久久久久| 国产亚洲美女精品久久久| 久久99久久99精品免视看动漫 | 久久国产精品视频| 久久精品桃花综合| 国产精品久久久久久搜索| 日产久久强奸免费的看| 少妇久久久久久被弄高潮| 国产香蕉97碰碰久久人人| 欧美熟妇另类久久久久久不卡 | 精品久久久无码21p发布 | 久久91精品国产91久久麻豆|