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

TC-SRM-233-1000pt BFS

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

關鍵是門的狀態(tài)表示,參考了網(wǎng)站上的代碼,挑了一個比較簡練的,用的優(yōu)先級隊列。寫完調好發(fā)現(xiàn)TLE 囧~ copy出網(wǎng)站上的再run依然TLE``` ``` 看了下發(fā)現(xiàn)現(xiàn)在的system testing 比原來增加了幾個測試用例~~~   于是找出misof大牛的解法,發(fā)現(xiàn)對狀態(tài)處理一樣的~~~只不過用了memo和deque,省去了優(yōu)先級隊列調整的時間開銷,改好了就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>
            亚洲欧美电影院| 久久久久久久欧美精品| 一区二区三区欧美日韩| 99精品国产一区二区青青牛奶| 开心色5月久久精品| 久久男人av资源网站| 黑人一区二区三区四区五区| 欧美中文字幕在线| 蜜桃av综合| 日韩视频免费大全中文字幕| 欧美日韩国产123| 亚洲一区二区欧美| 久久亚洲精选| 亚洲一区日韩| 一本色道久久综合| 欧美一区二区三区视频免费播放| 国产亚洲午夜高清国产拍精品| 久久国产婷婷国产香蕉| 免费成人在线视频网站| 宅男精品视频| 狠狠色丁香婷婷综合| 欧美成人亚洲| 亚洲欧美日韩视频一区| 欧美黄色小视频| 亚洲欧美日韩精品在线| 亚洲第一页在线| 国产精品三级久久久久久电影| 久久精品国产视频| 99re66热这里只有精品4| 老鸭窝毛片一区二区三区| av成人毛片| 欧美图区在线视频| 久久狠狠婷婷| 日韩午夜精品| 亚洲第一毛片| 亚洲一区二区在线看| 一本色道久久88精品综合| 午夜精品婷婷| 亚洲精品乱码久久久久久久久 | 久久久国产精彩视频美女艺术照福利| 欧美国产视频日韩| 久久精品理论片| 宅男噜噜噜66一区二区 | 欧美日韩1区2区3区| 久久精品夜色噜噜亚洲aⅴ| 亚洲视频香蕉人妖| 亚洲人成网站在线观看播放| 久久精品一区二区三区四区| 亚洲一二区在线| 99精品久久久| 在线观看不卡| 激情久久久久久| 国产一区在线观看视频| 欧美视频一区| 欧美日韩视频在线一区二区 | 欧美a级片网站| 久久精品国产清高在天天线| 亚洲一区二区在线播放| 久久久综合网站| 亚洲一区二区三区久久| 亚洲三级毛片| 亚洲黄色在线| 亚洲黄色三级| 日韩视频在线免费观看| 亚洲精品无人区| 亚洲精选久久| 在线一区二区三区四区| 99精品国产在热久久| 亚洲一级二级在线| 亚洲欧美另类综合偷拍| 亚洲女同精品视频| 欧美专区在线播放| 久久亚洲欧美| 免费日韩av| 亚洲高清成人| 亚洲人成网站色ww在线| 一区二区三区精品国产| 亚洲女优在线| 久久久久一区二区三区| 久久先锋影音av| 欧美精品一区在线发布| 国产精品www网站| 国产亚洲精品成人av久久ww| 黄色亚洲精品| 一区二区三区高清不卡| 亚洲自拍电影| 欧美日韩爆操| 欧美午夜国产| 一区二区在线观看av| 亚洲精品一区二区三区蜜桃久| 正在播放欧美视频| 欧美一级视频| 欧美激情1区| 在线中文字幕一区| 欧美中文字幕视频| 欧美日韩视频一区二区| 国产日韩欧美日韩大片| 亚洲人成网站精品片在线观看| 亚洲一区二区三区成人在线视频精品 | 欧美xx视频| 国产精品大片免费观看| 国产综合色产在线精品| 日韩视频三区| 久久久www成人免费毛片麻豆| 欧美激情黄色片| 亚洲一区精彩视频| 欧美在线一二三四区| 欧美日韩日韩| 在线精品福利| 性一交一乱一区二区洋洋av| 欧美激情第五页| 久久动漫亚洲| 日韩西西人体444www| 久久精品99国产精品| 欧美日韩在线三区| 在线播放亚洲| 亚洲欧美日韩在线综合| 亚洲激情偷拍| 欧美一区国产一区| 欧美日韩一二三四五区| 在线视频观看日韩| 久久精品人人做人人爽电影蜜月| 亚洲激情一区二区| 裸体素人女欧美日韩| 国产综合久久久久影院| 欧美一区午夜精品| 在线性视频日韩欧美| 欧美人妖另类| 亚洲精品婷婷| 亚洲欧洲日本专区| 欧美不卡在线| 亚洲国产精品va在看黑人| 久久久久成人网| 久久av二区| 一区二区三区在线不卡| 久久婷婷国产麻豆91天堂| 亚欧成人在线| 国产主播精品| 久久免费视频在线| 久久激情一区| 亚洲大黄网站| 亚洲国产精品视频| 欧美高清在线视频| 99re66热这里只有精品3直播| 欧美激情中文字幕一区二区| 久久先锋影音| 亚洲精品美女在线观看| 亚洲激情黄色| 国产精品成人在线观看| 性欧美xxxx大乳国产app| 亚洲欧美日韩精品一区二区| 国产精品综合不卡av| 久久国产婷婷国产香蕉| 久久国产视频网| 亚洲人成啪啪网站| 一本色道久久综合亚洲91| 国产精品欧美日韩一区二区| 久久国产色av| 欧美.日韩.国产.一区.二区| 99精品热视频| 亚洲夜间福利| 亚洲国产美女久久久久| 亚洲美女视频网| 国产视频精品xxxx| 欧美成人免费在线| 欧美视频一区二区三区| 亚洲欧美日韩精品久久久| 欧美一区二区三区成人 | 日韩视频精品在线| 亚洲欧美国产另类| 136国产福利精品导航| 亚洲精品久久嫩草网站秘色| 亚洲一区二区黄色| 在线观看国产成人av片| 亚洲日本在线观看| 国产精品永久| 欧美黑人在线观看| 国产伦精品一区二区三区免费迷 | 亚洲在线视频免费观看| 亚洲国产日韩在线一区模特| 国产精品99久久久久久久vr| 伊人伊人伊人久久| 亚洲一区二区免费| 日韩亚洲欧美成人| 久久精品99无色码中文字幕| 国产精品99久久不卡二区| 久久久久成人精品免费播放动漫| 日韩视频免费观看| 久久亚洲私人国产精品va| 性欧美办公室18xxxxhd| 欧美激情一二区| 欧美成人午夜| 国内精品嫩模av私拍在线观看| 99精品99久久久久久宅男| 一区福利视频| 性色av一区二区怡红| 亚洲免费视频一区二区| 欧美另类久久久品 | 亚洲欧美日韩直播| 亚洲一区在线视频|