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

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

Children of the Candy Corn [bfs][dfs]

2019-11-11 05:03:55
字體:
來源:轉載
供稿:網友

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
久久精品色欧美aⅴ一区二区| 国产精品日韩欧美综合| 亚洲国产成人精品久久| 日本在线观看天堂男亚洲| 欧美精品在线免费| 伊人青青综合网站| 日韩精品在线看| 中文字幕亚洲激情| 97人洗澡人人免费公开视频碰碰碰| 久久好看免费视频| 91在线国产电影| 91在线免费网站| 亚洲国产成人在线播放| 久久精品免费播放| 欧美日韩一区二区在线| 中文字幕日韩综合av| 国内精品视频在线| 黑人巨大精品欧美一区二区一视频| 亚洲成人激情小说| 国产一区二区三区高清在线观看| 欧美精品激情blacked18| 国产精品人人做人人爽| 免费成人高清视频| 情事1991在线| 国内精品国产三级国产在线专| 欧美亚洲在线播放| 日韩精品在线电影| 国产精品九九久久久久久久| 国产成人高清激情视频在线观看| 久久久av一区| 欧美日韩国产一区二区| 亚洲国产成人久久综合| 亚洲精品电影网在线观看| 亚洲一级免费视频| 久久久久久免费精品| 日韩女优在线播放| 97不卡在线视频| 不卡av电影院| 国产视频久久久| 久久躁狠狠躁夜夜爽| 精品国偷自产在线| 国产z一区二区三区| 日韩精品视频三区| 日本欧美爱爱爱| 精品福利在线观看| 久久精品国产亚洲7777| 久久久久中文字幕2018| 久久精品99国产精品酒店日本| 国产精品视频网站| 成人黄色av免费在线观看| 91精品国产91久久| 亚洲片国产一区一级在线观看| 尤物精品国产第一福利三区| 综合网日日天干夜夜久久| 7m第一福利500精品视频| 国产亚洲一区精品| 欧美午夜宅男影院在线观看| 日韩中文字幕视频在线观看| 2019中文字幕在线免费观看| 日韩成人性视频| 欧美日本亚洲视频| 亚洲欧美成人一区二区在线电影| 久久久精品国产网站| 高清一区二区三区日本久| 国产精品视频最多的网站| 亚洲欧美日韩成人| 亚洲视频在线免费观看| 亚洲午夜色婷婷在线| 欧美性在线观看| 91国偷自产一区二区三区的观看方式| 亚洲网站在线观看| 欧美日韩亚洲高清| 中文字幕亚洲一区二区三区五十路| 日韩欧美在线观看视频| 91高潮精品免费porn| 亚洲欧美激情在线视频| 日本亚洲欧美三级| xxx欧美精品| 日韩视频―中文字幕| 国产精品夜色7777狼人| 国产成人一区二| 国产这里只有精品| 久久久久久久久久久国产| 伊人久久久久久久久久久| 亚洲精品91美女久久久久久久| 亚洲丁香久久久| 久久99国产精品久久久久久久久| 97精品国产91久久久久久| 91热精品视频| 久久的精品视频| 欧美精品18videos性欧美| 91av在线影院| 日韩中文字幕在线观看| 亚洲欧洲xxxx| 欧美午夜片欧美片在线观看| 疯狂做受xxxx欧美肥白少妇| 国产精品毛片a∨一区二区三区|国| 欧美日韩国产中文字幕| 国产成人亚洲综合青青| 欧美伊久线香蕉线新在线| 日韩精品中文字幕在线| 精品国产福利在线| 久久69精品久久久久久国产越南| 午夜精品福利电影| 国产福利视频一区| 伊人久久久久久久久久久久久| 亚洲精品国产精品国自产观看浪潮| 青草青草久热精品视频在线网站| 欧美激情一区二区久久久| 日韩av快播网址| 欧美精品做受xxx性少妇| 精品久久久久久国产| 日本伊人精品一区二区三区介绍| 国产丝袜一区二区三区| 欧美高清视频免费观看| 亚洲欧美日韩另类| 国产成人av在线| 精品久久久999| 欧美大成色www永久网站婷| 26uuu亚洲伊人春色| 日韩成人免费视频| 日韩av第一页| 国产精品久久久久免费a∨大胸| 亚洲精品98久久久久久中文字幕| 亚洲激情视频在线| 在线精品视频视频中文字幕| 欧美色道久久88综合亚洲精品| 国产精品欧美日韩一区二区| 国内精品一区二区三区四区| 美女扒开尿口让男人操亚洲视频网站| 亚洲精品福利在线| 国产精品毛片a∨一区二区三区|国| 亚洲国产一区二区三区在线观看| 国产精品一区电影| 国模精品一区二区三区色天香| 亚洲精品日韩激情在线电影| 亚洲成人av在线播放| 日韩免费观看视频| www亚洲欧美| 欧美激情精品久久久久久大尺度| 色诱女教师一区二区三区| 欧美成人激情视频| 欧美日韩黄色大片| 久久综合伊人77777尤物| 亚洲精品电影久久久| 国产精品久久久久久久久影视| 丝袜情趣国产精品| 国产欧美在线观看| 日韩中文字幕在线视频| 欧美亚州一区二区三区| 91精品久久久久久久久中文字幕| 亚洲第一男人天堂| 日韩中文字幕网址| 亚洲成人免费在线视频| 日韩av快播网址| 久久777国产线看观看精品| 黄网站色欧美视频| 国产精品91久久久久久| 国产精品一区=区| 亚洲第一av在线| 美女视频久久黄| 国产精品久久久久久久久久三级| 久久久久久香蕉网| 日韩精品在线观看视频|