亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

Children of the Candy Corn [bfs][dfs]

2019-11-11 04:23:47
字體:
來源:轉載
供稿:網友

The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit.

One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there’s no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn’t work on mazes with exits that are not on the edge; those types of mazes are not rePResented in this problem.)

As the proprieter of a cornfield that is about to be converted into a maze, you’d like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.

Input

Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks (‘#’), empty space by periods (‘.’), the start by an ‘S’ and the exit by an ‘E’.

Exactly one ‘S’ and one ‘E’ will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls (‘#’), with the only openings being the ‘S’ and ‘E’. The ‘S’ and ‘E’ will also be separated by at least one wall (‘#’).

You may assume that the maze exit is always reachable from the start point.

Output

For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the ‘S’ and ‘E’) for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.

Sample Input28 8#########......##.####.##.####.##.####.##.####.##...#..##S#E####9 5##########.#.#.#.#S.......E#.#.#.#.##########Sample Output37 5 517 17 9

題解

迷宮算法有個貪心算法就是右墻優先算法,計算機導論老師講機器人走迷宮時講過。右邊通往右轉否則前面通前走否則左邊同左轉否則后轉,走一遍能走出迷宮,該算法對我們ACM沒有什么利用價值。對于這個題知道按它走一遍即可。

用bfs找最短路。dfs找left or right優先路徑。 巧用模運算,以動態對應不同狀態下的上下左右,如此可以省略相當一部分代碼。

//Accepted 604kB 0ms#include<stdio.h>#include<string.h>#include<queue>#define MAX_N 42using namespace std;char map[MAX_N][MAX_N];int best[MAX_N][MAX_N];int W,H,s_x,s_y,e_x,e_y;//一次是 左 上 右 下int ox[]={0,1,0,-1};int oy[]={-1,0,1,0};int bfs(){ memset(best,-1,sizeof(best)); queue<pair<int,int> > que; que.push(make_pair(s_x,s_y)); best[s_x][s_y]=1; while(!que.empty()){ int X=que.front().first,Y=que.front().second;que.pop(); int step=best[X][Y]; if(X==e_x&&Y==e_y) return step; for(int i=0;i<4;i++){ int x=X+ox[i]; int y=Y+oy[i]; if(0<=x&&x<H&&0<=y&&y<W&&map[x][y]=='.'&&best[x][y]==-1){ best[x][y]=step+1; que.push(make_pair(x,y)); } } } return -1;}int dfs1(int X,int Y,int op){ if(X==e_x&&Y==e_y) return 1; for(int i=0;i<4;i++){ int x=X+ox[(i+op)%4]; int y=Y+oy[(i+op)%4]; if(0<=x&&x<H&&0<=y&&y<W&&map[x][y]=='.') return dfs1(x,y,(i+op+3)%4)+1; }}//一次是 右 上 左 下int Ox[]={0,1,0,-1};int Oy[]={1,0,-1,0};int dfs2(int X,int Y,int op){ if(X==e_x&&Y==e_y) return 1; for(int i=0;i<4;i++){ int x=X+Ox[(i+op)%4]; int y=Y+Oy[(i+op)%4]; if(0<=x&&x<H&&0<=y&&y<W&&map[x][y]=='.') return dfs2(x,y,(i+op+3)%4)+1; }}int sove_b(){}int main(){ int T; scanf("%d",&T); while(T--){ scanf("%d%d",&W,&H); for(int i=0;i<H;i++) scanf("%s",map[i]); for(int k=0;k<H;k++) for(int j=0;j<W;j++) if(map[k][j]=='S') s_x=k,s_y=j; else if(map[k][j]=='E'){ map[k][j]='.'; e_x=k,e_y=j; } int a=dfs1(s_x,s_y,0); int b=dfs2(s_x,s_y,0); int ans=bfs(); printf("%d %d %d/n",b,a,ans); } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美日韩性视频| 国产亚洲视频在线观看| 91精品久久久久久久久久| 91在线网站视频| 欧美成人免费一级人片100| 日韩久久精品成人| 国产专区精品视频| 欧美日韩国产va另类| 久久久精品免费视频| 欧美中文字幕在线视频| 国产精品中文字幕在线观看| 国模精品视频一区二区| 欧美国产日韩xxxxx| 午夜精品99久久免费| 一区二区欧美亚洲| 国内精品一区二区三区四区| 亚洲精品国产精品自产a区红杏吧| 欧美激情喷水视频| 国产91久久婷婷一区二区| 国产成人91久久精品| 亚洲伊人一本大道中文字幕| 国产精品久久久久久久久免费看| 亚洲精品福利在线| 亚洲精品一区久久久久久| 亚洲精品成人久久久| 国产午夜精品全部视频播放| 欧美激情在线一区| 欧美精品videossex88| 亚洲精品国产综合久久| 国产91色在线播放| 亚洲欧美另类国产| 欧美极品在线播放| 4p变态网欧美系列| 国产精品一区二区三区久久| 国产精品久久久久免费a∨| 欧美性猛交xxxx乱大交| 欧美裸体xxxxx| 久久亚洲私人国产精品va| 最好看的2019年中文视频| 伊人久久久久久久久久| 国产精品一区二区久久| 国产精品白嫩初高中害羞小美女| 久久视频在线观看免费| 成人免费视频在线观看超级碰| 日韩精品视频在线免费观看| 欧美最顶级丰满的aⅴ艳星| 久久视频国产精品免费视频在线| 欧美极品xxxx| 尤物yw午夜国产精品视频| 91久久久在线| 成人网在线视频| 久久噜噜噜精品国产亚洲综合| 亚洲欧美日韩国产精品| 91久久精品美女| 中文字幕无线精品亚洲乱码一区| 日韩中文字幕第一页| 日本精品一区二区三区在线播放视频| 亚洲香蕉在线观看| 欧美福利在线观看| 久久91亚洲精品中文字幕奶水| 成人在线视频网站| 日韩精品高清视频| 亚洲男人的天堂网站| 国产综合色香蕉精品| 在线亚洲欧美视频| 久久久精品999| 日韩av大片免费看| 欧美性猛交xxxx乱大交| 国产精品成人一区二区三区吃奶| 国产精品第8页| 亚洲乱码国产乱码精品精天堂| 欧美极品美女视频网站在线观看免费| 日韩欧美一区二区在线| 日韩在线高清视频| 日韩欧美999| 欧美在线www| 亚洲欧美在线第一页| 亚洲网址你懂得| 亚洲电影在线观看| 97热在线精品视频在线观看| 亚洲视频国产视频| 亚洲精品久久久一区二区三区| 久久免费视频这里只有精品| 亚洲天堂网在线观看| 久久精品99久久久久久久久| 国内外成人免费激情在线视频网站| 久久久久久国产精品三级玉女聊斋| 亚洲国产精品人人爽夜夜爽| 成人h片在线播放免费网站| 亚洲视频精品在线| 日韩欧美亚洲范冰冰与中字| 欧美日韩不卡合集视频| 最近2019中文字幕一页二页| 国产精品电影一区| 欧美成人中文字幕| 亚洲色在线视频| 2023亚洲男人天堂| 国产精品成人av性教育| 91av在线国产| 欧美成人全部免费| 国产精品久久久久影院日本| 欧美成人精品激情在线观看| 国产精品久久久久久久久借妻| 欧美专区在线视频| 55夜色66夜色国产精品视频| 欧美日韩中文字幕日韩欧美| 国产主播喷水一区二区| 亚洲精品日韩久久久| 亚洲97在线观看| 国内精品久久久久久久久| 亚洲欧美另类人妖| 亚洲电影成人av99爱色| 国产69精品久久久| 日本成人免费在线| 日本一区二三区好的精华液| 久久久久久久影视| 欧美日韩国产成人高清视频| 亚洲剧情一区二区| 欧美性极品xxxx娇小| 中文字幕视频一区二区在线有码| 久久精品国产视频| 国产精品96久久久久久| 久久久噜噜噜久久中文字免| 欧美精品videossex性护士| 岛国av一区二区在线在线观看| 中文字幕亚洲一区| 日韩成人激情影院| 国产日韩精品在线| 清纯唯美日韩制服另类| 国产欧美一区二区三区久久| 欧美精品电影免费在线观看| 国产精品主播视频| 最近中文字幕2019免费| 色综合视频一区中文字幕| 欧美大片免费观看| 日韩福利在线播放| 欧美日韩亚洲天堂| 日韩美女写真福利在线观看| 亚洲精品自拍视频| 亚洲欧美日本另类| 欧美第一黄色网| 国产精品一区二区三区久久| 北条麻妃在线一区二区| 国产午夜精品视频免费不卡69堂| 久久久久久尹人网香蕉| 欧美激情2020午夜免费观看| 久久精品99久久香蕉国产色戒| 中文日韩电影网站| 日韩中文字幕在线精品| 欧美xxxx做受欧美.88| 亚洲国产精久久久久久| 日韩电影中文字幕在线| 亚洲人成电影网站| 国产日韩精品在线播放| 亚洲韩国青草视频| 欧美国产精品人人做人人爱| 992tv成人免费视频| 久久久久久网站| 亚洲精品欧美一区二区三区| 久久久久久久成人| 久久久精品亚洲| 亚洲精品av在线播放| 最好看的2019的中文字幕视频| 欧美电影院免费观看|