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

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精品视频网| 国产欧美日韩精品专区| 91久久国产精品91久久性色| 欧美一区二区三区在线视频| 午夜在线一区二区| 久久久人成影片一区二区三区观看| 亚洲欧美国产精品专区久久| 欧美一级黄色录像| 欧美暴力喷水在线| 亚洲精品久久久久中文字幕欢迎你 | 国产在线视频不卡二| 国产日韩欧美综合在线| 在线成人av| 亚洲影视在线| 久久天天躁夜夜躁狠狠躁2022| 男男成人高潮片免费网站| 亚洲精品日韩综合观看成人91| 亚洲深爱激情| 久久午夜电影| 国产精品日韩精品欧美精品| 国内精品久久久| 一本色道久久88亚洲综合88| 欧美一区二区在线免费观看| 欧美成人有码| 亚洲欧美国产高清va在线播| 欧美高清视频一区二区| 国产午夜精品一区理论片飘花 | 欧美xart系列高清| 国产精品日韩久久久| 最新国产乱人伦偷精品免费网站 | 久久久一区二区| 亚洲国产欧美国产综合一区 | 亚洲精品一区二区三区av| 午夜在线a亚洲v天堂网2018| 欧美成人网在线| 国产亚洲精品v| 亚洲午夜精品久久久久久浪潮 | 国产欧美精品| 国产视频在线观看一区二区| 日韩一级在线| 久久理论片午夜琪琪电影网| 日韩视频不卡| 美日韩精品视频| 国产永久精品大片wwwapp| 亚洲一区二区精品在线观看| 亚洲第一狼人社区| 久久久人人人| 影音先锋日韩资源| 香蕉成人啪国产精品视频综合网| 亚洲国产成人av好男人在线观看| 久久精品免费| 国产字幕视频一区二区| 欧美在线网站| 亚洲欧美日韩综合国产aⅴ| 国产精品久久久久永久免费观看| 一区二区三区精品视频| 亚洲精品欧美在线| 欧美区亚洲区| 中文日韩电影网站| 99精品久久| 国产精品嫩草影院av蜜臀| 亚洲免费在线观看视频| 一区二区三区色| 国产精品青草久久久久福利99| 亚洲在线播放电影| 亚洲制服av| 韩国精品一区二区三区| 免费亚洲电影在线| 欧美高清视频免费观看| 亚洲午夜视频| 午夜免费电影一区在线观看| 狠狠色综合网| 亚洲国产一区二区a毛片| 欧美日韩a区| 午夜伦欧美伦电影理论片| 亚洲欧美日韩直播| 伊人久久大香线蕉av超碰演员| 久久琪琪电影院| 免费观看在线综合| 亚洲午夜黄色| 欧美与黑人午夜性猛交久久久| 极品少妇一区二区三区精品视频| 毛片一区二区三区| 欧美日韩激情小视频| 欧美怡红院视频一区二区三区| 欧美在线啊v一区| 亚洲精品国产拍免费91在线| 日韩视频免费大全中文字幕| 国产女人水真多18毛片18精品视频| 久久在线播放| 欧美日韩在线大尺度| 久久精品人人爽| 欧美久久久久久久| 久久噜噜噜精品国产亚洲综合| 免费在线观看一区二区| 亚洲欧美www| 欧美成人精品h版在线观看| 性久久久久久久| 美女视频黄a大片欧美| 国产亚洲欧美日韩在线一区| 欧美成人一品| 国产日本欧美一区二区三区在线| 欧美福利精品| 国产亚洲欧美一区| 亚洲精品视频一区二区三区| 国产一区二区高清视频| 一本大道av伊人久久综合| 在线欧美日韩| 欧美一级在线播放| 亚洲在线免费观看| 欧美激情精品久久久久| 久久久精彩视频| 国产精品99免视看9| 欧美激情精品久久久久久变态| 国产欧美综合一区二区三区| 夜夜精品视频| 一本大道久久a久久综合婷婷| 久久久久久久一区二区三区| 香蕉久久久久久久av网站| 欧美日韩理论| 亚洲人精品午夜在线观看| 亚洲高清在线观看| 久久精品中文字幕免费mv| 久久精品人人做人人爽| 国产精品丝袜白浆摸在线| 亚洲伦理自拍| 亚洲美女视频网| 免费亚洲电影| 欧美激情精品久久久久久久变态| 狠狠色丁香久久婷婷综合丁香| 亚洲一区激情| 午夜日韩激情| 国产精品午夜av在线| 亚洲视频 欧洲视频| 亚洲免费一区二区| 国产精品日韩高清| 欧美一级成年大片在线观看| 欧美主播一区二区三区| 国产日韩精品一区二区三区在线| 亚洲小少妇裸体bbw| 午夜精品视频在线观看| 国产精品一区视频网站| 午夜精品久久久久久久白皮肤| 性伦欧美刺激片在线观看| 国产农村妇女精品一区二区| 欧美一区二区视频97| 久久青青草综合| 亚洲国产高清一区二区三区| 欧美国产成人精品| 在线视频你懂得一区二区三区| 亚洲欧美在线网| 国产在线欧美日韩| 欧美aa在线视频| 一本久道久久综合中文字幕| 午夜亚洲视频| 依依成人综合视频| 欧美激情亚洲视频| 亚洲免费一在线| 麻豆国产va免费精品高清在线| 亚洲片在线观看| 国产精品久久久久9999高清| 欧美在线观看一区二区三区| 欧美国产激情二区三区| 在线中文字幕日韩| 国产情侣一区| 免费成人网www| 亚洲视频在线免费观看| 久久久天天操| 一本久久综合亚洲鲁鲁五月天| 国产精品日本一区二区| 欧美电影免费观看| 一区二区三区鲁丝不卡| 国产欧美在线| 欧美成人精品在线| 亚洲欧美日韩网| 亚洲电影免费在线| 午夜免费久久久久| 亚洲人体1000| 国产乱肥老妇国产一区二| 老色鬼久久亚洲一区二区| 亚洲少妇诱惑| 亚洲国产日韩美| 久久成人资源| 一区二区三区四区五区精品视频| 国产一区二区三区久久 | 久久三级视频| 在线亚洲美日韩| 亚洲第一福利视频| 国产美女精品在线| 欧美日韩国产色站一区二区三区 | 国产午夜亚洲精品羞羞网站| 欧美暴力喷水在线| 久久精品成人一区二区三区蜜臀| 99re视频这里只有精品| 亚洲福利视频网| 久久精品二区亚洲w码| 亚洲一区二区三区精品视频| 亚洲国产精品一区制服丝袜 | 欧美在线观看一二区| 国产精品99久久久久久有的能看|