*poj-3083-Children of the Candy Corn-图的BFS DFS

来源:互联网 发布:淘宝与天猫的流量分配 编辑:程序博客网 时间:2024/06/02 00:48
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 Input

28 8#########......##.####.##.####.##.####.##.####.##...#..##S#E####9 5##########.#.#.#.#S.......E#.#.#.#.##########

Sample Output

37 5 517 17 9


题意 第一个数据是从S出发到E,沿着左边走DFS,第二个沿着右边走,DFS,第三个,最短路-BFS

左走,向前一个,顺时针,右走,逆时针,可以,写一个DFS,一个是从S到E,一个是E到S,那莫都是顺时针,改一下数据就行了

 ((d-1+4)%4+i)%4;

#include<stdio.h>#include<string.h>#include<stdlib.h>int dx[4]= {1,-1,0,0},dy[] = {0,0,-1,1};int dl[][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};int n, m;int x0, y0, xs,ys;char map[151][151];int bj[151][151];struct node{    int x,y;    int step;} pos[1510];int dfs(int x, int y, int d, int step, int dir[][2]){    for(int i=0; i<4; i++)        {        int j = ((d-1+4)%4+i)%4;        int nx = x+dir[j][0];        int ny = y+dir[j][1];        if(nx == xs && ny == ys) return step+1;        if(nx < 0 || ny < 0 || nx > n || ny > m) continue;        if(map[nx][ny] == '#') continue;        return dfs(nx, ny, j, step+1, dir);    }}void bfs(){    int tl = 0, tp = 0;    pos[tl].x = x0;    pos[tl].y = y0;    pos[tl++].step = 1;    bj[x0][y0] = 1;    while(tp < tl)    {        int  x = pos[tp].x;        int y = pos[tp].y;        int step = pos[tp++].step;        if(x == xs && y== ys)        {            printf("%d\n",step);            break;        }        for(int i = 0; i < 4; i++)        {            int tx = x + dx[i], ty = y + dy[i];            if(map[tx][ty] != '#' && tx >= 0 && tx < n && ty >= 0 && ty < m && !bj[tx][ty])            {                pos[tl].x = tx;                pos[tl].y = ty;                pos[tl++].step = step+1;                bj[tx][ty] = 1;            }        }    }}int main(){    int t,i,j;    scanf("%d",&t);    while(t--)    {        memset(bj,0,sizeof(bj));        memset(pos,0,sizeof(pos));        scanf("%d%d",&m,&n);        for(i = 0; i < n; i++)        {            scanf("%s",map[i]);        }        for(i = 0; i < n; i++)            for(j = 0; j < m; j++)            {                if(map[i][j] == 'S')                {                    x0 = i;                    y0 = j;                }                else if(map[i][j] == 'E')                {                    xs = i;                    ys = j;                }            }            int tk;        printf("%d ", dfs(x0, y0, 1, 1, dl));          tk = x0;            x0 = xs;            xs = tk;            tk = y0;            y0 = ys;            ys = tk;        printf("%d ", dfs(x0, y0, 1, 1, dl));        bfs();    }    return 0;}


0 0
原创粉丝点击