青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

TC-SRM-233-1000pt BFS

Posted on 2009-11-25 11:39 rikisand 閱讀(301) 評論(0)  編輯 收藏 引用 所屬分類: TopcoderAlgorithm
繼續是misof 數字教學里面的習題~ 第一篇的最后一道題了~
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.

關鍵是門的狀態表示,參考了網站上的代碼,挑了一個比較簡練的,用的優先級隊列。寫完調好發現TLE 囧~ copy出網站上的再run依然TLE``` ``` 看了下發現現在的system testing 比原來增加了幾個測試用例~~~   于是找出misof大牛的解法,發現對狀態處理一樣的~~~只不過用了memo和deque,省去了優先級隊列調整的時間開銷,改好了就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是亂碼···囧啊~~ 俺的破爛英文注釋啊~~~

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            国产精品第一区| 欧美母乳在线| 激情文学一区| 久久综合99re88久久爱| 久久精品视频99| 亚洲人体1000| 亚洲免费精彩视频| 国产女人18毛片水18精品| 久久精品一区二区三区四区| 久久爱www.| 最新中文字幕一区二区三区| 亚洲精品免费观看| 国产精品高潮呻吟久久av无限| 亚洲综合清纯丝袜自拍| 久久av二区| 99re6热在线精品视频播放速度| 一区二区三欧美| 国产一区二区三区四区五区美女| 欧美成人在线免费视频| 欧美日韩成人一区| 久久精品国产亚洲一区二区三区| 久久人人97超碰人人澡爱香蕉| 99视频超级精品| 欧美制服丝袜第一页| 亚洲精选一区| 久久xxxx精品视频| 亚洲视频第一页| 久久久夜色精品亚洲| 亚洲一区二区高清视频| 久久在线播放| 亚洲欧美影院| 欧美高清视频一区| 久久久综合免费视频| 欧美日韩视频在线观看一区二区三区 | 久久久久久久国产| 亚洲神马久久| 女人天堂亚洲aⅴ在线观看| 亚洲综合色噜噜狠狠| 麻豆成人精品| 欧美综合激情网| 欧美性天天影院| 亚洲第一精品在线| 精久久久久久| 一区二区日韩欧美| 日韩一区二区高清| 美女黄毛**国产精品啪啪| 欧美中在线观看| 国产精品福利网| 亚洲美女色禁图| 亚洲伦理在线| 美日韩精品免费观看视频| 久久精品国产综合| 国产目拍亚洲精品99久久精品| 91久久在线观看| 最新热久久免费视频| 老司机67194精品线观看| 久久精品国产清自在天天线| 国产精品久久久久一区| 99国产精品久久久久久久成人热| 亚洲人成高清| 欧美精品免费观看二区| 亚洲大胆美女视频| 亚洲国内精品在线| 欧美电影免费观看高清| 欧美激情区在线播放| 亚洲精品一区二区三区福利| 久久视频精品在线| 免费不卡在线观看| 亚洲国产精品成人一区二区 | 久久本道综合色狠狠五月| 午夜精品久久久久久久99水蜜桃| 欧美午夜精品久久久| av成人福利| 亚洲一区二区三区在线| 国产精品毛片一区二区三区| 亚洲图片欧洲图片av| 欧美在线三区| 海角社区69精品视频| 久久女同精品一区二区| 亚洲第一页在线| 一区二区三区欧美在线| 国产精品久久久久久久午夜片| 亚洲在线观看视频| 久久久欧美精品| 亚洲激情网址| 国产精品二区三区四区| 香蕉成人伊视频在线观看| 久久夜色精品亚洲噜噜国产mv| 在线观看三级视频欧美| 欧美高清在线播放| 亚洲一级在线观看| 久久综合99re88久久爱| 日韩视频在线一区二区| 国产精品亚洲欧美| 卡一卡二国产精品| 日韩一区二区精品在线观看| 欧美在线短视频| 亚洲精品一区二区网址| 国产精品视频1区| 久久综合九色综合久99| 亚洲最新在线视频| 久久综合导航| 亚洲免费在线观看| 亚洲电影有码| 国产精品视频久久一区| 久久综合九色欧美综合狠狠| 一区二区三区精品| 欧美成人午夜| 久久er精品视频| 一区二区三区 在线观看视| 国产在线视频欧美| 国产精品久久久久久久久久久久久| 久久高清福利视频| 一区二区欧美亚洲| 亚洲国产高清自拍| 久久久久久亚洲精品杨幂换脸| 99在线精品免费视频九九视| 狠狠色丁香久久婷婷综合_中| 欧美日韩高清在线播放| 久久精品一区| 欧美一级午夜免费电影| 日韩写真在线| 亚洲国产成人久久| 女女同性精品视频| 久久精品夜色噜噜亚洲a∨ | 亚洲欧美另类久久久精品2019| 怡红院av一区二区三区| 国产欧美日韩亚洲精品| 欧美日韩在线视频观看| 欧美成年人网| 免费视频久久| 浪潮色综合久久天堂| 久久久久一区二区| 久久精品视频导航| 久久精品一级爱片| 久久久久久久波多野高潮日日 | 欧美电影美腿模特1979在线看| 久久aⅴ国产紧身牛仔裤| 亚洲一区二区久久| 亚洲视频日本| 亚洲一区成人| 亚洲一区二区不卡免费| 亚洲午夜久久久久久久久电影院| 亚洲美女黄色| 一区二区三区国产在线| 9i看片成人免费高清| 亚洲精品专区| 中文国产成人精品| 亚洲欧美电影在线观看| 午夜精品福利电影| 欧美一区二区三区婷婷月色 | 亚洲一区在线播放| 亚洲在线播放电影| 午夜免费日韩视频| 久久久精品999| 猛男gaygay欧美视频| 欧美激情第8页| 国产精品ⅴa在线观看h| 国产精品一区免费观看| 韩国成人精品a∨在线观看| 精品二区久久| 日韩午夜视频在线观看| 制服诱惑一区二区| 久久国产精品一区二区三区| 久久深夜福利免费观看| 欧美国产三区| 中国成人在线视频| 欧美在线1区| 欧美精品国产精品日韩精品| 欧美性色综合| 精品福利电影| 国产精品99久久久久久白浆小说| 亚洲欧美日韩一区二区三区在线 | 亚洲国产日韩精品| 一区二区电影免费观看| 久久av资源网| 亚洲黄色三级| 欧美一区二区三区婷婷月色 | 亚洲午夜激情在线| 久久精品国产99| 欧美色图麻豆| 在线观看日韩国产| 亚洲一区国产精品| 欧美成人精品福利| 亚洲午夜免费福利视频| 美腿丝袜亚洲色图| 国产欧美日韩综合| 一本大道av伊人久久综合| 久久久一区二区三区| 日韩香蕉视频| 免费欧美日韩| 国产中文一区二区| 亚洲欧美不卡| 亚洲黄色视屏| 麻豆av一区二区三区久久| 国产欧美精品一区aⅴ影院| 一区二区免费在线播放| 暖暖成人免费视频| 欧美自拍偷拍| 国产亚洲二区|