• <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>

            PKU 2251 用DFS TLE了 BFS AC了

                                                                                   Dungeon Master
            Time Limit: 1000MS Memory Limit: 65536K
            Total Submissions: 6561 Accepted: 2610

            Description

            You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

            Is an escape possible? If yes, how long will it take?

            Input

            The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
            L is the number of levels making up the dungeon.
            R and C are the number of rows and columns making up the plan of each level.
            Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.

            Output

            Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
            Escaped in x minute(s).

            where x is replaced by the shortest time it takes to escape.
            If it is not possible to escape, print the line
            Trapped!

            Sample Input

            3 4 5
            S....
            .###.
            .##..
            ###.#
            #####
            #####
            ##.##
            ##...
            #####
            #####
            #.###
            ####E
            1 3 3
            S##
            #E#
            ###
            0 0 0
            

            Sample Output

            Escaped in 11 minute(s).
            Trapped!
            

            三維迷宮問題
            據說要用BFS,范圍30 ,我以為DFS能搞定的,為什么會超時呢????
             1#include<iostream>
             2#include<string.h>
             3using namespace std;
             4char G[35][35][35];
             5bool g[35][35][35];
             6void dfs(int l,int r,int c)
             7{
             8     if(G[l][r][c]==0||G[l][r][c]=='#')return ;
             9     if((G[l+1][r][c]=='.'||G[l+1][r][c]>G[l][r][c])&&G[l+1][r][c]!=0&&G[l+1][r][c]!='#') { G[l+1][r][c]=G[l][r][c]+1;g[l+1][r][c]=1;dfs(l+1,r,c);}
            10     if((G[l][r-1][c]=='.'||G[l][r-1][c]>G[l][r][c])&&G[l][r-1][c]!=0&&G[l][r-1][c]!='#') { G[l][r-1][c]=G[l][r][c]+1;g[l][r-1][c]=1;dfs(l,r-1,c);}  
            11     if((G[l][r+1][c]=='.'||G[l][r+1][c]>G[l][r][c])&&G[l][r+1][c]!=0&&G[l][r+1][c]!='#') { G[l][r+1][c]=G[l][r][c]+1;g[l][r+1][c]=1;dfs(l,r+1,c);}
            12     if((G[l][r][c-1]=='.'||G[l][r][c-1]>G[l][r][c])&&G[l][r][c-1]!=0&&G[l][r][c-1]!='#') { G[l][r][c-1]=G[l][r][c]+1;g[l][r][c-1]=1;dfs(l,r,c-1);}
            13     if((G[l][r][c+1]=='.'||G[l][r][c+1]>G[l][r][c])&&G[l][r][c+1]!=0&&G[l][r][c+1]!='#') { G[l][r][c+1]=G[l][r][c]+1;g[l][r][c+1]=1;dfs(l,r,c+1);}
            14     if((G[l-1][r][c]=='.'||G[l-1][r][c]>G[l][r][c])&&G[l-1][r][c]!=0&&G[l-1][r][c]!='#') { G[l-1][r][c]=G[l][r][c]+1;g[l-1][r][c]=1;dfs(l-1,r,c);}
            15}
            16
            17int main()
            18{    
            19     int i,j,k,l,r,c,sl,sr,sc,el,er,ec;  //起點,終點 
            20     while(cin>>l>>r>>c,l||r||c)
            21     {
            22        memset(G,0,sizeof (G));
            23        memset(g,0,sizeof (g));
            24        for(i=1; i<=l ;i++)
            25        for(j=1; j<=r; j++)
            26        for(k=1; k<=c; k++)
            27        {
            28          cin>>G[i][j][k];
            29          if(G[i][j][k]=='S')
            30          { sl=i; sr=j; sc=j; G[i][j][k]=1; g[i][j][k]=1; }
            31          else if(G[i][j][k]=='E')
            32          {el=i; er=j; ec=k; G[i][j][k]=127; }
            33        }           
            34        
            35        dfs(sl,sr,sc); 
            36      /**//*  for(i=1; i<=l ; i++,cout<<endl)
            37        for(j=1; j<=r; j++,cout<<endl)
            38        for(k=1; k<=c; k++)
            39        cout<<(int)G[i][j][k]<<" ";
            40        for(i=1; i<=l ; i++,cout<<endl)
            41        for(j=1; j<=r; j++,cout<<endl)
            42        for(k=1; k<=c; k++)
            43        cout<<(int)g[i][j][k]<<" ";
            44        */if(g[el][er][ec]==0)cout<<"Trapped!"<<endl;
            45        else cout<<"Escaped in "<<int(G[el][er][ec]-1)<<" minute(s)."<<endl;
            46     }
            47    // system("pause");
            48    return 0;
            49}

            寫個BFS終于過了,只要在平面的迷宮基礎上在垂直方向往上,往下都搜兩次就可以了
             1 #include<iostream>
             2 #include<string.h>
             3 #include<queue>
             4 using namespace std;
             5 char G[35][35][35];
             6 int cnt[35][35][35];
             7 struct type
             8 int l,r,c; };
             9 
            10 int main()
            11 {
            12     int l,c,r,sl,sc,sr,el,ec,er,i,j,k;
            13     void bfs(int sl,int sr,int sc);
            14     while(cin>>l>>r>>c,l||c||r)
            15     {
            16       memset(G,0,sizeof (G));
            17       memset(cnt,0,sizeof (cnt));
            18       for(i=1; i<=l; i++)
            19       for(j=1; j<=r; j++)
            20       for(k=1; k<=c; k++)
            21       {
            22         cin>>G[i][j][k];
            23         if(G[i][j][k]=='S'){sl=i; sr=j; sc=k; }
            24         else if(G[i][j][k]=='E'){el=i; er=j; ec=k; }
            25       }
            26     
            27       bfs(sl,sr,sc);
            28    //  for(i=1; i<=l; i++,cout<<endl)
            29     // for(j=1; j<=r; j++,cout<<endl)
            30      //for(k=1; k<=c; k++)
            31      //cout<<cnt[i][j][k]<<' ';
            32     
            33     if(el==sl&&er==sr&&ec==sc)cout<<"Escaped in "<<0<<" minute(s). "<<endl;
            34     else if(cnt[el][er][ec]>0)cout<<"Escaped in "<<cnt[el][er][ec]<<" minute(s). "<<endl;
            35     else cout<<"Trapped! "<<endl;
            36     }
            37    // system("pause");
            38     return 0;
            39 }
            40 
            41 bool valid(int l,int r,int c)
            42 {
            43   return (G[l][r][c]=='.'||G[l][r][c]=='E');  
            44 }
            45 
            46 void bfs(int sl,int sr, int sc)
            47 {
            48   queue<struct type> q;
            49   struct type tem; tem.l=sl; tem.r=sr; tem.c=sc; 
            50   q.push(tem);
            51   while(q.size())
            52   {
            53     tem=q.front(); q.pop();
            54     int l=tem.l,r=tem.r,c=tem.c;
            55     if(valid(l,r+1,c)&&cnt[l][r+1][c]==0){cnt[l][r+1][c]=cnt[l][r][c]+1;tem.l=l; tem.r=r+1; tem.c=c; q.push(tem); }
            56     if(valid(l,r-1,c)&&cnt[l][r-1][c]==0){cnt[l][r-1][c]=cnt[l][r][c]+1;tem.l=l; tem.r=r-1; tem.c=c; q.push(tem); }
            57     if(valid(l,r,c+1)&&cnt[l][r][c+1]==0){cnt[l][r][c+1]=cnt[l][r][c]+1;tem.l=l; tem.r=r; tem.c=c+1; q.push(tem); }
            58     if(valid(l,r,c-1)&&cnt[l][r][c-1]==0){cnt[l][r][c-1]=cnt[l][r][c]+1;tem.l=l; tem.r=r; tem.c=c-1; q.push(tem); }
            59     if(valid(l-1,r,c)&&cnt[l-1][r][c]==0){cnt[l-1][r][c]=cnt[l][r][c]+1;tem.l=l-1; tem.r=r; tem.c=c; q.push(tem); }
            60     if(valid(l+1,r,c)&&cnt[l+1][r][c]==0){cnt[l+1][r][c]=cnt[l][r][c]+1;tem.l=l+1; tem.r=r; tem.c=c; q.push(tem); }
            61     
            62   }
            63 }

            posted on 2010-05-28 21:50 田兵 閱讀(998) 評論(0)  編輯 收藏 引用 所屬分類: 算法筆記

            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            導航

            統計

            常用鏈接

            留言簿(2)

            隨筆分類(65)

            隨筆檔案(65)

            文章檔案(2)

            ACM

            搜索

            積分與排名

            最新隨筆

            最新評論

            閱讀排行榜

            久久人妻少妇嫩草AV无码专区 | 国产精品久久亚洲不卡动漫| 中文字幕日本人妻久久久免费 | 99精品久久久久久久婷婷| 狠狠色婷婷久久综合频道日韩 | 久久人人爽人人爽人人AV东京热 | 亚洲精品午夜国产va久久| 亚洲国产精品无码久久98| 久久亚洲国产中v天仙www| 色婷婷久久综合中文久久一本| 奇米影视7777久久精品| 成人a毛片久久免费播放| 久久精品aⅴ无码中文字字幕不卡| 久久91亚洲人成电影网站| 女人高潮久久久叫人喷水| 国内精品久久久久| 亚洲中文字幕无码久久精品1| 国产成人精品久久亚洲| 久久99国内精品自在现线| 国产精品久久久久a影院| 欧美精品一本久久男人的天堂| 久久久国产打桩机| 久久久久久国产a免费观看黄色大片| 2021精品国产综合久久| 久久精品人妻中文系列| 中文字幕久久精品| 婷婷久久综合九色综合绿巨人| 国产精品99久久久久久董美香| AV无码久久久久不卡蜜桃| 一本色道久久综合狠狠躁 | 久久青草国产精品一区| 久久AV高清无码| 久久精品www人人爽人人| 久久人与动人物a级毛片| 久久久久亚洲AV无码专区网站| 狠狠色综合网站久久久久久久| avtt天堂网久久精品| 99麻豆久久久国产精品免费| 天堂久久天堂AV色综合| 久久精品国产亚洲av麻豆小说 | 国产精品久久久久久久久久免费|