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

TC-SRM-233-1000pt BFS

Posted on 2009-11-25 11:39 rikisand 閱讀(301) 評論(0)  編輯 收藏 引用 所屬分類: TopcoderAlgorithm
繼續(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è)比較簡練的,用的優(yōu)先級隊(duì)列。寫完調(diào)好發(fā)現(xiàn)TLE 囧~ copy出網(wǎng)站上的再run依然TLE``` ``` 看了下發(fā)現(xiàn)現(xiàn)在的system testing 比原來增加了幾個(gè)測試用例~~~   于是找出misof大牛的解法,發(fā)現(xiàn)對狀態(tài)處理一樣的~~~只不過用了memo和deque,省去了優(yōu)先級隊(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是亂碼···囧啊~~ 俺的破爛英文注釋啊~~~


只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲国产精品日韩| 亚洲人成亚洲人成在线观看图片| 免费成人黄色片| 午夜在线视频观看日韩17c| 久久影视三级福利片| 欧美一区二区三区免费观看| 欧美精品久久久久a| 免费永久网站黄欧美| 国产午夜精品久久久久久免费视| 99亚洲精品| 亚洲乱码日产精品bd| 久久日韩精品| 蜜桃av综合| 曰韩精品一区二区| 久久激情综合| 久久久精品久久久久| 国产精品永久入口久久久| 一区二区三区国产在线| 在线中文字幕日韩| 欧美日韩国产a| 亚洲精品中文在线| 正在播放欧美视频| 欧美久久久久| 日韩一级在线观看| 亚洲少妇诱惑| 国产精品成人v| 亚洲与欧洲av电影| 欧美在线视频导航| 国产香蕉97碰碰久久人人| 午夜一区二区三区在线观看| 欧美在线视频导航| 国产一区二区三区黄视频| 欧美一区二区三区在线观看视频| 久久久999精品| 依依成人综合视频| 欧美成年视频| 日韩午夜在线播放| 香蕉成人伊视频在线观看| 国产日产欧产精品推荐色 | 久久久亚洲欧洲日产国码αv| 久久久久久久久久码影片| 激情婷婷欧美| 欧美成人性生活| 亚洲婷婷综合久久一本伊一区| 亚洲欧美视频在线观看视频| 国产伦精品一区二区三区高清| 小黄鸭视频精品导航| 免费观看久久久4p| 99视频一区| 国产乱码精品一区二区三区忘忧草 | 亚洲欧美一区二区激情| 久久精品一区二区国产| 亚洲第一视频| 欧美日韩无遮挡| 欧美一区日本一区韩国一区| 欧美国产日韩xxxxx| 亚洲天堂成人在线视频| 国产视频观看一区| 欧美成人精品一区二区三区| 亚洲夜间福利| 欧美a级一区| 亚洲一级片在线看| 黑人极品videos精品欧美裸| 欧美日本三区| 欧美在线综合| 一本久久a久久免费精品不卡| 久热精品视频在线免费观看| 日韩亚洲在线| 国产亚洲欧美日韩一区二区| 欧美激情亚洲一区| 欧美在线资源| 中文在线一区| 亚洲国产高清aⅴ视频| 欧美伊人影院| 中文日韩电影网站| 黄色精品一区二区| 欧美一区二区三区日韩| 欧美日韩激情小视频| 亚洲精选视频在线| 久久影音先锋| 西瓜成人精品人成网站| 亚洲精品国产系列| 激情欧美国产欧美| 欧美午夜视频在线观看| 裸体一区二区| 久久国产视频网站| 亚洲男人影院| 中文亚洲视频在线| 狠狠色综合网| 国产精品毛片在线看| 乱码第一页成人| 久久精彩免费视频| 亚洲少妇最新在线视频| 欧美插天视频在线播放| 亚洲欧美日韩精品久久亚洲区 | 亚洲欧美激情四射在线日 | 亚洲图片欧美午夜| 激情综合亚洲| 国产精品一二三四区| 老司机精品导航| 久久xxxx精品视频| 日韩一区二区免费看| 久久综合色婷婷| 羞羞色国产精品| 在线一区亚洲| 一本色道88久久加勒比精品 | 亚洲手机成人高清视频| 亚洲第一综合天堂另类专| 国产精品一二一区| 欧美日韩在线播放| 欧美~级网站不卡| 久久免费99精品久久久久久| 午夜精品免费在线| 日韩一级网站| 在线亚洲观看| 99精品国产在热久久下载| 亚洲福利视频二区| 欧美高清视频www夜色资源网| 亚洲欧美在线免费观看| 亚洲制服少妇| 亚洲一区二区视频| 一区二区三区精品视频| 午夜精品久久久久久99热| 在线性视频日韩欧美| 亚洲宅男天堂在线观看无病毒| 亚洲免费播放| 99re6这里只有精品| 亚洲精品午夜| 99精品国产福利在线观看免费| 亚洲电影网站| 亚洲国产精品福利| 亚洲人被黑人高潮完整版| 在线电影一区| 亚洲国内欧美| 正在播放亚洲一区| 99国产精品久久久久久久| 亚洲欧美经典视频| 久久综合免费视频影院| 亚洲片区在线| 羞羞答答国产精品www一本| 麻豆精品网站| 国产精品日韩精品| 曰韩精品一区二区| 亚洲一二区在线| 久久综合色一综合色88| 日韩视频在线观看免费| 欧美在线一区二区| 欧美日韩亚洲另类| 黄色亚洲大片免费在线观看| 洋洋av久久久久久久一区| 欧美在线视频在线播放完整版免费观看 | 久久久精品国产一区二区三区| 欧美激情精品久久久久久免费印度 | 亚洲人成绝费网站色www| 亚洲在线视频一区| 免费观看成人网| 国产九九精品视频| 99国产精品视频免费观看| 久久精品国产清高在天天线| 亚洲三级免费观看| 久久精品亚洲乱码伦伦中文| 欧美日韩精品一区二区| 影音先锋一区| 欧美一区亚洲一区| 日韩写真视频在线观看| 美女露胸一区二区三区| 国产欧美日韩精品专区| 中国女人久久久| 亚洲高清电影| 久久久久高清| 国产亚洲精品久| 亚洲女人av| a4yy欧美一区二区三区| 美女诱惑黄网站一区| 黑人极品videos精品欧美裸| 午夜日韩在线| 一区二区日韩| 欧美日韩国产色视频| 最新国产成人在线观看 | 久久国产免费| 亚洲欧美制服另类日韩| 欧美午夜电影在线| 夜夜嗨av一区二区三区四季av| 欧美承认网站| 久久久久国产精品人| 国产亚洲女人久久久久毛片| 欧美亚洲色图校园春色| 亚洲午夜高清视频| 欧美天天视频| 亚洲综合另类| 亚洲视频在线观看视频| 国产精品成人v| 亚洲欧美在线另类| 亚洲午夜视频| 国产欧美日本一区视频| 欧美一区二区视频97| 翔田千里一区二区| 国产日韩精品一区二区浪潮av| 欧美在线电影| 久久久噜噜噜久久|