麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日本xxxx视频 | 91情侣在线偷精品国产 | 国产91大片 | 国产高潮好爽受不了了夜色 | 欧美1—12sexvideos | 国产一级毛片a | 亚洲国产精品久久久久 | 精品国产一区二区三区久久久蜜月 | 久久美女免费视频 | 91久久国产 | 欧美精品久久久久久久久久 | 国产精品久久久乱弄 | 狠狠色成色综合网 | www.54271.com | 国产精品久久77777 | 一级成人免费 | 久久人添人人爽人人爽人人片av | 国产91亚洲精品久久久 | 小视频成人| 国产成人精品免费视频大全最热 | 午夜视频在线在免费 | 国产亚洲精品久久久久婷婷瑜伽 | 中文字幕在线免费观看电影 | 久久亚洲线观看视频 | 欧美一级片一区 | 欧美成人一区二区三区电影 | 一级做a爰片性色毛片2021 | 99国语露脸久久精品国产ktv | 最新中文字幕免费视频 | 一级尻逼视频 | www.91成人| 色婷婷久久一区二区 | 国产精品亚洲欧美一级在线 | 特色一级黄色片 | 欧美一级棒 | 嗯~啊~弄嗯~啊h高潮视频 | 毛片在线免费播放 | 国产老师做www爽爽爽视频 | 亚洲第一成人久久网站 | 黄色高清视频网站 | 国产一区二区不卡视频 |