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

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

Children of the Candy Corn [bfs][dfs]

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

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久久久久久老师| 国产精品va在线播放我和闺蜜| 欧美乱大交xxxxx另类电影| 午夜精品美女自拍福到在线| 一本一道久久a久久精品逆3p| 久久男人av资源网站| 亚洲欧美另类国产| 中文字幕精品视频| 国产精品久久久久久久久久久久| 国产日韩中文字幕| 国产一区二区色| 俺去了亚洲欧美日韩| 成人性生交大片免费看视频直播| 45www国产精品网站| 亚洲视频一区二区| 国产精品国产三级国产专播精品人| 国产精品日日摸夜夜添夜夜av| 久久欧美在线电影| 18久久久久久| 另类天堂视频在线观看| 成人免费在线视频网址| 欧美激情影音先锋| 久久视频在线播放| 伊人成人开心激情综合网| 91最新国产视频| 欧美激情视频在线免费观看 欧美视频免费一| 91精品国产自产在线观看永久| 狠狠色狠狠色综合日日小说| 日韩在线观看精品| 亚洲精品一二区| 国产精品一区二区女厕厕| 国产999精品久久久| 亚洲网站在线观看| 欧美国产极速在线| 在线成人激情黄色| 尤物九九久久国产精品的分类| 国产精品777| 国产精品丝袜高跟| 欧美亚洲激情视频| 亚洲欧美中文字幕| 久久91亚洲精品中文字幕| 亚洲欧美日韩另类| 亚洲欧美中文日韩在线v日本| 久久视频在线播放| 久久久伊人欧美| 精品福利免费观看| 美女精品久久久| 黄色一区二区三区| www.国产一区| 一区二区三区 在线观看视| 97人人模人人爽人人喊中文字| 亚洲国产高清高潮精品美女| 国产精品久久久久久搜索| 日韩免费不卡av| 亚洲欧美制服中文字幕| 日韩精品久久久久| 国产成人a亚洲精品| 欧美在线观看www| 成人精品在线观看| 欧美肥臀大乳一区二区免费视频| 2021久久精品国产99国产精品| 国内免费精品永久在线视频| 亚洲色图第三页| 精品久久久久久亚洲精品| 国产精品稀缺呦系列在线| 日本道色综合久久影院| 久久99国产精品久久久久久久久| 国产精品久久久久久av下载红粉| 国产精品视频男人的天堂| 亚洲国语精品自产拍在线观看| 91夜夜揉人人捏人人添红杏| 亚洲成人网av| 91成人在线视频| 国产亚洲精品一区二区| 欧美日产国产成人免费图片| 日韩欧美国产视频| 亚洲xxxx在线| 成人激情视频在线观看| 伊人成人开心激情综合网| 亚洲va欧美va国产综合久久| 秋霞午夜一区二区| 欧美激情小视频| 欧美一级电影在线| 久久久久久久久久久成人| 国产一区二区三区视频在线观看| 亚洲视频精品在线| 91久久精品国产91性色| 国产视频亚洲精品| 欧美日韩国产精品一区| 日韩色av导航| 欧美性猛交xxxx免费看久久久| 欧洲美女免费图片一区| 成人亚洲综合色就1024| 国产精品日韩久久久久| 91亚洲一区精品| 亚洲跨种族黑人xxx| 福利一区视频在线观看| 日韩专区中文字幕| 91福利视频在线观看| 日韩经典中文字幕| 国产精品久久久久久久久影视| 精品久久久久久中文字幕大豆网| 91av在线播放视频| 琪琪亚洲精品午夜在线| 日本亚洲欧洲色α| 青青a在线精品免费观看| 韩国三级日本三级少妇99| 欧美日韩激情视频8区| 国产精品普通话| 久久夜精品va视频免费观看| 久久夜精品香蕉| 日本视频久久久| 欧美激情第一页xxx| 欧美日韩在线免费观看| 亚洲欧洲在线播放| 久久久久久亚洲精品| 中文字幕免费国产精品| 欧美另类极品videosbest最新版本| 亚洲成人动漫在线播放| 日韩av在线播放资源| 日本欧美精品在线| 日韩欧中文字幕| 国产日韩欧美一二三区| 亚洲性生活视频在线观看| 欧美日韩亚洲激情| 欧美最顶级丰满的aⅴ艳星| 久久久久在线观看| 亚洲欧美日韩国产成人| 操人视频在线观看欧美| 91在线观看免费高清完整版在线观看| 欧美精品999| 狠狠躁夜夜躁人人躁婷婷91| 97久久伊人激情网| 国产精品久在线观看| 亚洲精品一区二区网址| 亚洲精品久久久久久久久久久久| 亚洲成色999久久网站| 欧美成人精品不卡视频在线观看| 久久精品视频播放| 欧洲亚洲免费在线| 亚洲精品日韩久久久| 久久久久久久亚洲精品| 国精产品一区一区三区有限在线| 亚洲欧美日韩爽爽影院| 亚洲欧美成人在线| 美女啪啪无遮挡免费久久网站| 欧美大片免费看| 成人日韩av在线| 久久久久久噜噜噜久久久精品| 欧美激情亚洲一区| 精品国产999| 国产一区二区三区视频在线观看| 久久精品国产一区| 久久久999国产| 91在线观看免费高清完整版在线观看| 欧美色道久久88综合亚洲精品| 精品国产老师黑色丝袜高跟鞋| 亚洲成人亚洲激情| 日韩中文字幕网站| 欧美黑人xxx| 成人免费观看网址| 精品视频偷偷看在线观看| 国产精品小说在线| 欧美日韩精品在线|