zoukankan      html  css  js  c++  java
  • Children of the Candy Corn

    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'.

    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

    2
    8 8
    ########
    #......#
    #.####.#
    #.####.#
    #.####.#
    #.####.#
    #...#..#
    #S#E####
    9 5
    #########
    #.#.#.#.#
    S.......E
    #.#.#.#.#
    #########

    Sample Output

    37 5 5
    17 17 9

    一道题,从学图到做出来,花了一天。

    大意:
    就是给出一个迷宫,然后按3种方式走出去:
         1.见岔路向左走。
         2.见岔路向右走。
         3.最短路。
    第三种我最陌生难以理解,学了bfs,轻松搞定,1、2花了很大力气,实在不好写,因为要记录方向,然后判断,我利用了两个数组,画图演示一下
    下面是代码,有点长,
    出了一点解释的错误,下面代码中的所有注释的栈都是·队列
      1 #include<cstdio>
      2 #include<cstdlib>
      3 #include<cstring>
      4 #include<iostream>
      5 #include<queue>
      6 using namespace std;
      7 int s[50][50],vis[50][50];
      8 int si,sj,ei,ej;
      9 int r[4]= {0,0,1,-1};//这个是访问上下左右
     10 int c[4]= {1,-1,0,0};
     11 struct node
     12 {
     13     int i,j,step;
     14 } p,ph;
     15 int ri[12]= {1,0,-1,0,1,0,-1,0,1,0,-1,0};//这个是为了方便转方向,具体用途刚才解释过
     16 int rj[12]= {0,-1,0,1,0,-1,0,1,0,-1,0,1};
     17 queue<node>que;
     18 void get(int a,int b)
     19 {
     20     int i,j;
     21     char ch;
     22     for(i=1; i<a; i++)
     23     {
     24         for(j=1; j<b; j++)
     25         {
     26             scanf("%c",&ch);//这是一个简单地输入转换
     27             if(ch=='#')
     28                 s[i][j]=0;
     29             else
     30             {
     31                 if(ch=='S')
     32                 {
     33                     si=i;
     34                     sj=j;
     35                 }
     36                 if(ch=='E')
     37                 {
     38                     ei=i;
     39                     ej=j;
     40                 }
     41                 s[i][j]=1;
     42             }
     43         }
     44         getchar();//去掉换行符
     45     }
     46 }
     47 void bfs()//广度优先搜索,找最短路径
     48 {
     49     int k,a,b;
     50     p.i=si,p.j=sj,p.step=1;
     51     que.push(p);//入栈
     52     memset(vis,0,sizeof(vis));
     53     vis[si][sj]=1;
     54     while(!que.empty())
     55     {
     56         p=que.front();//取栈顶元素
     57         que.pop();//取后弹出
     58         for(k=0; k<4; k++)
     59         {
     60             a=p.i+r[k],b=p.j+c[k];
     61             if(a==ei&&b==ej)//是要找的就返回
     62             {
     63                 printf("%d
    ",p.step+1);
     64                 return;
     65             }
     66             if(!vis[a][b]&&s[a][b])//如果未被访问过,又是可以可以走的路,就入栈
     67             {
     68                 ph.i=a,ph.j=b,ph.step=p.step+1;
     69                 que.push(ph);
     70                 vis[a][b]=1;
     71             }
     72         }
     73     }
     74 }
     75 void lr(char ch)//解决靠左靠右走的问题,合并了
     76 {
     77     int k,i,j,ia=si,ja=sj,ib,jb,step=1;
     78     for(k=0; k<4; k++)//第一步只有一种走法
     79     {
     80         if(s[ia+r[k]][ja+c[k]])
     81         {
     82             ib=ia+r[k];
     83             jb=ja+c[k];
     84             break;
     85         }
     86     }
     87     while(1)//直到找到正确的路再返回
     88     {
     89         if(ib==ei&&jb==ej)//找到正确的路返回
     90         {
     91             printf("%d ",++step);
     92             return ;
     93         }
     94         step++;
     95         i=ib-ia,j=jb-ja;
     96         ia=ib,ja=jb;
     97         for(k=3; k<8; k++)//没有的话,这里确定方向
     98             if(ri[k]==i&&rj[k]==j)
     99                 break;
    100         if(ch=='R')//看是左优先还是右优先
    101         {
    102             k++;
    103             if(s[ib+ri[k]][jb+rj[k]])//判断右侧是不是路,是就走
    104             {
    105                 ib+=ri[k],jb+=rj[k];
    106                 continue;
    107             }
    108             for(k=k-1; k>0; k--)//不是依次向左找路
    109                 if(s[ib+ri[k]][jb+rj[k]])
    110                 {
    111                     ib+=ri[k],jb+=rj[k];
    112                     break;
    113                 }
    114         }
    115         else//参考向右的
    116         {
    117             k--;
    118             if(s[ib+ri[k]][jb+rj[k]])
    119             {
    120                 ib+=ri[k],jb+=rj[k];
    121                 continue;
    122             }
    123             for(k=k+1;; k++)
    124                 if(s[ib+ri[k]][jb+rj[k]])
    125                 {
    126                     ib+=ri[k],jb+=rj[k];
    127                     break;
    128                 }
    129         }
    130     }
    131 }
    132 int main()
    133 {
    134     int p,a,b;
    135     cin>>p;
    136     getchar();
    137     while(p--)
    138     {
    139         memset(s,0,sizeof(s));
    140         cin>>b>>a;
    141         getchar();//输入不解释,其实主函数最简单 不解释了
    142         get(a+1,b+1);
    143         lr('L');
    144         lr('R');
    145         bfs();
    146     }
    147     return 0;
    148 }
    View Code
  • 相关阅读:
    技术专题:ROS通过TTL值来防止二层路由的最简单办法
    唉,一大早起床遇到脑残的,实在无语!QQ:124316912
    简单描述FTTH方案中EPON、GPON设置的优势、原理及城中村的解决方案
    9.9成新WAYOS、HZZ、ROS软件路由WAN扩展交换机大量到货只需450
    辅助工具:免输入命令,WAYOS通过交换机一键扩展WAN口工具
    配置文档:3COM 4200 3C17300A配置文件,可与WAYOS、ROS、海蜘蛛多WAN对接
    网站页面跳转代码大全,网站网页跳转代码
    popupWin 属性及用法介绍 ASP.NET控件,仿QQ,msn右下角弹出窗口
    IIS打开ASP文件出现Server Application Error提示的解决方法,本人亲历,成功
    教你学会提高无线网下载速度的方法
  • 原文地址:https://www.cnblogs.com/kongkaikai/p/3253998.html
Copyright © 2011-2022 走看看