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

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>
            欧美专区亚洲专区| 亚洲天堂黄色| 国产日韩欧美在线视频观看| 在线视频亚洲欧美| 洋洋av久久久久久久一区| 欧美日韩国产综合视频在线观看| 亚洲国产成人高清精品| 亚洲成人中文| 欧美成熟视频| 日韩午夜高潮| 亚洲一区二区三区成人在线视频精品| 国产精品久久久一区二区三区| 欧美一区二区三区视频在线 | 欧美理论在线| 亚洲欧美日韩精品久久久| 先锋影音网一区二区| 亚洲国产1区| 亚洲色图综合久久| 亚洲成人资源| 亚洲天堂av高清| 亚洲国产精品va在线看黑人动漫| 亚洲精品老司机| 国产亚洲欧美日韩美女| 亚洲欧洲一区二区天堂久久| 国产精品综合视频| 亚洲激情在线播放| 国产亚洲一二三区| 亚洲乱码国产乱码精品精可以看| 国产一区在线免费观看| 亚洲精品护士| 激情综合网址| 亚洲色诱最新| 亚洲精品黄色| 久久精品在线视频| 一区二区三区精品视频在线观看| 久久久免费观看视频| 亚洲——在线| 欧美精品综合| 亚洲国产成人一区| 激情欧美一区二区| 亚洲一区尤物| 亚洲视频一区在线观看| 久久夜色精品国产欧美乱| 午夜精彩视频在线观看不卡| 欧美激情一区二区三区蜜桃视频 | 亚洲裸体俱乐部裸体舞表演av| 激情综合色综合久久| 午夜精品一区二区在线观看 | 午夜久久久久| 欧美亚一区二区| 亚洲精品麻豆| 夜夜躁日日躁狠狠久久88av| 美日韩丰满少妇在线观看| 久久婷婷蜜乳一本欲蜜臀| 国产免费成人| 午夜精品一区二区在线观看| 亚洲欧美日韩爽爽影院| 国产精品久久久久久av福利软件| 99av国产精品欲麻豆| 一本色道久久综合亚洲91| 欧美激情中文字幕一区二区| 亚洲高清毛片| 亚洲精品中文字| 欧美精品免费在线| 99国产精品久久久久久久久久| 亚洲伦理网站| 欧美日韩一区二区三区在线观看免| 亚洲精品美女久久7777777| 亚洲精品欧美一区二区三区| 欧美激情亚洲另类| 99精品欧美| 欧美一级视频| 国内精品视频666| 久久免费精品日本久久中文字幕| 欧美风情在线| 99热精品在线观看| 国产精品国产精品| 欧美一区二区三区在线看| 老司机精品久久| 日韩亚洲欧美中文三级| 国产精品av免费在线观看| 亚洲欧美成人一区二区三区| 久久蜜桃精品| 99re6这里只有精品| 国产精品三区www17con| 久久se精品一区二区| 欧美韩日一区二区| 亚洲天堂成人| 一区二区三区中文在线观看| 欧美成人午夜视频| 亚洲一区国产精品| 久久一区精品| 99国产精品视频免费观看一公开 | 欧美三区免费完整视频在线观看| 亚洲影院高清在线| 欧美国产精品日韩| 亚洲一区中文字幕在线观看| 国产一区91精品张津瑜| 欧美岛国在线观看| 欧美一区二区三区四区在线观看地址| 欧美成人一区二区三区| 亚洲欧美日韩在线| 亚洲国产高清一区| 国产精品一区二区三区成人| 欧美成人精品高清在线播放| 午夜天堂精品久久久久| 亚洲精品免费网站| 免费观看在线综合色| 亚洲在线播放电影| 亚洲人成在线播放| 国产婷婷精品| 国产精品嫩草影院一区二区| 欧美 亚欧 日韩视频在线| 欧美影院久久久| 中日韩视频在线观看| 亚洲激情影视| 欧美电影专区| 裸体丰满少妇做受久久99精品| 亚洲自拍都市欧美小说| 日韩视频专区| 亚洲二区三区四区| 精品99一区二区| 国产欧美日韩三级| 国产精品日韩一区二区三区| 欧美全黄视频| 欧美精品激情在线| 欧美成熟视频| 欧美激情一区二区在线| 美国成人直播| 牛夜精品久久久久久久99黑人| 久久精品91| 久久久久成人精品| 欧美一级成年大片在线观看| 亚洲免费一区二区| 亚洲影音一区| 羞羞色国产精品| 午夜精彩国产免费不卡不顿大片| 在线性视频日韩欧美| 夜夜嗨av一区二区三区免费区| 亚洲美女黄网| 日韩亚洲精品在线| 在线中文字幕一区| 亚洲午夜视频在线| 午夜精品福利一区二区蜜股av| 亚洲综合第一页| 欧美一区二区国产| 久久精品一区二区三区中文字幕| 久久久水蜜桃av免费网站| 久久精品在线免费观看| 久久综合网络一区二区| 美腿丝袜亚洲色图| 欧美日韩精品一区视频| 国产精品裸体一区二区三区| 国产欧美精品国产国产专区| 国内一区二区三区| 亚洲欧洲一区二区在线观看| 亚洲最新视频在线播放| 午夜久久美女| 老色批av在线精品| 91久久线看在观草草青青| 日韩一级黄色片| 午夜精品成人在线| 免费在线看一区| 欧美视频在线观看 亚洲欧| 国产乱肥老妇国产一区二| 精品二区视频| 9色精品在线| 欧美在线看片a免费观看| 免费国产自线拍一欧美视频| 亚洲日韩欧美视频一区| 亚洲欧美成人一区二区在线电影 | 欧美亚洲在线播放| 欧美国产日韩视频| 国产人成精品一区二区三| 亚洲激情网站免费观看| 亚洲欧美国产不卡| 欧美成人久久| 亚洲伊人伊色伊影伊综合网| 狼人天天伊人久久| 国产精品人成在线观看免费| 亚洲黄色一区| 久久国产欧美| 亚洲美女av在线播放| 久久久国产精品一区二区中文| 欧美日韩不卡视频| 在线欧美亚洲| 香蕉av福利精品导航| 亚洲人成网站在线播| 欧美在线视频a| 国产精品美女www爽爽爽| 精久久久久久| 久久国产日本精品| 99热免费精品在线观看| 免费人成网站在线观看欧美高清| 国产免费成人在线视频| 亚洲午夜av在线| 亚洲电影自拍| 麻豆成人在线观看| 国内一区二区在线视频观看| 午夜一区二区三区在线观看|