Children of the Candy Corn

Problem Description
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'. <br> <br>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 ('#'). <br> <br>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
  
  
2 8 8 ######## #......# #.####.# #.####.# #.####.# #.####.# #...#..# #S#E#### 9 5 ######### #.#.#.#.# S.......E #.#.#.#.# #########

Sample Output
  
  
37 5 5 17 17 9

题目大概:

算出在迷宫中,一直向右走和一直向左走,走到终点的步数,还要算出最短路径。

思路:

最短路:bfs

向右,向左:dfs

代码:

借鉴自某位大佬

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
int w,h;
char map[50][50];
int vi[50][50];
int x1,y1,x2,y2;
int v;
int dleft[4][2]={{0,-1},{1,0},{0,1},{-1,0}};
int dright[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int sum;
struct point
{
    int a,b,s;
};


int pan(int q,int e)
{
    if(q>0&&q<=h&&e>0&&e<=w&&map[q][e]!='#'&&!vi[q][e])return 1;
    else return 0;
}


int bfs(int d[][2])
{

    point t1,t2;
    t1.a=x1;t1.b=y1;t1.s=0;
    queue<point>q;
    q.push(t1);
    vi[x1][y1]=1;
    while(!q.empty())
    {
        t1=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            t2.a=t1.a+d[i][0];
            t2.b=t1.b+d[i][1];
            if(pan(t2.a,t2.b))
            {
                t2.s=t1.s+1;
                q.push(t2);
                    vi[t2.a][t2.b]=1;
                if(t2.a==x2&&t2.b==y2)
                {
                    sum=min(sum,t2.s+1);
                }

            }
        }
    }
    return sum;

}




int cnt;
int dfs(int x,int y,int k,int d[][2])
{
    if(x==x2&&y==y2){return 1;}
      for(int i=0;i<4;i++)
      {
          int j=(k+i)%4;
          int dx=x+d[j][1];
          int dy=y+d[j][0];
          if(pan(dx,dy))
          {
              cnt=dfs(dx,dy,(j+3)%4,d)+1;
              break;
          }
      }



  return cnt;
}

int main()
{int t;
      cin>>t;
      for(int i=1;i<=t;i++)
      {   sum=1000000;

          memset(vi,0,sizeof(vi));
          cin>>w>>h;
          for(int j=1;j<=h;j++)
          {
              for(int k=1;k<=w;k++)
              {
                  cin>>map[j][k];

                  if(map[j][k]=='S'){x1=j;y1=k;}
                  else if(map[j][k]=='E'){x2=j;y2=k;}

              }
          }
        int k1,k2;
        if(x1==1){k1=1;k2=1;}
        else if(x1==h){k1=3;k2=3;}
        else if(y1==w){k1=0;k2=2;}
        else {k1=2;k2=0;}
          cnt=0;
          int aa=dfs(x1,y1,k1,dleft);
          cnt=0;
          memset(vi,0,sizeof(vi));
          int bb=dfs(x1,y1,k2,dright);
          memset(vi,0,sizeof(vi));
          int cc=bfs(dright);
          cout<<aa<<" "<<bb<<" "<<cc<<endl;

      }


    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值